空气质量.xlsx
导出下载为本地文件
然后在Jupyter的home页面导入文件,就会与你的代码文件在同一目录下
import pandas as pd
import json
from pyecharts.charts import *
import pyecharts.options as opts
from pyecharts.globals import ThemeType, ChartType
data = pd.read_excel("./空气质量.xlsx")
data.head() #.head( ) :是指取数据的前n行数据,默认是前5行(没有print语句)
weather_level = data['质量等级'].unique() #.unique() 去重且不改变顺序
data['质量等级'].value_counts() #value_counts()查看表格某列中有多少个不同值,并计算每个值的重复次数。
quality_dict = {weather_level[i]:i+1 for i in range(len(weather_level))}# 用数字代表污染程度
quality_dict
data['level'] = data['质量等级'].map(quality_dict) #加了level一列
data.head()
city_list = data['城市'].unique()
province_list = data['省'].unique()
time_list = data['日期'].unique()
可检查数据
国家地图展示不出来,就不放截图了
n =32
def timeline1():
# 创建轮播图
timeline = Timeline(init_opts = opts.InitOpts(width='1200px', height='700px'))
for t in time_list[:n]:
data_one_day = []
# 取出所有城市当天空气污染质量值
for i in city_list:
index_c = (data['城市'] == i) & (data['日期'] == t)
data_one_day.append((i, int(data.loc[index_c, 'level'])))
geo = (Geo()
.add_schema(maptype="china", itemstyle_opts=opts.ItemStyleOpts(border_color="#f7f8fa"))
.add("", data_one_day, type_=ChartType.EFFECT_SCATTER)
.set_series_opts(itemstyle_opts=opts.ItemStyleOpts(color='green'),
label_opts=opts.LabelOpts(is_show=False))
.set_global_opts(title_opts=opts.TitleOpts(title="全国空气质量等级",
subtitle="{}更新".format(t),
pos_left='center'),
visualmap_opts=opts.VisualMapOpts(is_show=True, min_ = 0, max_ = 6, precision = 1,
is_piecewise=True, split_number=6,
pieces=[
{"max":6, "min":5, "label":"严重污染",
"color":"#B40404"},
{"max":5, "min":4, "label":"重度污染",
"color":"#DF0101"},
{"max":4, "min":3, "label":"中度污染",
"color":"#F78181"},
{"max":3, "min":2, "label":"轻度污染",
"color":"#F5A9A9"},
{"max":2, "min":1, "label":"良",
"color":"#b7dd6f"},
{"max":1, "min":0, "label":"优",
"color":"#32e0c4"},
])
)
)
timeline.add(geo, t)
timeline.add_schema(
play_interval = 3000, #轮播速度
is_timeline_show = True,#是否显示timeline组件
is_auto_play=True
)
return timeline
timeline1().render_notebook()
def timeline2():
timeline = Timeline(init_opts = opts.InitOpts(width = "600px", height = "600px"))
for ind, t in enumerate(time_list[:n]):
data_t = data.loc[data['日期'] == t,:]
#取出空气质量最差的十个城市
data_top10 = data_t.sort_values(by='AQI指数', ascending = True).tail(10)
#放入图
bar = (Bar()
.add_xaxis(data_top10['城市'].tolist())
.add_yaxis('PM2.5', data_top10['PM2.5'].tolist(), stack='stack1')
.add_yaxis('PM10', data_top10['PM10'].tolist(), stack='stack1')
.add_yaxis('So2', data_top10['So2'].tolist(), stack='stack1')
.add_yaxis('No2', data_top10['No2'].tolist(), stack='stack1')
.reversal_axis()#转置
.set_series_opts(label_opts=opts.LabelOpts(is_show=False))
.set_global_opts(
title_opts = opts.TitleOpts(title = "空气质量最差的10个城市情况",
pos_left='center'),
xaxis_opts=opts.AxisOpts(axislabel_opts=opts.LabelOpts(is_show=False),
axisline_opts=opts.AxisLineOpts(is_show=False),
axistick_opts=opts.AxisTickOpts(is_show=False)
),
legend_opts=opts.LegendOpts(pos_top="6%")
)
)
timeline.add(bar, t)
#配置轮播图
timeline.add_schema(
play_interval=3000, # 轮播速度
is_timeline_show=True, #是否显示timeline组件
is_auto_play=True
)
return timeline
timeline().render_notebook()
def line():
data_group = pd.pivot_table(data,
index="日期",
columns="质量等级",
values ="城市",
aggfunc=len,
fill_value=0)
line = (Line()
.add_xaxis(data_group.index.tolist())
.add_yaxis('轻度污染', data_group['轻度污染'].tolist(),
linestyle_opts=opts.LineStyleOpts(type_='solid',
color='red'))
.add_yaxis('中度污染', data_group['中度污染'].tolist(),
linestyle_opts=opts.LineStyleOpts(type_='dotted',
color='green'))
.add_yaxis('重度污染', data_group['重度污染'].tolist(),
linestyle_opts=opts.LineStyleOpts(type_='dashed',
color='blue'))
.add_yaxis('严重污染', data_group['严重污染'].tolist(),
linestyle_opts=opts.LineStyleOpts(type_='',
color='orange'))
.set_series_opts(label_opts=opts.LabelOpts(is_show=False))
.set_global_opts(
title_opts=opts.TitleOpts(title='8月份污染天气变化情况',
pos_left='right',
pos_top='5%'),
legend_opts=opts.LegendOpts(pos_left='right', pos_top='10%')
)
)
return line
line().render_notebook()
page = Page(layout=Page.DraggablePageLayout)
page.add(
timeline1(),
timeline2(),
line()
)
page.render()
会生成一个html文件,如果复制到浏览器打不开,就从文件资源管理器中打开
打开后,点击Save Config
按钮
另存到本地文件后,导入Jupyter Home页面文件夹
重命名保存到Jupyter Home页面文件:
Page.save_resize_html('render.html', cfg_file='chart_config.json', dest='空气可视化.html')