From ad0f4a206cc919bccfa8aaf834d11d45065f7717 Mon Sep 17 00:00:00 2001 From: pun8qzra7 <1245011749@qq.com> Date: Thu, 2 May 2024 12:51:21 +0800 Subject: [PATCH] ADD file via upload --- show2.py | 45 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 show2.py diff --git a/show2.py b/show2.py new file mode 100644 index 0000000..870212f --- /dev/null +++ b/show2.py @@ -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("已生成每月天气柱状图和每日气温折线图的时间轴可视化") \ No newline at end of file