parent
5d5a42f749
commit
ad0f4a206c
@ -0,0 +1,45 @@
|
|||||||
|
import pandas as pd
|
||||||
|
from pyecharts import options as opts
|
||||||
|
from pyecharts.charts import Bar, Line, Grid, Timeline
|
||||||
|
|
||||||
|
# 读取CSV文件
|
||||||
|
df = pd.read_csv('weather.csv', encoding='utf-8')
|
||||||
|
|
||||||
|
# 将日期列转换为 datetime 类型
|
||||||
|
df['日期'] = pd.to_datetime(df['日期'])
|
||||||
|
|
||||||
|
# 新增月份列
|
||||||
|
df['month'] = df['日期'].dt.month
|
||||||
|
|
||||||
|
# 创建 Timeline 实例
|
||||||
|
timeline = Timeline()
|
||||||
|
|
||||||
|
# 创建每月天气柱状图和气温折线图
|
||||||
|
for month in df['month'].unique():
|
||||||
|
# 过滤出当前月份的数据
|
||||||
|
data = df[df['month'] == month]
|
||||||
|
|
||||||
|
# 创建柱状图
|
||||||
|
bar = Bar()
|
||||||
|
bar.add_xaxis(data['天气'].value_counts().index.tolist())
|
||||||
|
bar.add_yaxis('', data['天气'].value_counts().tolist(), category_gap="50%")
|
||||||
|
bar.set_global_opts(title_opts=opts.TitleOpts(title=f'桂林2023年{month}月天气变化'))
|
||||||
|
|
||||||
|
# 创建折线图
|
||||||
|
line = Line()
|
||||||
|
line.add_xaxis(data['日期'].dt.strftime('%Y-%m-%d').tolist())
|
||||||
|
line.add_yaxis('最高气温', data['最高气温'].tolist())
|
||||||
|
line.add_yaxis('最低气温', data['最低气温'].tolist())
|
||||||
|
|
||||||
|
# 创建 Grid 组件,将柱状图和折线图放在同一个 Grid 中
|
||||||
|
grid = Grid(init_opts=opts.InitOpts(theme="light"))
|
||||||
|
grid.add(bar, grid_opts=opts.GridOpts(pos_left="5%", pos_right="25%", pos_top="10%", pos_bottom="55%"))
|
||||||
|
grid.add(line, grid_opts=opts.GridOpts(pos_left="5%", pos_right="25%", pos_top="55%", pos_bottom="15%"))
|
||||||
|
|
||||||
|
# 将 Grid 添加到 Timeline 中
|
||||||
|
timeline.add(grid, f'{month}月')
|
||||||
|
|
||||||
|
# 渲染生成 HTML 文件
|
||||||
|
timeline.render('weather_timeline.html')
|
||||||
|
|
||||||
|
print("已生成每月天气柱状图和每日气温折线图的时间轴可视化")
|
Loading…
Reference in new issue