1.构建折线图,并进行一定的全局配置
#绘制折线图
import pyecharts as pp
line=pp.charts.Line()
line.add_xaxis(["中国","美国","日本"])
line.add_yaxis("GDP",[30,40,20])
#全局设置
line.set_global_opts(title_opts = pp.options.TitleOpts(title="GDP展示",pos_left="center",pos_bottom="1%"))
#post_bottom代表距离底部的距离
line.render()
#注意图片生成的是一个文件,需要通过浏览器去打开
TitleOpts是一个类,title_opts参数需要传进去的是一个对象。
2.使用json模块对数据进行处理,并绘制疫情患者人数走势图
网站:ab173.com 可以用来查看josn数据的格式,复制json数据的时候需要符合格式。
代码:
#绘制折线图
import pyecharts as pp
import json
f=open("日本.txt","r",encoding="UTF-8")
#注意文件的名称需要加上后缀
jp=f.read() #获取内容
#去掉文件开头与结尾不符合规范的东西
jp=jp.replace("jsonp_1629350871167_29498(","")
jp=jp[:-2]
print(jp)
jp_data=json.loads(jp)
jp_data=jp_data["data"][0]["trend"] #0是因为列表
x=jp_data["updateDate"][:314]
y=jp_data["list"][0]["data"][:314]
print(x)
print(y)
line=pp.charts.Line()
line.add_xaxis(x)
line.add_yaxis("患者人数",y)
line.render()
f.close()
结果
3.全国地图可视化
基础部分:
代码如下:
#地图的基本演示
import pyecharts
map=pyecharts.charts.Map()
data=[("陕西省",999)]
map.add("地图",data,"china")
#只有调用其他包里面的方法文件时才需要.
#自己设置颜色的映射关系
map.set_global_opts(
#这是一个类
visualmap_opts=pyecharts.options.VisualMapOpts(
is_show=True,
is_piecewise=True, #允许自定义
pieces=[{"min":1,"max":1000,"label":"1-1000","color":"#CCFFFF"}]
)
)
map.render()
注意:1.data=[()]的时候可以什么都不添加先生成查看具体省份的名字,需要确保传进去的省份名字字与图中的名字完全一致。
2.有关颜色的代码可以前往ab173.com查看。(前端——>WEB安全色)
全国疫情地图的构建:
代码如下:
import json
import pyecharts
#读取数据文件
f=open("疫情.txt",'r',encoding="UTF-8")
data=f.read()
f.close()
#将数据转换为字典
data_dict=json.loads(data)
data1=data_dict["areaTree"][0]["children"]
End=[]
for p in data1:
name=p["name"]+"省" #需要与图一一对应,有些地方应改为区
data2=p["total"]["confirm"]
End.append((name,data2))
print(End)
map=pyecharts.charts.Map()
map.add("人数",End,"china") #图例的标题
map.set_global_opts(
title_opts=pyecharts.options.TitleOpts(title="全国疫情地图"),#总体的标题
visualmap_opts=pyecharts.options.VisualMapOpts(
is_show=True, #视觉映射过渡类型
is_piecewise=True,
pieces=[
{"min": 1, "max": 99, "lable": "1-99", "color": "#CCFFFF"},
{"min": 100, "max": 999, "lable": "1-99", "color": "#FFFF99"},
{"min": 1000, "max": 4999, "lable": "1-99", "color": "#FF9966"},
{"min": 5000, "max": 9999, "lable": "1-99", "color": "#FF6666"},
{"min": 10000, "max": 99999, "lable": "1-99", "color": "#CC3333"},
{"min": 100000, "lable": "100000+", "color": "#990033"},
]
)
)
map.render()
具体颜色可以根据自己想要的进行设置。
4.柱状图与动态GDP图的绘制
基础部分
import json
import pyecharts
bar=pyecharts.charts.Bar()
bar.add_xaxis(["中国","美国","日本"])
#调用对应方法将数据添加到右侧
bar.add_yaxis("GDP",[30,40,20],label_opts=pyecharts.options.LabelOpts(position="right"))
bar.reversal_axis()#反转x与y
bar.render()
基础时间柱状图
import json
import pyecharts
bar1=pyecharts.charts.Bar()
bar1.add_xaxis(["中国","美国","日本"])
bar1.add_yaxis("GDP",[30,40,20],label_opts=pyecharts.options.LabelOpts(position="right"))
bar1.reversal_axis()#反转x与y
bar2=pyecharts.charts.Bar()
bar2.add_xaxis(["中国","美国","日本"])
bar2.add_yaxis("GDP",[50,20,100],label_opts=pyecharts.options.LabelOpts(position="right"))
bar2.reversal_axis()#反转x与y
bar3=pyecharts.charts.Bar()
bar3.add_xaxis(["中国","美国","日本"])
bar3.add_yaxis("GDP",[70,60,120],label_opts=pyecharts.options.LabelOpts(position="right"))
bar3.reversal_axis()#反转x与y
#构建对象
time1=pyecharts.charts.Timeline(
{"theme":pyecharts.globals.ThemeType.LIGHT}#使用字典来设置主题
)
#添加对应点
time1.add(bar1,"1999")
time1.add(bar2,"2999")
time1.add(bar3,"3999")
#绘图,用时间线对象绘图
time1.render("绘图.html")
#自动播放
time1.add_schema(
play_interval=1000,
is_timeline_show=True,
is_auto_play=True,
is_loop_play=True,
)
动态图标的绘制
import pyecharts
#数据处理
f=open("1960-2019全球GDP数据.csv","r",encoding="GB2312")
data_lines=f.readlines()
f.close()
#删除第一条数据
data_lines.pop(0)
#转换格式,分割年份
data_dict={}
for line in data_lines:
year=int(line.split(",")[0])
country=line.split(",")[1]
GDP=float(line.split(",")[2]) #强制转换有科学计数法
#如果已经存在,直接添加,如果未存在,先创建再添加,也可以通过异常捕获的方式
if year in data_dict.keys():
data_dict[year].append([country,GDP])
else:
data_dict[year]=[[country,GDP]]
#排序年份
sort_year=sorted(data_dict.keys())
print(sort_year)
time1=pyecharts.charts.Timeline(
{"theme":pyecharts.globals.ThemeType.LIGHT}#使用字典来设置主题
)
def cmp(a):
return a[1]
for year in sort_year:
data_dict[year].sort(key=cmp,reverse=True)
#取前8
year_data=data_dict[year][0:8]
x=[];y=[]
for i in year_data:
x.append(i[0])
y.append(i[1]/100000000)
#构建
x.reverse()
y.reverse()
bar1 = pyecharts.charts.Bar()
bar1.add_xaxis(x)
bar1.add_yaxis("GDP(亿)", y, label_opts=pyecharts.options.LabelOpts(position="right"))
bar1.reversal_axis() # 反转x与y
#表头
bar1.set_global_opts(
title_opts=pyecharts.options.TitleOpts(title=f"{year}年gdp")
)
time1.add(bar1,str(year))
time1.add_schema(
play_interval=1000,
is_timeline_show=True,
is_auto_play=True,
is_loop_play=True,
)
#绘图,用时间线对象绘图
time1.render("绘图.html")