<span>Python——pyecharts数据可视化</span>
关于项目
pyecharts 是一个用于生成 Echarts 图表的类库。Echarts 是百度开源的一个数据可视化 JS 库。用 Echarts 生成的图可视化效果非常棒,为了与 Python 进行对接,方便在 Python 中直接使用数据生成图,这个项目诞生了。
预热准备
进入命令行,安装pyecharts第三方库
Windows系统:
pip install pyecharts_jupyter_installer
pip install pyecharts
Mac系统:
pip3 install pyecharts_jupyter_installer
pip3 install pyecharts
一、图表绘制流程
- chart_name = 图表类型() 初始化具体类型图表。
- chart_name.add() 添加数据及配置项。
- chart_name.render() 生成 .html 文件。
新建一个pyecharts文件
- 导入pyecharts模块
Import pyecharts as pec
- 创建一个Bar图(柱状图/条形图)
bar = pec.Bar('第一个图表','这里是副标题',)
- 添加数据,add(名称, X轴数据, Y轴数据), xaxis_rotate=旋转角度(-90~90之间)
bar =add(“服装”, attr = ['衬衫','羊毛衫','雪纺衫','裤子','袜子'] ,[5,20,36,42,41], xaxis_rotate=30)
- 渲染bar图
bar.render()
- 生成如图
二、柱状图数据堆叠
Import pyecharts as pec
1、 传入两组数据,is_stack=True表示两组数据堆叠在一起
attr = ['衬衫','羊毛衫','雪纺衫','裤子','袜子']
v1 = [5,20,36,42,41]
v2 = [15,27,6,12,25]
bar = pec.Bar('柱状图表','副标题',width=800,height=800)
bar.add('服装A',attr ,v1,is_stack = True)
bar.add('服装B',attr ,v2,is_stack = True)
bar.render()
2、 柱状图数据堆叠
3、 柱状图标记点和标记线
mark_point 标记点(填一个列表) 默认有’min’, ‘max’, ‘average’可选
自定义标记点使用如下
[{“coord”: [a1, b1], “name”: “first markpoint”}, {“coord”: [a2, b2],
“name”: “second markpoint”}] mark_point_symbol 标记点形状
默认为’pin’
可选’circle’, ‘rect’, ‘roundRect’, ‘triangle’, ‘diamond’, ‘pin’, ‘arrow’
mark_line 标记线(填一个列表) 默认有’min’, ‘max’, 'average’可选
代码如下:
bar = pec.Bar('柱状图','标记线和标点',width=800,height=800)
bar.add('服装A',attr ,v1,is_stack = True, mark_line=["average"])
bar.add('服装B',attr ,v2,is_stack = True, mark_point=["min","max"])
4、 显示如图
5、 柱状图X轴和Y轴交换
is_convert=True注意放在最后一个add里
bar = pec.Bar('柱状图表','x轴和y轴交换',width=800,height=800)
bar.add('服装A',attr ,v1)
bar.add('服装B',attr ,v2,is_convert = True)
6、 显示如图
7、 柱状图带窗口滑块,添加参数is_datazoom_show=True
具体代码:
import pyecharts as pec
import random
attr = ['{}年'.format(i) for i in range(40)]
v3 = [random.randint(4,10) for _ in range(40)]
bar = pec.Bar('柱状图表','窗口滑块效果图',width=800,height=800)
bar.add("",attr,v3,is_label_show=True,is_datazoom_show=True)
bar.render()
8、 具体如图
三、具体实例
1、 3D柱状图
在Bar3D中添加参数width和height控制图表大小
具体代码:
import pyecharts as pec
import random
bar3D = pec.Bar3D('变化3D图表','窗口滑块效果图',width=1200,height=600)
#创建X轴和Y轴标签
x_axis = ["Saturday","Friday","Thursday","Wednesday","Tuesday","Monday","Sunday"
]
# "12a","1a","2a","3a","4a","5a","6a","7a","8a",
y_axis = ["25p","1p","2p","3p","4p","5p","6p","7p","8p","9p","10p",
"11p","12p","13p","14p","15p","16p","17p","18p","19p","20p",
"21p","22p","23p","24p"]
#创建数据
#数据格式: 一个列表,每一项都是一个三个元素组成的列表 [X坐标, Y坐标, Z轴高度]
data = [
[0, 0, 5], [0, 1, 1], [0, 2, 0], [0, 3, 0], [0, 4, 0], [0, 5, 0],
[0, 6, 0], [0, 7, 0], [0, 8, 0], [0, 9, 0], [0, 10, 0], [0, 11, 2],
[0, 12, 4], [0, 13, 1], [0, 14, 1], [0, 15, 3], [0, 16, 4], [0, 17, 6],
[0, 18, 4], [0, 19, 4], [0, 20, 3], [0, 21, 3], [0, 22, 2], [0, 23, 5],
[1, 0, 7], [1, 1, 0], [1, 2, 0], [1, 3, 0], [1, 4, 0], [1, 5, 0],
[1, 6, 0], [1, 7, 0], [1, 8, 0], [1, 9, 0], [1, 10, 5], [1, 11, 2],
[1, 12, 2], [1, 13, 6], [1, 14, 9], [1, 15, 11], [1, 16, 6], [1, 17, 7],
[1, 18, 8], [1, 19, 12], [1, 20, 5], [1, 21, 5], [1, 22, 7], [1, 23, 2],
[2, 0, 1], [2, 1, 1], [2, 2, 0], [2, 3, 0], [2, 4, 0], [2, 5, 0],
[2, 6, 0], [2, 7, 0], [2, 8, 0], [2, 9, 0], [2, 10, 3], [2, 11, 2],
[2, 12, 1], [2, 13, 9], [2, 14, 8], [2, 15, 10], [2, 16, 6], [2, 17, 5],
[2, 18, 5], [2, 19, 5], [2, 20, 7], [2, 21, 4], [2, 22, 2], [2, 23, 4],
[3, 0, 7], [3, 1, 3], [3, 2, 0], [3, 3, 0], [3, 4, 0], [3, 5, 0],
[3, 6, 0], [3, 7, 0], [3, 8, 1], [3, 9, 0], [3, 10, 5], [3, 11, 4],
[3, 12, 7], [3, 13, 14], [3, 14, 13], [3, 15, 12], [3, 16, 9], [3, 17, 5],
[3, 18, 5], [3, 19, 10], [3, 20, 6], [3, 21, 4], [3, 22, 4], [3, 23, 1],
[4, 0, 1], [4, 1, 3], [4, 2, 0], [4, 3, 0], [4, 4, 0], [4, 5, 1],
[4, 6, 0], [4, 7, 0], [4, 8, 0], [4, 9, 2], [4, 10, 4], [4, 11, 4],
[4, 12, 2], [4, 13, 4], [4, 14, 4], [4, 15, 14], [4, 16, 12], [4, 17, 1],
[4, 18, 8], [4, 19, 5], [4, 20, 3], [4, 21, 7], [4, 22, 3], [4, 23, 0],
[5, 0, 2], [5, 1, 1], [5, 2, 0], [5, 3, 3], [5, 4, 0], [5, 5, 0],
[5, 6, 0], [5, 7, 0], [5, 8, 2], [5, 9, 0], [5, 10, 4], [5, 11, 1],
[5, 12, 5], [5, 13, 10], [5, 14, 5], [5, 15, 7], [5, 16, 11], [5, 17, 6],
[5, 18, 0], [5, 19, 5], [5, 20, 3], [5, 21, 4], [5, 22, 2], [5, 23, 0],
[6, 0, 1], [6, 1, 0], [6, 2, 0], [6, 3, 0], [6, 4, 0], [6, 5, 0],
[6, 6, 0], [6, 7, 0], [6, 8, 0], [6, 9, 0], [6, 10, 1], [6, 11, 0],
[6, 12, 2], [6, 13, 1], [6, 14, 3], [6, 15, 4], [6, 16, 0], [6, 17, 0],
[6, 18, 0], [6, 19, 0], [6, 20, 1], [6, 21, 2], [6, 22, 2], [6, 23, 6]]
#创建自定义的过渡颜色
range_color = ["#c23531","#2f4554","#61a0a8","#d48265","#749f83","#ca8622","#bda29a","#6e7074",
"#546570","#c4ccd3","#f05b72","#ef5b9c","#f47920","#905a3d","#fab27b","#2a5caa",
"#444693","#726930","#b2d235","#6d8346","#ac6767","#1d953f","#6950a1","#918597","#f6f5ec"]
#添加图表并渲染
#is_visualmap: 是否使用视觉映射组件
#visual_range: 允许的最小值与最大值
#visual_range_color: 自定义的过渡颜色
#grid3d_width: 图表格子X轴方向宽度
#grid3d_depth: 图表格子深度
bar3D.add('A城',x_axis,y_axis,data,visual_range_color=range_color,grid3d_width=200,grid3d_depth=90,is_visualmap=True,visual_range=[0,40])#添加平均值的线
bar3D.render()
生成图形:
2、 折线图生成
is_smooth: 是否让折线变平滑
具体代码:
import pyecharts as pec
attr = ['衬衫','羊毛衫','雪纺衫','裤子','袜子']
v1 = [5,20,36,42,41]
v2 = [15,27,6,12,25]
line = pec.Line("折线图示例")
line.add("商家A",attr ,v1,mark_point=["average"])
line.add("商家B",attr ,v2,is_smooth=True,mark_line=["max","average"])
line.render()
生成图像:
3、 阴影折线图生成
is_fill: 是否填充颜***r> line_opacity: 折线透明度
area_opacity: 填充区域透明度
area_color: 填充区域颜***r> symbol: 标记点类型,有’circle’, ‘rect’, ‘roundRect’, ‘triangle’, ‘diamond’, ‘pin’, 'arrow’可选
具体代码:
import pyecharts as pec
attr = ['衬衫','羊毛衫','雪纺衫','裤子','袜子']
v1 = [5,20,36,42,41]
v2 = [15,27,6,12,25]
line = pec.Line("折线图示例")
line.add("商家A",attr ,v1,is_fill=True,line_opacity=0.2,
area_opacity=0.4,symbol=None)
line.add('商家B',attr ,v2,is_fill=True,area_color='#000',area_opacity=0.3,
is_stack = True,is_smooth=True)#显示最值,折线变平滑
line.render()
生成图像:
4、 饼图
具体代码:
import pyecharts as pec
attr = ['衬衫','羊毛衫','雪纺衫','裤子','袜子']
v1 = [15,20,36,32,37]
pie = pec.Pie("饼图示例")
pie.add("",attr,v1,is_lable_show=True)
pie.render()
生成图像:
5、 圆环饼图
label_text_color: 标签颜色,设置为None则和每块区域颜色相同
radius: 内环和外环半径,列表类型
具体代码:
Import pyecharts from pec
attr = ['衬衫','羊毛衫','雪纺衫','裤子','袜子',"T恤","外衣"]
v1 = [15,20,36,32,37,13,18]
pie = pec.Pie("环形-饼图示例",title_pos="center")
pie.add("",attr,v1,radius=[40,75],label_text_color=None,is_lable_show=True,legend_orient='vertical',legend_pos='left')
pie.render()
生成图像:
6、 玫瑰饼图
is_random: 是否随机设置颜***r> rosetype: 内环和外环半径,列表类型
is_legend_show: 是否显示图例
具体代码:
Import pyecharts from pec
import random
attr = ['衬衫','羊毛衫','雪纺衫','裤子','袜子',"T恤","外衣"]
v1 = [15,20,36,32,37,13,18]
pie = pec.Pie("环形-饼图示例","subtitle")
pie.add("商品B",attr,v1,radius=[30,75],is_random=True,
is_label_show=True,is_legend_show=True,rosetype='area')
pie.render()
生成图像:
7、 带有涟漪特效动画的散点图
symbol_size: 散点大小
effect_scale: 动态波纹最大缩放比
effect_period: 动画持续时间
symbol: 散点图形
effect_brushtype: 波纹绘制方式’stroke’, ‘fill’
具体代码:
Import pyecharts from pec
v1 = [15,20,36,32,37,13,18]
v2 = [22,20,15,10,60,33,50]
es = pec.EffectScatter("动态图示例")
es.add("effectScatter",v1,v2)
es.render()
生成图像:
#带涟漪的散点图2
Import pyecharts from pec
es = pec.EffectScatter("动态散点图的各种图形示例")
es.add("",[10],[10],symbol_size=20,effect_scale=3.5,effect_period=3,symbol="pin")
es.add("",[20],[20],symbol_size=12,effect_scale=4.5,effect_period=4,symbol="rect")
es.add("",[30],[30],symbol_size=30,effect_scale=5.5,effect_period=5,symbol="roundRect")
es.add("",[40],[40],symbol_size=10,effect_scale=6.5,effect_period="fill",symbol="diamond")
es.add("",[50],[50],symbol_size=16,effect_scale=5.5,effect_period=3,symbol="arrow")
es.add("",[60],[60],symbol_size=6,effect_scale=2.5,effect_period=3,symbol="triangle")
es.render()
8、 水球图
is_liquid_outline_show: 是否显示外边框
is_liquid_animation: 是否显示动画效果
shape: 边框形状,和symbol可用的形状类似,还可以自定义
具体代码:
Import pyecharts from pec
shape = ("path://M353.987 348.949c0 0 173.124 89.875 319.294 55.941 0 0 295.345-94.649 283.413 243.47 0 0-55.056 399.715-580.615 263.354 0 0-394.589-137.597-292.253-499.754 0 0 30.223-73.793 112.5-71.318 0 0-109.584-115.063-10.693-221.818 52.67-57.089 356.411-76.444 346.602 201.58 0 0-251.511-10.693-332.815-163.669 0 0-51.434 30.754 35.527 182.934 0.089 0.177 45.865-13.61 119.039 9.28z")
liquid = pec.Liquid("水球图示例")
liquid.add("Liquid",[0.6,0.5,0.4,0.3],is_liquid_outline_show=False,shape=shape)
liquid.render()
生成如图:
9、 漏斗图
is_label_show: 是否显示标签 label_pos: 标签显示位置’outside’, ‘inside’
legend_orient: 图例方向 legend_pos : 图例位置
具体代码:
Import pyecharts from pec
attr = ['浏览人数','报名人数','听课人数','下单人数','支付成功人数']
value = [4000,2000,900,100,60]
funnel =pec.Funnel("用户转化漏斗图示例",title_pos='center')
funnel.add("各环节人数",attr,value,is_label_show=True,label_pos="outside",legend_orient='vertical',legend_pos='left')
funnel.render()
生成图像:
10、 词云图
word_size_range: 单词字体大小范围
word_gap: 单词间隔
shape: 词云图轮廓,有’circle’, 'cardioid’, ‘diamond’,'triangle-forward’, ‘triangle’, ‘pentagon’, 'star’可选
具体代码:
Import pyecharts from pec
name=['气凌霄汉','头昏脑眩','毫无疑义','陌上桑间','金革之难','欲说还休','安枕而卧','礼先一饭','一面之荣','舍近求远','触类而通','风烛残年','不逢不若','中馈之思','韦弦之佩','不瘟不火','裂土分茅','德言容功','一技之微','日月其除','朋党比周','赤绳绾足','铮铮铁汉','琼楼金阙','出于意外','不为戎首']
value = ['200','412','160','520','121','336','480','1000','361','562','63','24','264','451','666','120','452','325','479','965','765','258','23','521','9','4545']
worldcloud = pec.WordCloud("动态图示例",width=1300,height=620)
worldcloud.add("WordCloud",name,value,word_size_range=[20,100])
worldcloud.render()
生成图像:
11、 雷达图
列表形式存储 每个元素含两个数据,一个是名称,一个是100%对应的数值
is_splitline: 是否显示雷达的分割线
is_axisline_show: 是否显示坐标轴线
legend_selectedmode:图例选择的模式,默认多选可设置位single即单选
具体代码:
import pyecharts as pec
schema = [
("销售",5000),("管理",16000),("信息技术",30000),
("客服",38000),("研发",52000),("市场",25000)
]
v1 = [
[4300,10000,25000,35000,50000,19000],
[3300,13000,25000,30000,48000,24000]
]
v2 = [[5000,14000,28000,31000,42000,42000,21000]]
radar = pec.Radar()
radar.config(schema)
radar.add("预算分配",v1,is_splitline=True,is_axisline_show=True)
radar.add("实际开销",v2,label_color=["#4e79a7"],is_area_show=False,legend_selectedmode='single')
radar.render()
生成图像:
12、 Grid图
grid_name = pec.Grid() • 实例化Grid类,可以指定参数。
2. grid_name.add(chart, grid_top=’60%’) • 向Grid中添加图表。 • 至少需要设置一个 grid_top, grid_bottom, grid_left, grid_right 四个参数中的一个
3. grid_name.render() • 生成 .html 文件。
4. add()中参数 add(chart, grid_width=None, grid_height=None, grid_top=None, grid_bottom=None, grid_left=None, grid_right=None)
具体代码:
Import pyecharts from pec
from pyecharts import Page
page = Page()
attr = ['衬衫','羊毛衫','雪纺衫','裤子','袜子']
v1 = [5,20,36,42,41]
v2 = [15,27,6,12,25]
bar = pec.Bar('柱状图表',height=720)
bar.add('服装A',attr ,v1,is_stack = True)
bar.add('服装B',attr ,v2,is_stack = True)
page.add(bar)
line = pec.Line("折线图示例",)
attr = ['周一','周二','周三','周四','周五',"周六","周日"]
line.add("最高气温",attr ,[11,11,15,13,12,13,10],mark_point=["max","min"],mark_line=["average"])
line.add("最低气温",attr ,[1,-2,2,3,5,2,0],mark_point=["max","min"],mark_line=["average"],legend_top="50%")
page.add(line)
page.render()
生成图像:
13、 地理坐标图
安装地图文件
Windows系统:
pip install echarts-countries-pypkg
pip install echarts-china-provinces-pypkg
pip install echarts-china-cities-pypkg
Mac系统:
pip3 install echarts-countries-pypkg
pip3 install echarts-china-provinces-pypkg
pip3 install echarts-china-cities-pypkg
background_color: 图表背景颜***r> type: 显示类型"heatmap“, “effectScatter”
具体代码:
import pyecharts as pec
data = [
("海门", 9), ("鄂尔多斯", 12), ("招远", 12), ("舟山", 12),
("齐齐哈尔", 14), ("盐城", 15), ("赤峰", 16), ("青岛", 18),
("乳山", 18), ("金昌", 19), ("泉州", 21), ("莱西", 21),
("日照", 21), ("胶南", 22), ("南通", 23), ("拉萨", 24),
("云浮", 24), ("梅州", 25), ("文登", 25), ("上海", 25),
("攀枝花", 25), ("威海", 25), ("承德", 25), ("厦门", 26),
("汕尾", 26), ("潮州", 26), ("丹东", 27), ("太仓", 27),
("曲靖", 27), ("烟台", 28), ("福州", 29), ("瓦房店", 30),
("即墨", 30), ("抚顺", 31), ("玉溪", 31), ("张家口", 31),
("阳泉", 31), ("莱州", 32), ("湖州", 32), ("汕头", 32),
("昆山", 33), ("宁波", 33), ("湛江", 33), ("揭阳", 34),
("荣成", 34), ("连云港", 35), ("葫芦岛", 35), ("常熟", 36),
("东莞", 36), ("河源", 36), ("淮安", 36), ("泰州", 36),
("南宁", 37), ("营口", 37), ("惠州", 37), ("江阴", 37),
("蓬莱", 37), ("韶关", 38), ("嘉峪关", 38), ("广州", 38),
("延安", 38), ("太原", 39), ("清远", 39), ("中山", 39),
("昆明", 39), ("寿光", 40), ("盘锦", 40), ("长治", 41),
("深圳", 41), ("珠海", 42), ("宿迁", 43), ("咸阳", 43),
("铜川", 44), ("平度", 44), ("佛山", 44), ("海口", 44),
("江门", 45), ("章丘", 45), ("肇庆", 46), ("大连", 47),
("临汾", 47), ("吴江", 47), ("石嘴山", 49), ("沈阳", 50),
("苏州", 50), ("茂名", 50), ("嘉兴", 51), ("长春", 51),
("胶州", 52), ("银川", 52), ("张家港", 52), ("三门峡", 53),
("锦州", 54), ("南昌", 54), ("柳州", 54), ("三亚", 54),
("自贡", 56), ("吉林", 56), ("阳江", 57), ("泸州", 57),
("西宁", 57), ("宜宾", 58), ("呼和浩特", 58), ("成都", 58),
("大同", 58), ("镇江", 59), ("桂林", 59), ("张家界", 59),
("宜兴", 59), ("北海", 60), ("西安", 61), ("金坛", 62),
("东营", 62), ("牡丹江", 63), ("遵义", 63), ("绍兴", 63),
("扬州", 64), ("常州", 64), ("潍坊", 65), ("重庆", 66),
("台州", 67), ("南京", 67), ("滨州", 70), ("贵阳", 71),
("无锡", 71), ("本溪", 71), ("克拉玛依", 72), ("渭南", 72),
("马鞍山", 72), ("宝鸡", 72), ("焦作", 75), ("句容", 75),
("北京", 79), ("徐州", 79), ("衡水", 80), ("包头", 80),
("绵阳", 80), ("乌鲁木齐", 84), ("枣庄", 84), ("杭州", 84),
("淄博", 85), ("鞍山", 86), ("溧阳", 86), ("库尔勒", 86),
("安阳", 90), ("开封", 90), ("济南", 92), ("德阳", 93),
("温州", 95), ("九江", 96), ("邯郸", 98), ("临安", 99),
("兰州", 99), ("沧州", 100), ("临沂", 103), ("南充", 104),
("天津", 105), ("富阳", 106), ("泰安", 112), ("诸暨", 112),
("郑州", 113), ("哈尔滨", 114), ("聊城", 116), ("芜湖", 117),
("唐山", 119), ("平顶山", 119), ("邢台", 119), ("德州", 120),
("济宁", 120), ("荆州", 127), ("宜昌", 130), ("义乌", 132),
("丽水", 133), ("洛阳", 134), ("秦皇岛", 136), ("株洲", 143),
("石家庄", 147), ("莱芜", 148), ("常德", 152), ("保定", 153),
("湘潭", 154), ("金华", 157), ("岳阳", 169), ("长沙", 175),
("衢州", 177), ("廊坊", 193), ("菏泽", 194), ("合肥", 229),
("武汉", 273), ("大庆", 279)
]
geo = pec.Geo("全国主要城市空气质量",'data from pm2.5',title_color='#fff',
title_pos="center",width=1200,height=600,background_color="#404a59")
attr,value = geo.cast(data)
geo.add("",attr,value,visual_range=[0,200],visual_text_color="#fff",symbol_size=15,is_visualmap=True)
geo.render()
生成图像:
14、HeatMap 类型
具体代码:
import pyecharts as pec
data = [
("海门", 9),("鄂尔多斯", 12),("招远", 12),("舟山", 12),("齐齐哈尔", 14),("盐城", 15),
("赤峰", 16),("青岛", 18),("乳山", 18),("金昌", 19),("泉州", 21),("莱西", 21),
("日照", 21),("胶南", 22),("南通", 23),("拉萨", 24),("云浮", 24),("梅州", 25),
("文登", 25),("上海", 25),("攀枝花", 25),("威海", 25),("承德", 25),("厦门", 26),
("汕尾", 26),("潮州", 26),("丹东", 27),("太仓", 27),("曲靖", 27),("烟台", 28),
("福州", 29),("瓦房店", 30),("即墨", 30),("抚顺", 31),("玉溪", 31),("张家口", 31),
("阳泉", 31),("莱州", 32),("湖州", 32),("汕头", 32),("昆山", 33),("宁波", 33),
("湛江", 33),("揭阳", 34),("荣成", 34),("连云港", 35),("葫芦岛", 35),("常熟", 36),
("东莞", 36),("河源", 36),("淮安", 36),("泰州", 36),("南宁", 37),("营口", 37),
("惠州", 37),("江阴", 37),("蓬莱", 37),("韶关", 38),("嘉峪关", 38),("广州", 38),
("延安", 38),("太原", 39),("清远", 39),("中山", 39),("昆明", 39),("寿光", 40),
("盘锦", 40),("长治", 41),("深圳", 41),("珠海", 42),("宿迁", 43),("咸阳", 43),
("铜川", 44),("平度", 44),("佛山", 44),("海口", 44),("江门", 45),("章丘", 45),
("肇庆", 46),("大连", 47),("临汾", 47),("吴江", 47),("石嘴山", 49),("沈阳", 50),
("苏州", 50),("茂名", 50),("嘉兴", 51),("长春", 51),("胶州", 52),("银川", 52),
("张家港", 52),("三门峡", 53),("锦州", 54),("南昌", 54),("柳州", 54),("三亚", 54),
("自贡", 56),("吉林", 56),("阳江", 57),("泸州", 57),("西宁", 57),("宜宾", 58),
("呼和浩特", 58),("成都", 58),("大同", 58),("镇江", 59),("桂林", 59),("张家界", 59),
("宜兴", 59),("北海", 60),("西安", 61),("金坛", 62),("东营", 62),("牡丹江", 63),
("遵义", 63),("绍兴", 63),("扬州", 64),("常州", 64),("潍坊", 65),("重庆", 66),
("台州", 67),("南京", 67),("滨州", 70),("贵阳", 71),("无锡", 71),("本溪", 71),
("克拉玛依", 72),("渭南", 72),("马鞍山", 72),("宝鸡", 72),("焦作", 75),("句容", 75),
("北京", 79),("徐州", 79),("衡水", 80),("包头", 80),("绵阳", 80),("乌鲁木齐", 84),
("枣庄", 84),("杭州", 84),("淄博", 85),("鞍山", 86),("溧阳", 86),("库尔勒", 86),
("安阳", 90),("开封", 90),("济南", 92),("德阳", 93),("温州", 95),("九江", 96),
("邯郸", 98),("临安", 99),("兰州", 99),("沧州", 100),("临沂", 103),("南充", 104),
("天津", 105),("富阳", 106),("泰安", 112),("诸暨", 112),("郑州", 113),("哈尔滨", 114),
("聊城", 116),("芜湖", 117),("唐山", 119),("平顶山", 119),("邢台", 119),("德州", 120),
("济宁", 120),("荆州", 127),("宜昌", 130),("义乌", 132),("丽水", 133),("洛阳", 134),
("秦皇岛", 136),("株洲", 143),("石家庄", 147),("莱芜", 148),("常德", 152),("保定", 153),
("湘潭", 154),("金华", 157),("岳阳", 169),("长沙", 175),("衢州", 177),("廊坊", 193),
("菏泽", 194),("合肥", 229),("武汉", 273),("大庆", 279)]
geo = pec.Geo("全国主要城市空气质量", "data from pm2.5", title_color="#fff",
title_pos="center", width=1000,
height=500, background_color='#404a59')
attr, value = geo.cast(data)
geo.add("", attr, value, type="heatmap", is_visualmap=True, visual_range=[0, 300],
visual_text_color='#fff')
geo.render()
生成图像: