这篇文章主要介绍了python使用pyecharts库画地图数据可视化的实现
python使用pyecharts库画地图数据可视化导库中国地图代码结果世界地图代码结果省级地图代码结果地级市地图代码结果
导库
from pyecharts import options as opts
from pyecharts.charts import Map
中国地图代码
data = [('湖北', 9074),('浙江', 661),('广东', 632),('河南', 493),('湖南', 463),
('安徽', 340),('江西', 333),('重庆', 275),('江苏', 236),('四川', 231),
('山东', 230),('北京', 191),('上海', 182),('福建', 159),('陕西', 116),
('广西', 111),('云南', 105),('河北', 104),('黑龙江', 95),('辽宁', 69),
('海南', 64),('新疆', 21),('内蒙古', 21),('宁夏', 28),('青海', 11),('甘肃', 40),('西藏', 1),
('贵州', 38),('山西', 56),('吉林', 23),('台湾', 10),('天津', 48),('香港', 14),('澳门', 8)]
def map_china() -> Map:
c = (
Map()
.add(series_name="确诊病例", data_pair=data, maptype="china",zoom = 1,center=[105,38])
.set_global_opts(
title_opts=opts.TitleOpts(title="疫情地图"),
visualmap_opts=opts.VisualMapOpts(max_=9999,is_piecewise=True,
pieces=[{"max": 9, "min": 0, "label": "0-9","color":"#FFE4E1"},
{"max": 99, "min": 10, "label": "10-99","color":"#FF7F50"},
{"max": 499, "min": 100, "label": "100-499","color":"#F08080"},
{"max": 999, "min": 500, "label": "500-999","color":"#CD5C5C"},
{"max": 9999, "min": 1000, "label": ">=1000", "color":"#8B0000"}]
)
)
)
return c
d_map = map_china()
d_map.render_notebook()
d_map.render("china_map.html")
结果
世界地图代码
data = [['China', 14489],['Japan', 20],['Thailand', 19],['Singapore', 18],['Korea', 15],
['Australia', 12],['Germany', 10],['Malaysia', 8],['United States', 8],['Vietnam', 7],['France', 6],
['United Arab Emirates', 5],['Canada', 4],['Italy', 2],['India', 2],
['United Kingdom', 2],['Philippines', 2],['Russia', 2],['Sri Lanka', 1],['Cambodia', 1],
['Nepal', 1],['Sweden', 1],['Finland', 1],['Spain', 1]]
def map_world() -> Map:
c = (
Map()
.add("确诊病例", data, maptype="world",zoom = 1)
.set_series_opts(label_opts=opts.LabelOpts(is_show=False))
.set_global_opts(
title_opts=opts.TitleOpts(title="疫情地图"),
visualmap_opts=opts.VisualMapOpts(max_=100,is_piecewise=False),
)
)
return c
d_map = map_world()
d_map.render_notebook()
d_map.render("countries_map.html")
结果
省级地图代码
data = [['昆明市', 31],['玉溪市', 11],['楚雄彝族自治州', 2],['西双版纳傣族自治州', 12],['普洱市', 4],
['昭通市', 8],['曲靖市', 5],['迪庆藏族自治州', 0],['丽江市', 7],['临沧市', 1],['保山市', 8],
['怒江傈僳族自治州', 0],['大理白族自治州', 7],['德宏傣族景颇族自治州', 4],['红河哈尼族彝族自治州', 5],
['文山壮族苗族自治州', 0]]
def map_yunnan() -> Map:
c = (
Map()
.add("确诊病例", data, "云南",zoom = 1)
.set_global_opts(
title_opts=opts.TitleOpts(title="云南疫情地图"),
visualmap_opts=opts.VisualMapOpts(max_=31,is_piecewise=True,
pieces=[{"max": 0, "min": 0, "label": "0","color":"#FFFFFF"},
{"max": 9, "min": 1, "label": "0-9","color":"#FFE4E1"},
{"max": 99, "min": 10, "label": "10-99","color":"#FF7F50"},
{"max": 499, "min": 100, "label": "100-499","color":"#F08080"},
{"max": 999, "min": 500, "label": "500-999","color":"#CD5C5C"},
{"max": 9999, "min": 1000, "label": ">=1000", "color":"#8B0000"}]
),
)
)
return c
d_map = map_yunnan()
d_map.render_notebook()
d_map.render("provinces_map.html")
结果
地级市地图代码
data = [['楚雄市', 31],['玉溪市', 11],['楚雄彝族自治州', 2],['西双版纳傣族自治州', 12],['普洱市', 4],
['昭通市', 8],['曲靖市', 5],['迪庆藏族自治州', 0],['丽江市', 7],['临沧市', 1],['保山市', 8],
['怒江傈僳族自治州', 0],['大理白族自治州', 7],['德宏傣族景颇族自治州', 4],['红河哈尼族彝族自治州', 5],
['文山壮族苗族自治州', 0]]
def map_yunnan() -> Map:
c = (
Map()
.add("确诊病例", data_pair=data, maptype="楚雄彝族自治州",zoom = 1)
.set_global_opts(
title_opts=opts.TitleOpts(title="楚雄地图"),
visualmap_opts=opts.VisualMapOpts(max_=31,is_piecewise=False),
)
)
return c
d_map = map_yunnan()
d_map.render_notebook()
d_map.render("cities_map.html")
结果
警告消除
以上的代码运行时会出现警告,这是因为新版本的提示
/usr/local/lib/python3.6/site-packages/pyecharts/charts/chart.py:14: PendingDeprecationWarning: pyecharts 所有图表类型将在 v1.9.0 版本开始强制使用 ChartItem 进行数据项配置 :)
super().__init__(init_opts=init_opts)
消除方案,只需在代码上添加以下代码就不会有提示了
from pyecharts.globals import WarningType
WarningType.ShowWarning = False
转载自https://blog.csdn.net/sjtulgl/article/details/105032309