python實(shí)現(xiàn)Pyecharts實(shí)現(xiàn)動(dòng)態(tài)地圖(Map、Geo)
一些經(jīng)常畫圖的開發(fā)人員大概都用過echart,不過小白用Python比較多,學(xué)習(xí)了python下的Pyecharts,發(fā)現(xiàn)這個(gè)包真的很強(qiáng)大。下面是小白對(duì)動(dòng)態(tài)地圖的實(shí)踐案例:
假如有這樣一組數(shù)據(jù),全國每個(gè)城市的酒店數(shù)(虛擬),那么如何在地圖上展示呢?
1.Python需要安裝Pycharts
當(dāng)安裝完成后需要添加地圖包:
安裝pyecharts后還需要根據(jù)需要安裝城市、省份等地圖包,下面是對(duì)包的整理,大家可以根據(jù)需要下載。
pip install pyechartspip install echarts-countries-pypkgpip install echarts-china-provinces-pypkgpip install echarts-china-cities-pypkgpip install echarts-countries-pypkgpip install echarts-china-provinces-pypkgpip install echarts-china-cities-pypkgpip install echarts-china-counties-pypkgpip install echarts-china-misc-pypkg
2.安裝完成后,畫圖
from pyecharts import Barfrom pyecharts import Map, Geoimport pandas as pd #讀取數(shù)據(jù)inpath = ’D:/Users/traindatas/map_2.csv’ #數(shù)據(jù)路徑data = pd.read_csv(inpath , header = ’infer’) #讀取數(shù)據(jù) #瀏覽數(shù)據(jù)data.head()
數(shù)據(jù)展示如下:這里是展示2019年1到九月每個(gè)月,城市酒店數(shù)的變化情況,每個(gè)月用1號(hào)代表當(dāng)月
由于每個(gè)城市包含9條數(shù)據(jù),因此,就需要用循環(huán)做出9張MAP地圖,來展示每個(gè)月每個(gè)城市的情況
#取出日期IsDuplicated = data[’effectdate’] list_of_month = list(IsDuplicated.drop_duplicates()) #循環(huán),做出每個(gè)月的分布圖for month in list_of_month: # 城市酒店數(shù) df = data[data[’effectdate’] == month] indexs = list(df[’cityname’]) values = list(df[’masterhotelid’]) geo = Geo('全國酒店分布', str(month) + '全國酒店分布', title_color='#fff', title_pos='center', width=1200, height=600, background_color=’#404a59’) # type='effectScatter', is_random=True, effect_scale=5 使點(diǎn)具有發(fā)散性 geo.add('全國酒店分布', indexs, values, type='effectScatter', is_random=True, effect_scale=5, visual_range=[0, 900],visual_text_color='#fff', symbol_size=15, is_visualmap=True, is_roam=False) #geo.show_config() filepath = ’D:/Users/pythonfeature/map/’ +str(month) + ’_month.html’ geo.render(path=filepath)
這樣就在指定的文件夾下生成了9張圖,我們隨意打開一張圖:在本地圖片中,圖中的點(diǎn)是動(dòng)態(tài)的,但是小白不知道html類型的圖片如何上傳,所以就上傳了一個(gè)下載的PNG圖片,大家可以自行嘗試之后觀看動(dòng)圖
下圖就是上面代碼生成的9張圖,文件類型是HTML類型
打開其中一張圖看下~
當(dāng)然Pyecharts中的圖形還是很豐富的,之后小白再分享一些其他的圖形
以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. python 批量下載bilibili視頻的gui程序2. 詳解Vue中Axios封裝API接口的思路及方法3. python thrift 實(shí)現(xiàn) 單端口多服務(wù)的過程4. CSS自定義滾動(dòng)條樣式案例詳解5. python中HTMLParser模塊知識(shí)點(diǎn)總結(jié)6. python中if嵌套命令實(shí)例講解7. python:刪除離群值操作(每一行為一類數(shù)據(jù))8. 使用css實(shí)現(xiàn)全兼容tooltip提示框9. python 通過exifread讀取照片信息10. JSP實(shí)現(xiàn)客戶信息管理系統(tǒng)
