最近需要做一個關于“數字一帶一路——氣候變化和災害風險評估”的可視化項目。
甲方給的具體數據為四個 Excel 文件,分為亞洲、非洲、歐洲、澳洲四大洲,每個表格的屬性有22項,經過篩選可用的屬性為 Country、Disaster type、Disaster subtype、Total deaths、Total affected、Total damage。
因為常用的 ECharts 官方地圖下載地址暫時關閉了,經過調研決定使用 pyecharts。 pyecharts 是一個用于生成 ECharts 圖表的類庫。它是為了與 Python 進行對接,以方便在 Python 中直接使用數據生成圖。與 matplotlib 相比,matplotlib 是靜態圖,而 pyecharts 可以展示動態效果。
- pyecharts http://pyecharts.org
安裝:cmd>pip install pyecharts。 - 地圖資源 http://pyecharts.org
全球國家地圖(世界地圖和213個國家,包括中國地圖):pip install echarts-countries-pypkg;
中國省級地圖(23個省,5個自治區):pip install echarts-china-provinces-pypkg;
中國市級地圖(370個中國城市):pip install echarts-china-cities-pypkg;
中國區縣級地圖(2882個中國縣·區):pip install echarts-china-counties-pypkg;
中國區域地圖(11個中國區域地圖,比如華南、華北):pip install echarts-china-misc-pypkg。 - 提取數據
# 災難類型
import xlrd #讀取EXCELdata = xlrd.open_workbook(r'D:\Disaster database\Oceania.xls') #打開EXCEL文件
# data = xlrd.open_workbook(r'D:\Disaster database\Africa.xls') #非洲
# data = xlrd.open_workbook(r'D:\Disaster database\Asia.xls') #亞洲
# data = xlrd.open_workbook(r'D:\Disaster database\Europe.xls') #歐洲
# data = xlrd.open_workbook(r'D:\Disaster database\Oceania.xls') #澳洲table = data.sheets()[0] #打開第一張表
nrows = table.nrows #獲取表的行數print('總行數:',nrows,'\n')
print('各國統計:')
s = 'American Samoa'
# s = 'Algeria' #非洲
# s = 'Afghanistan' #亞洲
# s = 'Albania' #歐洲
# s = 'American Samoa' #澳洲now = s
L = []
i = 1
res = []
while i<=287:
# i<=1437 #非洲
# i<=3424 #亞洲
# i<=1003 #歐洲
# i<=287 #澳洲while now == s:L.append(table.row_values(i)[14])res.append(table.row_values(i)[14])i += 1if i<=287:now = table.row_values(i)[2]else:breakprint(s, ':',' Riverine flood:', L.count('Riverine flood'),' Flash flood:', L.count('Flash flood'),' Coastal flood:', L.count('Coastal flood'),' Drought:', L.count('Drought'), ' Landslide:', L.count('Landslide'),' Ground movement:', L.count('Ground movement'),' Forest fire:', L.count('Forest fire'),' Land fire (Brush, Bush, Pastur):', L.count('Land fire (Brush, Bush, Pastur'),' Tsunami:', L.count('Tsunami'),' Mudslide:', L.count('Mudslide'),' Avalanche:', L.count('Avalanche'),' Rockfall:', L.count('Rockfall'),' Subsidence:', L.count('Subsidence'))s = nowL =[]
print('\n')print('總統計:',' Riverine flood:', res.count('Riverine flood'), ' Flash flood:', res.count('Flash flood'), ' Coastal flood:', res.count('Coastal flood'), ' Drought:', res.count('Drought'), ' Landslide:', res.count('Landslide'), ' Ground movement:', res.count('Ground movement'),' Forest fire:', res.count('Forest fire'),' Land fire (Brush, Bush, Pastur):', res.count('Land fire (Brush, Bush, Pastur'),' Tsunami:', res.count('Tsunami'),' Mudslide:', res.count('Mudslide'),' Avalanche:', res.count('Avalanche'),' Rockfall:', res.count('Rockfall'),' Subsidence:', res.count('Subsidence'))
# 按格式輸出
import xlrd #讀取EXCELdata = xlrd.open_workbook(r'D:\Disaster database\Africa.xls')table = data.sheets()[0] #打開第一張表
nrows = table.nrows #獲取表的行數print('各國統計:')
s = 'Algeria'
now = s
nsum = 0
i = 1while i<=1436:while now == s:num=int(float(table.row_values(i)[17]))# print(i+1, num)nsum += numi += 1if i<=1436:now = table.row_values(i)[2]else:breakprint('\'', s, '\':', nsum, ',')s = nownsum = 0
from pyecharts import Mapprovince_distribution = {'China':11003666} # 定義數據
province_keys=province_distribution.keys()
province_values=province_distribution.values()map1 = Map('主標題', '副標題', width=1200, height=600)
map1.add('', province_keys, province_values, maptype='world', visual_range=[0, 10000], is_visualmap=True, visual_text_color='#000', is_map_symbol_show=False)
map1.render('1.html')
map1
from pyecharts import Mapprovince_distribution = {'內蒙古':9, '黑龍江':12, '新疆':20, '青海':1, '北京':15, '安徽':0, }
province_keys=province_distribution.keys()
province_values=province_distribution.values()map1 = Map('主標題', '副標題', width=1200, height=600)
map1.add('', province_keys, province_values, maptype='china', visual_range=[0, 20], is_visualmap=True, visual_text_color='#000', is_map_symbol_show=False)
map1.render('1.html')
map1
坑:…\Anaconda\Lib\site-packages\pyecharts\datasets\city_coordinates.json
參考鏈接:https://www.jianshu.com/p/e0b2851672cd
總結
以上是生活随笔為你收集整理的可视化 - pyecharts的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。