要在jupyter notebook中一行行运行
以爬取国家数据网数据举例子:https://data.stats.gov.cn
爬取数据
#爬取数据
import requests
url="https://data.stats.gov.cn/easyquery.htm?m=QueryData&dbcode=fsnd&rowcode=reg&colcode=sj&wds=%5B%7B%22wdcode%22%3A%22zb%22%2C%22valuecode%22%3A%22A0M0101%22%7D%5D&dfwds=%5B%5D&k1=1717554144150&h=1"
headers={"User-Agent":
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/125.0.0.0 Safari/537.36 Edg/125.0.0.0"}
response=requests.get(url=url,headers=headers)
datajson=response.json()#获取数据
datajson
datajson["returndata"]["datanodes"]#从 datajson 中获取名为 “returndata” 的对象,然后从这个对象中获取名为 “datanodes” 的属性或字段。
整理数据
import pandas as pd
df1=pd.DataFrame(datajson["returndata"]["datanodes"])#列表转化为dataframe
df1['value']=[df1['data'][i]['data']
for i in range(310)]
df1['year']=[df1['wds'][i][2]['valuecode']
for i in range(310)]
df1['gaozhongshu']=[df1['wds'][i][0]['valuecode']
for i in range(310)]
df1['shengfen']=[df1['wds'][i][1]['valuecode']
for i in range(310)]
df1.drop(['data','wds','code'],axis=1,inplace=True)
df2=pd.DataFrame(datajson['returndata']['wdnodes'][1]['nodes'])
df3=df2[['cname','code']]
df=pd.merge(df1,df3,left_on='shengfen',right_on='code')
df.to_csv("data/gaozhongshu.csv")
df
读取数据
#读取刚保存的csv
df = pd.read_csv("data/gaozhongshu.csv")
df['year']=df['year'].astype(str)
datapair2022=df[df['year']=='2022'][['cname','value']].values.tolist()
datapair2022
绘制地图Map
from pyecharts.charts import Map
from pyecharts import options as opts
map=Map(init_opts=opts.InitOpts(width="800px",height="800px"))
map.add("高中数",datapair2022,
maptype="china",is_roam=False)
map.set_global_opts(visualmap_opts=opts.VisualMapOpts(is_show=True,min_=10,max_=170))
map.render_notebook()
绘地图Timeline
years=[str(year) for year in range(2014,2023)]
datapairs={}
for year in years:
datapairs[year]=df[df['year']==year][['cname','value']].values.tolist()
datapairs['2022']
#绘制地图Timeline
from pyecharts.charts import Timeline
timeline=Timeline()
for year in years:
map1=Map(init_opts=opts.InitOpts(width="800px",height="800px"))
map1.add("",datapairs[year],maptype="china",is_roam=False)
map1.set_global_opts(visualmap_opts=opts.VisualMapOpts(is_show=False,min_=10,max_=170))
timeline.add(map1,year)
timeline.render_notebook()
绘制横向条形图Timeline1
#绘制横向条形图
timeline1=Timeline()
for year in years:
bar=Bar()
x=[i[0] for i in datapairs[year]]
y=[i[1] for i in datapairs[year]]
bar.add_xaxis(x)
bar.add_yaxis("",y,itemstyle_opts={"normal":{"color":"violet","shadowColor":"cyan","shadowBlur":5}})
bar.reversal_axis()
bar.set_series_opts(label_opts=opts.LabelOpts(is_show=True,position="right",formatter="{b}:{c}"))
bar.set_global_opts(title_opts=opts.TitleOpts(title="各省份高中数",title_textstyle_opts=opts.TextStyleOpts(color="black",font_size=20))
,legend_opts=opts.LegendOpts(type_="plain",is_show=True,pos_top="50%",pos_left="90%"
,textstyle_opts=opts.TextStyleOpts(color="red",font_size=15)))
timeline1.add(bar,year)
# 设置时间轴的全局选项,实现自动播放且隐藏时间轴
timeline1.add_schema(is_auto_play=True, is_timeline_show=False)
timeline1.render_notebook()
取出省份数据
df
datashanxi=df[df['cname']=="陕西省"][['year','value']].sort_values(by="year")
datashanxi
datashanxi.drop(260,axis=0,inplace=True)
datashanxi=datashanxi.values.tolist()
datashanxi
创建折线图
from pyecharts.charts import Line
x1=[i[0] for i in datashanxi]
y1=[i[1] for i in datashanxi]
line=(Line(init_opts=opts.InitOpts(width='800px', height='400px', bg_color='white')))
#要调整背景颜色,可以在init_opts中设置bg_color参数
#创建了一个Line对象,设置了图表的宽度和高度
line.add_xaxis(x1)
line.add_yaxis("陕西省2014年-2022年高中数",y1,symbol="circle",is_symbol_show=True,itemstyle_opts={"color": "violet"},symbol_size=20)
line.set_global_opts(yaxis_opts=opts.AxisOpts(min_=90,max_=100))
line.render_notebook()
创建折线图Timeline2
#创建折线图Timeline2
from pyecharts.charts import Timeline
timeline2=Timeline()
for index,year in enumerate(years):
line=(Line(init_opts=opts.InitOpts(width='800px', height='400px', bg_color='white')))
line.add_xaxis(x1)
line.add_yaxis("陕西省2014年-2022年高中数",y1[0:index+1],symbol="circle",is_symbol_show=True,itemstyle_opts={"color": "violet"},symbol_size=20)
line.set_global_opts(yaxis_opts=opts.AxisOpts(min_=90,max_=100))
timeline2.add(line,year)
# 设置时间轴的全局选项,实现自动播放且隐藏时间轴
timeline2.add_schema(is_auto_play=True, is_timeline_show=False)
timeline2.render_notebook()
创建散点图Timeline3
#创建散点图Timeline3
from pyecharts.charts import Scatter
timeline3=Timeline()
for index,year in enumerate(years):
scatter=Scatter()
scatter.add_xaxis(x1)
scatter.add_yaxis("",y1[0:index+1])
scatter.set_global_opts(yaxis_opts=opts.AxisOpts(min_=90,max_=100),visualmap_opts=[opts.VisualMapOpts(is_show=True,type_="size",min_=90,max_=100)
,opts.VisualMapOpts(is_show=True,type_="color")])
timeline3.add(scatter,year)
# 设置时间轴的全局选项,实现自动播放且隐藏时间轴
timeline3.add_schema(is_auto_play=True, is_timeline_show=False)
timeline3.render_notebook()
from pyecharts.charts import Scatter,EffectScatter
timeline3=Timeline()
for index,year in enumerate(years):
esc=EffectScatter()
esc.add_xaxis(x1)
esc.add_yaxis("",y1[0:index+1],symbol_size=20, # 设置点的大小
symbol='triangle', # 设置点的形状为三角形
color='violet')
esc.set_global_opts(yaxis_opts=opts.AxisOpts(min_=90,max_=100))
timeline3.add(esc,year)
timeline3.render_notebook()
创建饼图Timeline4
#创建饼图Timeline4
from pyecharts.charts import Pie
timeline4=Timeline()
# 遍历年份列表并设置索引
for index,year in enumerate(years):
# 创建一个饼图对象
pie=Pie(init_opts=opts.InitOpts(width="800px",height="800px"))
# 添加饼图数据,使用 datapairs 中对应年份的数据
pie.add("", datapairs[year], center=["50%", "50%"])
# 设置全局选项,包括图例的位置和是否显示
pie.set_global_opts(legend_opts=opts.LegendOpts(orient="vertical", pos_top="1%", pos_left="1%", is_show=False))
# 设置全局视觉映射选项,这里设置为不显示视觉映射
map1.set_global_opts(visualmap_opts=opts.VisualMapOpts(is_show=False,min_=600,max_=15000))
# 将饼图对象添加到时间轴上,指定年份作为时间轴上的标签
timeline4.add(pie,year)
# 设置时间轴的全局选项,实现自动播放且隐藏时间轴
timeline4.add_schema(is_auto_play=True, is_timeline_show=False)
# 在Notebook中渲染时间轴
timeline4.render_notebook()
from pyecharts.charts import Pie
timeline4=Timeline()
for index,year in enumerate(years):
pie=Pie(init_opts=opts.InitOpts(width="800px",height="800px"))
pie.add("",datapairs[year],center=["50%","50%"],radius=["10%","25%"])
pie.set_global_opts(legend_opts=opts.LegendOpts(is_show=False,orient="vertical",pos_top="50%", pos_left="50%"))
map1.set_global_opts(visualmap_opts=opts.VisualMapOpts(is_show=False,min_=600,max_=15000))
timeline4.add(pie,year)
timeline4.render_notebook()
用Page绘制大屏,(将render.html文件用Vscode打开,自己调整好位置后,点击左上角的save Config获得chart_config (4).json文件)
#用Page组图
from pyecharts import options as opts
from pyecharts.charts import Bar, Line, Page, Pie
# 创建一个页面对象,设置页面布局为可拖动的布局
page = Page(layout=Page.DraggablePageLayout)
# 将各个时间轴对象添加到页面中
page.add(
timeline,
timeline1,
timeline2,
timeline3,
timeline4,
)
# 渲染整个页面
page.render()
# 保存页面为可调整大小的HTML文件,使用指定的配置文件 'chart_config (4).json'
Page.save_resize_html('render.html',cfg_file = 'chart_config (4).json')
用Grid绘制大屏