pyecharts绘制地图可视化
pyecharts:官方文档
我们这里使用pyecharts模块进行绘图。
pyecharts 项目包含了一系列的地理地图数据,这些数据或者已经内置,或者需要额外安装和加载,我们需要下载下面六个包。
选择自己需要的安装 pip install echarts-countries-pypkg pip install echarts-china-provinces-pypkg pip install echarts-china-cities-pypkg pip install echarts-china-counties-pypkg pip install echarts-china-misc-pypkg pip install echarts-united-kingdom-pypkg
pyecharts中Geo表达和城市关联的数据,Map表达和国家和省份关联的数据。
世界地图
from pyecharts import Map, Geo # 世界地图数据 value = [95.1, 23.2, 43.3, 66.4, 88.5] attr= ["China", "Canada", "Brazil", "Russia", "United States"] map0 = Map("世界地图示例", width=1200, height=600) map0.add("世界地图", attr, value, maptype="world", is_visualmap=True, visual_text_color=\'#000\') map0.render(path="世界地图.html")
中国地图
from pyecharts import Map, Geo # 中国地图 province_distribution = {\'河南\': 45.23, \'北京\': 37.56, \'河北\': 21, \'辽宁\': 12, \'江西\': 6, \'上海\': 20, \'安徽\': 10, \'江苏\': 16, \'湖南\': 9, \'浙江\': 13, \'海南\': 2, \'广东\': 22, \'湖北\': 8, \'黑龙江\': 11, \'澳门\': 1, \'陕西\': 11, \'四川\': 7, \'内蒙古\': 3, \'重庆\': 3, \'云南\': 6, \'贵州\': 2, \'吉林\': 3, \'山西\': 12, \'山东\': 11, \'福建\': 4, \'青海\': 1, \'舵主科技,质量保证\': 1, \'天津\': 1, \'其他\': 1} provice = list(province_distribution.keys()) values = list(province_distribution.values()) map = Map("中国地图",\'中国地图\', width=1200, height=600) map.add("", provice, values, visual_range=[0, 50], maptype=\'china\', is_visualmap=True, visual_text_color=\'#000\') map.show_config() map.render(path="中国地图.html")
省份地图
from pyecharts import Map, Geo # 城市 -- 指定省的城市 xx市 city = [\'郑州市\', \'安阳市\', \'洛阳市\', \'濮阳市\', \'南阳市\', \'开封市\', \'商丘市\', \'信阳市\', \'新乡市\'] values2 = [1.07, 3.85, 6.38, 8.21, 2.53, 4.37, 9.38, 4.29, 6.1] map2 = Map("河南地图",\'河南\', width=1200, height=600) map2.add(\'河南\', city, values2, visual_range=[1, 10], maptype=\'河南\', is_visualmap=True, visual_text_color=\'#000\') map2.show_config() map2.render(path="河南地图.html")
市区地图
from pyecharts import Map, Geo # 区县 -- 具体城市内的区县 xx县 quxian = [\'夏邑县\', \'民权县\', \'梁园区\', \'睢阳区\', \'柘城县\', \'宁陵县\'] values3 = [3, 5, 7, 8, 2, 4] map3 = Map("商丘地图",\'商丘\', width=1200, height=600) map3.add("商丘", quxian, values3, visual_range=[1, 10], maptype=\'商丘\', is_visualmap=True, visual_text_color=\'#000\') map3.render(path="商丘地图.html")
热力图
from pyecharts import Map, Geo data = [ ("海门", 9), ("鄂尔多斯", 12), ("招远", 12), ("舟山", 12), ("齐齐哈尔", 14), ("盐城", 15), ("赤峰", 16), ("青岛", 18), ("乳山", 18), ("金昌", 19), ("泉州", 21), ("莱西", 21), ("日照", 21), ("胶南", 22), ("南通", 23), ("拉萨", 24), ("云浮", 24), ("梅州", 25)] attr, value = Geo.cast(data) geo = Geo("全国主要城市空气质量热力图", "data from pm2.5", title_color="#fff", title_pos="center", width=1200, height=600, background_color=\'#404a59\') geo.add("空气质量热力图", attr, value, visual_range=[0, 25], type=\'heatmap\', visual_text_color="#fff", symbol_size=15, is_visualmap=True, is_roam=False) geo.show_config() geo.render(path="空气质量热力图.html")
from pyecharts import Map, Geo indexs = [\'上海\', \'北京\', \'合肥\', \'哈尔滨\', \'广州\', \'成都\', \'无锡\', \'杭州\', \'武汉\', \'深圳\', \'西安\', \'郑州\', \'重庆\', \'长沙\'] values = [4.07, 1.85, 4.38, 2.21, 3.53, 4.37, 1.38, 4.29, 4.1, 1.31, 3.92, 4.47, 2.40, 3.60] geo = Geo("全国主要城市空气质量评分", "data from pm2.5", title_color="#fff", title_pos="center", width=1200, height=600, background_color=\'#404a59\') # type="effectScatter", is_random=True, effect_scale=5 使点具有发散性 geo.add("空气质量评分", indexs, values, type="effectScatter", is_random=True, effect_scale=5, visual_range=[0, 5], visual_text_color="#fff", symbol_size=15, is_visualmap=True, is_roam=False) geo.show_config() geo.render(path="空气质量评分.html")
from pyecharts import Map, Geo data = [ ("海门", 9), ("鄂尔多斯", 12), ("招远", 12), ("舟山", 12), ("齐齐哈尔", 14), ("盐城", 15), ("赤峰", 16), ("青岛", 18), ("乳山", 18), ("金昌", 19), ("泉州", 21), ("莱西", 21), ("日照", 21), ("胶南", 22), ("南通", 23), ("拉萨", 24), ("云浮", 24), ("梅州", 25)] attr, value = Geo.cast(data) geo = Geo("全国主要城市空气质量热力图", "data from pm2.5", title_color="#fff", title_pos="center", width=1200, height=600, background_color=\'#404a59\') geo.add("空气质量热力图", attr, value, visual_range=[0, 25], type=\'scatter\', visual_text_color="#fff", symbol_size=15, is_visualmap=True, is_roam=False) geo.show_config() geo.render(path="质量热力图.html")
示例:获取冠状病毒感染数据并可视化
import urllib import urllib.request import json import os import prettytable as pt from pyecharts import Map, Geo class Searchdata(object): def __init__(self,url): self.url = url self.china_list = {} self.china_data = {} self.province_list = {} self.province_data = {} def get_data(self): try: data = urllib.request.urlopen(self.url) data = json.loads(data.read()) self.save(data) return data except: print("请求数据出错!") def is_exist(self): for i in os.listdir(os.getcwd()): if i == "data.txt": return 1 return 0 def save(self,data): if self.is_exist() == 0: with open("data.txt",\'w\') as fp: fp.write(str(data)) def get_china(self,data): data = data[\'data\'] self.china_data[\'total\'] = data[\'gntotal\'] self.china_data[\'deathNum\'] = data[\'deathtotal\'] self.china_data[\'susNum\'] = data[\'sustotal\'] self.china_data[\'cureNum\'] = data[\'curetotal\'] for i in data[\'list\']: dic ={} dic[\'total\'] = i[\'value\'] dic[\'deathNum\'] = i[\'deathNum\'] dic[\'susNum\'] = i[\'susNum\'] dic[\'cureNum\'] = i[\'cureNum\'] self.china_list[i[\'name\']] = dic def get_province(self,data): data = data[\'data\'] for i in data[\'list\']: if i[\'name\'] == \'湖北\': self.province_data[\'total\'] = i[\'value\'] self.province_data[\'deathNum\'] = i[\'deathNum\'] self.province_data[\'susNum\'] = i[\'susNum\'] self.province_data[\'cureNum\'] = i[\'cureNum\'] for temp in i[\'city\']: dic = {} dic[\'total\'] = temp[\'conNum\'] dic[\'deathNum\'] = temp[\'deathNum\'] dic[\'susNum\'] = temp[\'susNum\'] dic[\'cureNum\'] = temp[\'cureNum\'] self.province_list[temp[\'name\']] = dic return def get_sheet1(self): self.tb = pt.PrettyTable() self.tb.field_names = ["省份", "总数", "死亡", "疑似", "治愈"] for k, v in self.china_list.items(): self.tb.add_row([k, v[\'total\'], v[\'deathNum\'], v[\'susNum\'],v[\'cureNum\']]) self.tb.add_row([\'合计\', self.china_data[\'total\'],self.china_data[\'deathNum\'], self.china_data[\'susNum\'], self.china_data[\'cureNum\']]) print(self.tb) def get_sheet2(self): self.tb = pt.PrettyTable() self.tb.field_names = ["地级市", "总数", "死亡", "疑似", "治愈"] for k, v in self.province_list.items(): self.tb.add_row([k, v[\'total\'], v[\'deathNum\'], v[\'susNum\'], v[\'cureNum\']]) self.tb.add_row([\'合计\', self.province_data[\'total\'], self.province_data[\'deathNum\'], self.province_data[\'susNum\'], self.province_data[\'cureNum\']]) print(self.tb) def get_graph1(self): provice = list(self.china_list.keys()) values = [] for k, v in self.china_list.items(): values.append(v[\'total\']) # 中国地图 map = Map("中国地图", \'中国地图\', width=1200, height=600) map.add("", provice, values, visual_range=[0, 20000], maptype=\'china\', is_visualmap=True, visual_text_color=\'#000\') map.show_config() map.render(path="地图.html") def get_graph2(self): city = list(self.province_list.keys()) new_city = [] for str in city: if len(str) == 2: str = str+"市" new_city.append(str) elif len(str) == 3: str = "恩施土家族苗族自治州" new_city.append(str) else: new_city.append(str) values = [] for k, v in self.province_list.items(): values.append(v[\'total\']) map2 = Map("湖北地图", \'湖北\', width=1200, height=600) map2.add(\'\', new_city, values, visual_range=[0,10000], maptype=\'湖北\', is_visualmap=True, visual_text_color=\'#000\') map2.show_config() map2.render(path="湖北地图.html") Searchtool =Searchdata(\'http://43.250.238.179:9090/showData\') data = Searchtool.get_data() Searchtool.get_province(data) Searchtool.get_sheet2() Searchtool.get_graph2()
refer:
http://pyecharts.herokuapp.com/
版权声明:本文为-wenli原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。