地图
from pyecharts.charts import Map
map=Map()
data=[
('北京市',999),
('上海市',133),
('河南省',123),
('西藏自治区',123),
('吉林省',12),
]
map.add("地图",data,"china")
map.render()
示例
from pyecharts.charts import Map
from pyecharts.options import VisualMapOpts
map=Map()
data=[
('北京市',999),
('上海市',133),
('河南省',123),
('西藏自治区',123),
('吉林省',12),
]
map.add("地图",data,"china")
map. set_global_opts(
visualmap_opts=VisualMapOpts(
is_show=True,
is_piecewise=True,
pieces=[
{"min" : 1,"max": 9, "label": "1-9人","color": "#CCFFFF"},
{ "min": 10,"max" : 99,"label" : "10-99人","color":"#FFFF99"},
{ "min": 100,"max": 499,"label": "99-499人","color" :"#FF9966"},
{"min": 500,"max": 999,"label": "499-999人","color":"#FF6666"},
{ "min": 1000,"max": 9999,"label": "1000-9999人","color":"#CC3333"},
{ "min" : 10000,"label" : "10000以上","color" :"#990033"}
])
)
map.render()
RGB颜色对照表获取
全国疫情地图
import json
from pyecharts.charts import Map
from pyecharts.options import VisualMapOpts
map=Map()
data1=open("E:/桌面文件/疫情.txt",'r',encoding="UTF-8")
data2=data1.read()
JSON_data=json.loads(data2)
province=JSON_data["areaTree"][0]["children"]
data=[]
for i_data in province:
province_name=i_data["name"]
if province_name[-1] != "省":
province_name = province_name + "省"
print(province_name)
province_num=i_data["total"]["confirm"]
yuan_data=(province_name,province_num)
data.append(yuan_data)
print(data)
map.add("全国疫情地图",data,"china")
map. set_global_opts(
visualmap_opts=VisualMapOpts(
is_show=True,#是否显示
is_piecewise=True,#是否分段
pieces=[
{"min" : 1,"max": 9, "label": "1-9人","color": "#CCFFFF"},
{ "min": 10,"max" : 99,"label" : "10-99人","color":"#FFFF99"},
{ "min": 100,"max": 499,"label": "99-499人","color" :"#FF9966"},
{"min": 500,"max": 999,"label": "499-999人","color":"#FF6666"},
{ "min": 1000,"max": 9999,"label": "1000-9999人","color":"#CC3333"},
{ "min" : 10000,"label" : "10000以上","color" :"#990033"}
])
)
map.render()
河南省疫情地图
import json
from pyecharts.charts import Map
from pyecharts.options import VisualMapOpts
map=Map()
data1=open("E:/桌面文件/疫情.txt",'r',encoding="UTF-8")
data2=data1.read()
JSON_data=json.loads(data2)
province=JSON_data["areaTree"][0]["children"][3]["children"]
data=[]
for i_data in province:
province_name=i_data["name"]
province_name = province_name + "市"
print(province_name)
province_num=i_data["total"]["confirm"]
yuan_data=(province_name,province_num)
data.append(yuan_data)
print(data)
map.add("河南省疫情地图",data,"河南")
map. set_global_opts(
visualmap_opts=VisualMapOpts(
is_show=True,#是否显示
is_piecewise=True,#是否分段
pieces=[
{"min" : 1,"max": 9, "label": "1-9人","color": "#CCFFFF"},
{ "min": 10,"max" : 99,"label" : "10-99人","color":"#FFFF99"},
{ "min": 100,"max": 499,"label": "99-499人","color" :"#FF9966"},
{"min": 500,"max": 999,"label": "499-999人","color":"#FF6666"},
{ "min": 1000,"max": 9999,"label": "1000-9999人","color":"#CC3333"},
{ "min" : 10000,"label" : "10000以上","color" :"#990033"}
])
)
map.render()
柱状图
from pyecharts.charts import Bar
from pyecharts.options import *
#构建柱状图对象
bar = Bar()
#添加x轴数据
bar.add_xaxis(["中国","美国","英国"])
#添加y轴数据
bar.add_yaxis ("GDP",[30,20,10])
#绘图
bar.render("基础柱状图.html" )
XY轴反转
bar.reversal_axis()
小结
时间线柱状图
#创建时间线
from pyecharts.charts import Bar,Timeline
from pyecharts.options import *
bar1= Bar()
bar1.add_xaxis(["中国","美国","英国"])
bar1.add_yaxis(" GDP",[30,20,10],label_opts=LabelOpts(position="right"))
bar1.reversal_axis()
bar2=Bar()
bar2.add_xaxis(["中国","美国","英国"])
bar2.add_yaxis("GDP",[50,30,20],label_opts=LabelOpts(position="right"))
bar2.reversal_axis()
#创建时间线对象
timeline = Timeline()#timeline对象添加bar柱状图
timeline.add(bar1,"2021年GDP")
timeline.add(bar2,"2022年GDP")
#通过时间线绘图
timeline.render("基础柱状图-时间线.html")
自动播放
#设置自动播放
timeline.add_schema(
play_interval=1000,
#自动播放的时间间隔,单位毫秒
is_timeline_show=True,
#是否在自动播放的时候,显示时间线
is_auto_play=True,
#是否自动播放
is_loop_play=True,
#是否循环自动播放
)
#创建时间线
from pyecharts.charts import Bar,Timeline
from pyecharts.options import *
from pyecharts.globals import ThemeType
bar1= Bar()
bar1.add_xaxis(["中国","美国","英国"])
bar1.add_yaxis(" GDP",[30,20,10],label_opts=LabelOpts(position="right"))
bar1.reversal_axis()
bar2=Bar()
bar2.add_xaxis(["中国","美国","英国"])
bar2.add_yaxis("GDP",[50,30,20],label_opts=LabelOpts(position="right"))
bar2.reversal_axis()
#创建时间线对象
timeline = Timeline({"theme":ThemeType.LIGHT})#timeline对象添加bar柱状图
timeline.add(bar1,"2021年GDP")
timeline.add(bar2,"2022年GDP")
#设置自动播放
timeline.add_schema(
play_interval=1000,
#自动播放的时间间隔,单位毫秒
is_timeline_show=True,
#是否在自动播放的时候,显示时间线
is_auto_play=True,
#是否自动播放
is_loop_play=True,
#是否循环自动播放
)
#通过时间线绘图
timeline.render("基础柱状图-时间线.html")
小结
动态柱状图
sort排序
my_list=[['a',33],['b',90],['c',12]]
def choose_sort_key(element):
return element[1]
my_list.sort(key=choose_sort_key,reverse=True)
print(my_list)
练习动态柱状图
from pyecharts.charts import Bar, Timeline
from pyecharts.options import *
from pyecharts.globals import ThemeType
def sort_dta(element):
return element[1]
f=open("E:/桌面文件/1960-2019全球GDP数据.csv","r",encoding="GB2312")
data=f.readlines()
f.close()
data.pop(0)
data_culi=[]
data_dirc={}
for element in data:
data_culi.append(element.strip())
for j in data_culi:
year=int(j.split(",")[0])
country=j.split(",")[1]
GDP=float(j.split(",")[2])
try:
data_dirc[year].append([country,GDP])
except KeyError:
data_dirc[year]=[]
data_dirc[year].append([country, GDP])
list_year_key=sorted(data_dirc.keys())
timeline = Timeline({"theme": ThemeType.LIGHT}) # timeline对象添加bar柱状图
for element2 in list_year_key:
data_dirc[element2].sort(key=sort_dta, reverse=True)
year_data=data_dirc[element2][0:8:1]
print(year_data)
x_data = []
y_data = []
for i in year_data:
x_data.append(i[0])
y_data.append(i[1]/100000000)
x_data.reverse()
y_data.reverse()
bar = Bar()
bar.add_xaxis(x_data)
bar.add_yaxis(" GDP(亿)", y_data, label_opts=LabelOpts(position="right"))
bar.reversal_axis()
# 创建时间线对象
bar.set_global_opts(
title_opts=TitleOpts(
title=f"{element2}全球前8GDP数据"
)
)
timeline.add(bar, str(element2))
timeline.add_schema(
play_interval=500,
#自动播放的时间间隔,单位毫秒
is_timeline_show=True,
#是否在自动播放的时候,显示时间线
is_auto_play=True,
#是否自动播放
is_loop_play=True,
#是否循环自动播放
)
#通过时间线绘图
timeline.render("GDP时间线.html")