其他
「教程」使用 pyecharts 绘制疫情地图
文 / fungis
一个热带生活、乐于分享、努力搬砖的giser
pyecharts 是一个用于生成 Echarts 图表的类库,用 Echarts 生成的图可视化效果非常棒。相比利用gis桌面产品制图来说,利用pyecharts来制作地图,少了矢量数据的限制,只要输入正确格式的数据,就能快速地制作地图,有多套地图样式可以使用,对于制作一些相对简单、频繁变动的图表来说具有莫大的优势。废话不多说,下面开始上图上代码。
实现代码
"""
@Author : fungis@163.com
@Time : 2020/2/9 13:12
"""
from pyecharts import options as opts
from pyecharts.charts import Map
from pyecharts.globals import ThemeType
data = [['江苏', 492], ['安徽', 830], ['湖南', 879], ['河南', 1073], ['浙江', 1092], ['广东', 1151], ['天津', 94], ['内蒙古', 58],
['北京', 337], ['陕西', 213], ['广西', 210], ['宁夏', 49], ['贵州', 109], ['上海', 295], ['江西', 771], ['福建', 261],
['新疆', 49], ['四川', 405], ['云南', 141], ['海南', 136], ['山东', 459], ['吉林', 80], ['黑龙江', 331], ['河北', 218],
['香港', 36], ['重庆', 468], ['山西', 119], ['甘肃', 83], ['湖北', 29631], ['辽宁', 107], ['台湾', 18], ['澳门', 10],
['青海', 18], ['西藏', 1]]
map = (
Map(init_opts=opts.InitOpts(bg_color="#FFFAFA", theme=ThemeType.ESSOS, width=1000))
.add("确诊人数", data)
.set_global_opts(
title_opts=opts.TitleOpts(title="fungis-基于丁香园数据的疫情图"),
visualmap_opts=opts.VisualMapOpts(
is_piecewise=True,
pieces=[
{"min": 2000, "label": '>2000人', "color": "#eb2f06"},
{"min": 1000, "max": 2000, "label": '1001-2000人', "color": "#FF3030"},
{"min": 500, "max": 1000, "label": '500-1000人', "color": "#FF4500"},
{"min": 100, "max": 499, "label": '100-499人', "color": "#FF7F50"},
{"min": 10, "max": 99, "label": '10-99人', "color": "#FFA500"},
{"min": 1, "max": 9, "label": '1-9人', "color": "#FFDEAD"},
],
range_text=['高', '低'],
),
)
)
map.render(path="./中国疫情人数分级设色地图1.html")
效果如图:
代码如下
"""
@Author : fungis@163.com
@Time : 2020/2/9 13:12
"""
from pyecharts import options as opts
from pyecharts.charts import Bar,Line,Grid
from pyecharts.globals import ThemeType
columns = ['江苏', '安徽', '湖南', '河南', '浙江', '广东', '天津', '内蒙古', '北京', '陕西', '广西', '宁夏', '贵州', '上海', '江西', '福建', '新疆', '四川',
'云南', '海南', '山东', '吉林', '黑龙江', '河北', '香港', '重庆', '山西', '甘肃', '湖北', '辽宁', '台湾', '澳门', '青海', '西藏']
confirmedCount = [492, 830, 879, 1073, 1092, 1151, 94, 58, 337, 213, 210, 49, 109, 295, 771, 261, 49, 405, 141, 136,
459, 80, 331, 218, 36, 468, 119, 83, 29631, 107, 18, 10, 18, 1]
curedCount = [80, 80, 194, 179, 205, 148, 4, 5, 44, 26, 18, 13, 7, 44, 102, 35, 0, 76, 18, 19, 63, 12, 15, 35, 0, 51,
25, 16, 1795, 12, 1, 1, 3, 0]
deadCount = [0, 3, 1, 6, 0, 1, 1, 0, 2, 0, 1, 0, 1, 1, 1, 0, 0, 1, 0, 3, 1, 1, 7, 2, 1, 2, 0, 2, 871, 0, 0, 0, 0, 0]
grid = Grid()
bar = Bar()
line = Line()
grid.theme = ThemeType.PURPLE_PASSION
grid.width = 1200
bar.height=1000
bar.add_xaxis(columns)
bar.add_yaxis("治愈人数",curedCount)
bar.add_yaxis("死亡人数",deadCount)
bar.set_global_opts(title_opts=opts.TitleOpts("全国疫情确诊人数分布图-fungis")
,tooltip_opts=opts.TooltipOpts(trigger="axis", axis_pointer_type="cross")
)
bar.extend_axis(yaxis=opts.AxisOpts(type_="value",
name="人数",
min_=0,
max_=35000,
position="right",
axislabel_opts=opts.LabelOpts(formatter="{value} 人"),
))
line.add_xaxis(columns)
line.add_yaxis("确诊人数",confirmedCount,yaxis_index = 1)
bar.overlap(line)
grid.add(chart = bar,grid_opts = opts.GridOpts(),is_control_axis_index = True)
grid.render(path='./中国组合图1.html')
代码如下:
"""
@Author : fungis@163.com
@Time : 2020/2/9 13:53
"""
from pyecharts.charts import Map
from pyecharts import options as opts
from pyecharts.globals import ThemeType
data = [['信阳市', 220], ['南阳市', 134], ['郑州市', 130], ['驻马店市', 123], ['商丘市', 83], ['周口市', 65], ['平顶山市', 52], ['新乡市', 46],
['安阳市', 45], ['许昌市', 31], ['漯河市', 30], ['洛阳市', 27], ['焦作市', 25], ['开封市', 24], ['鹤壁市', 17], ['濮阳市', 10],
['三门峡市', 7], ['济源市', 4]]
cityName = ['信阳市', '南阳市', '郑州市', '驻马店市', '商丘市', '周口市', '平顶山市', '新乡市', '安阳市', '许昌市', '漯河市', '洛阳市', '焦作市', '开封市', '鹤壁市',
'濮阳市', '三门峡市', '济源市']
confirmedCount = [220, 134, 130, 123, 83, 65, 52, 46, 45, 31, 30, 27, 25, 24, 17, 10, 7, 4]
curedCount = [28, 28, 34, 14, 9, 11, 12, 6, 10, 2, 6, 2, 1, 1, 3, 0, 3, 0]
deadCount = [0, 2, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
map = (
Map(init_opts=opts.InitOpts(bg_color="#FFFAFA", theme=ThemeType.ROMANTIC, width=1000))
.add("确诊人数", data, "河南", is_map_symbol_show=False, )
.set_global_opts(
title_opts=opts.TitleOpts(title="河南疫情确诊人数分布图-fungis", pos_left="left"),
visualmap_opts=opts.VisualMapOpts(
is_piecewise=True,
pieces=[
{"min": 201, "label": '>200人', "color": "#e55039"},
{"min": 101, "max": 200, "label": '101-200人', "color": "#FF4500"},
{"min": 51, "max": 100, "label": '51-100人', "color": "#FF7F50"},
{"min": 10, "max": 50, "label": '10-50人', "color": "#FFA500"},
{"min": 1, "max": 9, "label": '1-9人', "color": "#FFDEAD"},
],
range_text=['高', '低'],
),
)
)
map.render(path="./河南疫情确诊人数分布图1.html")
效果如图:
代码如下:
"""
@Author : fungis@163.com
@Time : 2020/2/9 13:53
"""
from pyecharts.charts import Bar
from pyecharts import options as opts
from pyecharts.globals import ThemeType
columns = ['信阳市', '南阳市', '郑州市', '驻马店市', '商丘市', '周口市', '平顶山市', '新乡市', '安阳市', '许昌市', '漯河市', '洛阳市', '焦作市', '开封市', '鹤壁市',
'濮阳市', '三门峡市', '济源市']
confirmedCount = [220, 134, 130, 123, 83, 65, 52, 46, 45, 31, 30, 27, 25, 24, 17, 10, 7, 4]
curedCount = [28, 28, 34, 14, 9, 11, 12, 6, 10, 2, 6, 2, 1, 1, 3, 0, 3, 0]
deadCount = [0, 2, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
bar = (
Bar(init_opts=opts.InitOpts(bg_color="#FFFAFA", theme=ThemeType.LIGHT, width=1200))
.add_xaxis(columns)
.add_yaxis("治愈人数", curedCount)
.add_yaxis("死亡人数", deadCount)
.extend_axis(yaxis=opts.AxisOpts(type_="value",
name="人数",
position="left",
axislabel_opts=opts.LabelOpts(formatter="{value} 人"),
))
.set_global_opts(title_opts=opts.TitleOpts(title="fungis-基于丁香园数据的河南疫情图"))
)
bar.render(path="./河南柱状图1.html")
参照之前文章《使用Python抓取2019-nCov疫情数据》
链接:https://pan.baidu.com/s/1soKUoDl6fcOzbZcu8wCCHQ
密码:74xm
投稿
扫码加小编
一起"稿"GIS
END
291616564(QQ群一)
166408035(QQ群二)
627853279(QQ群三)436386604(QQ群四)
606176554(QQ群五)
946178380(QQ群六)
861271808(QQ群七)
http://malagis.com
长按识别二维码关注我们