|
|
import pandas as pd
|
|
|
from pyecharts import options as opts
|
|
|
from pyecharts.charts import Pie, Bar, Timeline
|
|
|
|
|
|
js = pd.read_csv('changsha天气.csv', encoding='gbk')
|
|
|
|
|
|
js['日期'] = js['日期'].apply(lambda x: pd.to_datetime(x))
|
|
|
|
|
|
js['month'] = js['日期'].dt.month
|
|
|
js_agg1 = js.groupby(['month', '天气']).size().reset_index()
|
|
|
|
|
|
js_agg1.columns = ['month', 'tianqi', 'count']
|
|
|
|
|
|
print(js_agg1[js_agg1['month'] == 1][['tianqi', 'count']] \
|
|
|
.sort_values(by='count', ascending=False).values.tolist())
|
|
|
|
|
|
#实例化一个时间序列的对象
|
|
|
timeline = Timeline()
|
|
|
#播放参数:设置时间间隔1s 单位是:ms(毫秒)
|
|
|
timeline.add_schema(play_interval=1000)
|
|
|
|
|
|
#循环遍历df_agg['month']里的唯一值
|
|
|
for month in js_agg1['month'].unique():
|
|
|
data1 = (
|
|
|
js_agg1[js_agg1['month'] == month][['tianqi', 'count']]
|
|
|
.sort_values(by='count', ascending=True)
|
|
|
.values.tolist()
|
|
|
)
|
|
|
|
|
|
# print(data) #绘制柱状图
|
|
|
bar1 = Bar()
|
|
|
# x轴是天气名称
|
|
|
bar1.add_xaxis([x[0] for x in data1])
|
|
|
# y轴是各天气出现次数
|
|
|
bar1.add_yaxis('天气情况', [x[1] for x in data1])
|
|
|
|
|
|
# 让柱状图横着放
|
|
|
bar1.reversal_axis()
|
|
|
# 将计数标签放在图形右边
|
|
|
bar1.set_series_opts(label_opts=opts.LabelOpts(position='right'))
|
|
|
# 设置下图表的名字
|
|
|
bar1.set_global_opts(title_opts=opts.TitleOpts(title='长沙2023年每月天气变化'))
|
|
|
# 将设置好的bar对象放置到时间轮播图中,并且标签选择月份 格式为:数字月
|
|
|
timeline.add(bar1, f'{month}月')
|
|
|
timeline.render('长沙天气轮播图.html')
|