You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

48 lines
1.4 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

# -*- coding: gbk -*-
import pandas as pd
from pyecharts import options as opts
from pyecharts.charts import Pie, Bar, Timeline
df = pd.read_csv('changsha天气.csv', encoding='gbk')
df['日期'] = df['日期'].apply(lambda x: pd.to_datetime(x))
df['month'] = df['日期'].dt.month
df_agg = df.groupby(['month', '风向/风速']).size().reset_index()
df_agg.columns = ['month', 'fengsu', 'count']
print(df_agg[df_agg['month'] == 1][['fengsu', '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 df_agg['month'].unique():
data = (
df_agg[df_agg['month'] == month][['fengsu', 'count']]
.sort_values(by='count', ascending=True)
.values.tolist()
)
bar = Bar()
# x轴是天气名称
bar.add_xaxis([x[0] for x in data])
# y轴是各天气出现次数
bar.add_yaxis('风向/风速情况', [x[1] for x in data])
# 让柱状图横着放
bar.reversal_axis()
# 将计数标签放在图形右边
bar.set_series_opts(label_opts=opts.LabelOpts(position='right'))
# 设置下图表的名字
bar.set_global_opts(title_opts=opts.TitleOpts(title='长沙2023年每月风向变化'))
# 将设置好的bar对象放置到时间轮播图中并且标签选择月份 格式为:数字月
timeline.add(bar, f'{month}')
timeline.render('长沙风速风向轮播图.html')