基于flask的天气数据可视化系统
立即下载
资源介绍:
基于flask的天气数据可视化系统
from flask import Flask, render_template
from pyecharts.charts import *
from pyecharts import options as opts
import pandas as pd
app = Flask(__name__)
df = pd.read_csv('data/weather.csv')
df['date'] = pd.to_datetime(df['date'])
df['year'] = df['date'].dt.year
df['month'] = df['date'].dt.month
@app.route('/')
def index():
return render_template("index.html")
def get_line():
df_beijing = df[(df['city'] == '北京') & (df['year'] == 2020)].sort_values("date")
df_beijing['日期s'] = df_beijing['date'].dt.strftime('%Y/%m/%d')
df_beijing['high_temp'] = df_beijing['high_temp'].apply(lambda x: x.replace('℃', '')).astype('int64')
df_beijing['low_temp'] = df_beijing['low_temp'].apply(lambda x: x.replace('℃', '')).astype('int64')
x_data = df_beijing['日期s'].tolist()
y_data1 = df_beijing['high_temp'].tolist()
y_data2 = df_beijing['low_temp'].tolist()
line = (
Line()
.add_xaxis(x_data)
.add_yaxis("最高气温", y_data1, is_smooth=False) # is_smooth默认是False,即折线
.add_yaxis("最低气温", y_data2, is_smooth=False) # is_smooth默认是False,即折线
.set_global_opts(
title_opts=opts.TitleOpts(title="2020年北京市温度分布图"),
toolbox_opts=opts.ToolboxOpts(),
legend_opts=opts.LegendOpts(is_show=True, pos_left='center', pos_top='top', item_width=25,
item_height=25,
legend_icon='circle'),
xaxis_opts=opts.AxisOpts(name='日期', name_textstyle_opts=opts.TextStyleOpts(color='red', font_size=20),
axislabel_opts=opts.LabelOpts(font_size=15)),
yaxis_opts=opts.AxisOpts(name='温度', name_textstyle_opts=opts.TextStyleOpts(color='red', font_size=20),
axislabel_opts=opts.LabelOpts(font_size=15), name_location="middle")
)
.set_series_opts(label_opts=opts.LabelOpts(is_show=False, position='top', color='black', font_size=15))
)
return line
def get_pie():
df_beijing = df[(df['city'] == '北京') & (df['year'] == 2020)].sort_values("date")
df_beijing['日期'] = df_beijing['date'].dt.strftime('%Y/%m/%d')
df_beijing['high_temp'] = df_beijing['high_temp'].apply(lambda x: x.replace('℃', '')).astype('int64')
df_beijing['low_temp'] = df_beijing['low_temp'].apply(lambda x: x.replace('℃', '')).astype('int64')
df_beijing['month'] = df_beijing['date'].dt.month
dd = ['北风', '西北风', '西风', '西南风', '南风', '东南风', '东风', '东北风']
df_wind_direction = df_beijing.groupby('wind_direction')["wind_direction"].count()
x_data = df_wind_direction.index.tolist()
y_data = df_wind_direction.values.tolist()
dic_tmp = dict([list(z) for z in zip(x_data, y_data)])
values = [dic_tmp[i] for i in dd]
c = (
Polar()
.add_schema(
radiusaxis_opts=opts.RadiusAxisOpts(data=dd, type_="category"), # 设置半径
)
.add("", values, type_="scatter") # bar scatter stack="stack0"
.set_global_opts(
title_opts=opts.TitleOpts(title="2020年北京市风向分布图"), # 设置标题
tooltip_opts=opts.TooltipOpts(trigger="axis", axis_pointer_type="cross") # 设置鼠标悬停工具
)
)
return c
def get_bar():
df_beijing = df[(df['city'] == '北京') & (df['year'] == 2020)].sort_values("date")
df_beijing['日期'] = df_beijing['date'].dt.strftime('%Y/%m/%d')
df_beijing['high_temp'] = df_beijing['high_temp'].apply(lambda x: x.replace('℃', '')).astype('int64')
df_beijing['low_temp'] = df_beijing['low_temp'].apply(lambda x: x.replace('℃', '')).astype('int64')
df_beijing['month'] = df_beijing['date'].dt.month
df_agg = df_beijing.groupby(['month', 'weather']).size().reset_index()
df_agg.columns = ['month', 'tianqi', 'count']
# 实例化一个时间序列的对象
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][['tianqi', '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="北京市2020年每月天气变化"))
# 将设置好的bar对象放置到时间轮播图当中,并且标签选择月份 格式为:数字月
timeline.add(bar, f'{month}月')
return timeline
@app.route('/line')
def line():
line = get_line()
return render_template("line.html", line_options=line.dump_options())
@app.route('/pie')
def pie():
pie = get_pie()
return render_template("pie.html", pie_options=pie.dump_options())
@app.route('/bar')
def bar():
bar = get_bar()
return render_template("bar.html", bar_options=bar.dump_options())
if __name__ == '__main__':
app.run(debug=True)