色综合图-色综合图片-色综合图片二区150p-色综合图区-玖玖国产精品视频-玖玖香蕉视频

您的位置:首頁技術(shù)文章
文章詳情頁

Python如何使用bokeh包和geojson數(shù)據(jù)繪制地圖

瀏覽:7日期:2022-08-01 16:52:30

最近要繪制倫敦區(qū)地圖,查閱了很多資料后最終選擇使用bokeh包以及倫敦區(qū)的geojson數(shù)據(jù)繪制。bokeh是基于python的繪圖工具,可以繪制各種類型的圖表,支持geojson數(shù)據(jù)的讀取及繪制地圖。

安裝bokeh

$ pip install bokeh

軟件版本

python-3.7.7bokeh-2.0.0

數(shù)據(jù)來源

倫敦地圖數(shù)據(jù)來源于Highmaps地圖數(shù)據(jù)集。下載的是英國的地圖數(shù)據(jù)united-kindom.geo.json。需要對得到的數(shù)據(jù)進(jìn)行預(yù)處理才能得到只含倫敦地區(qū)的數(shù)據(jù)。這需要對geojson數(shù)據(jù)的格式有一定的了解。在對數(shù)據(jù)進(jìn)行處理之前,先看如何繪制英國地圖。

繪制英國地圖

from bokeh.plotting import curdoc, figurefrom bokeh.models import GeoJSONDataSource# 讀入英國地圖數(shù)據(jù)并傳給GeoJSONDataSourcewith open('united-kindom.geo.json', encoding='utf8') as f: geo_source = GeoJSONDataSource(geojson=f.read())# 設(shè)置一張畫布p = figure(width=500, height=500)# 使用patches函數(shù)以及geo_source繪制地圖p.patches(xs=’xs’, ys=’ys’, source=geo_source)curdoc().add_root(p)

上述代碼可以繪制出英國地圖。將上述代碼保存為test.py,在終端運(yùn)行

$ bokeh serve --show test.py

這會自動打開瀏覽器,并顯示英國地圖。運(yùn)行結(jié)果如圖:

Python如何使用bokeh包和geojson數(shù)據(jù)繪制地圖

獲取倫敦地區(qū)數(shù)據(jù)

獲取倫敦地區(qū)數(shù)據(jù)可以手動從united-kingdom.geo.json文件中篩選出倫敦的數(shù)據(jù),也可以先用python先把數(shù)據(jù)過濾一遍,然后將數(shù)據(jù)傳給bokeh。這需要對geojson文件格式有一定的了解,在此不詳細(xì)介紹。

from bokeh.plotting import curdoc, figurefrom bokeh.models import GeoJSONDataSourceimport json# 用json庫讀取數(shù)據(jù)with open('united-kindom.geo.json', encoding='utf8') as f: data = json.loads(f.read())# 判斷是不是倫敦地區(qū)數(shù)據(jù)def isInLondon(district): if ’type’ in district[’properties’] and ’london borough’ in district[’properties’][’type’].lower(): return True if ’type-en’ in district[’properties’] and ’london borough’ in district[’properties’][’type’].lower(): return True if ’woe-name’ in district[’properties’] and ’city of london’ in district[’properties’][’woe-name’].lower(): return True return False# 過濾數(shù)據(jù)data[’features’] = list(filter(isInLondon, data[’features’]))#geo_source = GeoJSONDataSource(geojson=json.dumps(data))p = figure(width=500, height=500)p.patches(xs=’xs’, ys=’ys’, source=geo_source)curdoc().add_root(p)

運(yùn)行結(jié)果如圖:

Python如何使用bokeh包和geojson數(shù)據(jù)繪制地圖

美化

上面的倫敦地圖只是一個(gè)大概的輪廓,下面對地圖添加一系列功能。

添加各區(qū)輪廓線

p.patches(xs=’xs’, ys=’ys’, fill_alpha=0.7, # 畫輪廓線 line_color=’white’, # 線的顏色 line_width=0.5, # 線的寬度 source=geo_source)

現(xiàn)在地圖區(qū)域輪廓很清晰。

添加顏色

# 為每一個(gè)地區(qū)增加一個(gè)color屬性for i in range(len(data[’features’])): data[’features’][i][’properties’][’color’] = [’blue’, ’red’, ’yellow’, ’orange’, ’gray’, ’purple’][i % 6]p.patches(xs=’xs’, ys=’ys’, fill_alpha=0.7, line_color=’white’, line_width=0.5, color='color', # 增加顏色屬性,這里的'color'對應(yīng)每個(gè)地區(qū)的color屬性 source=geo_source)

現(xiàn)在地圖五顏六色。

增加圖注

import random# 隨機(jī)產(chǎn)生數(shù)據(jù)用于展示for i in range(len(data[’features’])): data[’features’][i][’properties’][’number’] = random.randint(0, 20_000)p = figure(width=500, height=500, tooltips='@name, number: @number' # 使用tooltips生成圖注,@+屬性名稱,這里的name是數(shù)據(jù)中原本有的,number是新近添加的。 )

現(xiàn)在鼠標(biāo)放到區(qū)域上時(shí),會顯示'區(qū)域名, number: 數(shù)字'。

去掉坐標(biāo)軸與背景線

p.axis.axis_label = Nonep.axis.visible = Falsep.grid.grid_line_color = None

最終代碼

from bokeh.plotting import curdoc, figurefrom bokeh.models import GeoJSONDataSourceimport jsonimport randomwith open('united-kindom.geo.json', encoding='utf8') as f: data = json.loads(f.read())def isInLondon(district): if ’type’ in district[’properties’] and ’london borough’ in district[’properties’][’type’].lower(): return True if ’type-en’ in district[’properties’] and ’london borough’ in district[’properties’][’type’].lower(): return True if ’woe-name’ in district[’properties’] and ’city of london’ in district[’properties’][’woe-name’].lower(): return True return Falsedata[’features’] = list(filter(isInLondon, data[’features’]))for i in range(len(data[’features’])): data[’features’][i][’properties’][’color’] = [’blue’, ’red’, ’yellow’, ’orange’, ’gray’, ’purple’][i % 6] data[’features’][i][’properties’][’number’] = random.randint(0, 20_000)geo_source = GeoJSONDataSource(geojson=json.dumps(data))p = figure(width=500, height=500, tooltips='@name, number: @number')p.patches(xs=’xs’, ys=’ys’, fill_alpha=0.7, line_color=’white’, line_width=0.5, color='color', source=geo_source)p.axis.axis_label = Nonep.axis.visible = Falsep.grid.grid_line_color = Nonecurdoc().add_root(p)

倫敦地圖完成了

Python如何使用bokeh包和geojson數(shù)據(jù)繪制地圖

總結(jié)

最開始想用pyecharts做的,但是pyecharts并沒有倫敦的地圖。折騰半天,最后只好自己找geojson數(shù)據(jù)來畫地圖。

找到了很多關(guān)于地圖的數(shù)據(jù)和工具,比如上文中提到的highmap數(shù)據(jù)集,以及DataV.altas,這個(gè)工具可以可視化地提取中國區(qū)域的地圖數(shù)據(jù),但感覺比起自己找數(shù)據(jù),畫中國地圖還是pyecharts來得實(shí)在。

數(shù)據(jù)最重要。

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。

標(biāo)簽: Python 編程
相關(guān)文章:
主站蜘蛛池模板: 美女张开腿让男生桶出水 | videosfree性欧美另类 | 成人午夜精品 | 亚洲网站www| 久久se精品一区精品二区 | 国产三级国产精品国产普男人 | 毛片在线看免费 | 欧美jizzhd精品欧美另类 | 亚洲欧美日本国产 | 国产普通话一二三道 | 男女男精品视频 | 欧美一区二区三区视频在线观看 | 久久久亚洲欧洲日产国码二区 | 国产美女动态免费视频 | 手机免费黄色网址 | 伊人22综合| 久久精品99精品免费观看 | 国产综合在线观看 | 亚洲伦| 国外精品视频在线观看免费 | 国产成人99精品免费观看 | 91精品成人免费国产 | 欧美一级毛片日韩一级 | 99精品久久99久久久久 | 国产精品久久久久久久久免费观看 | 美女131爽爽爽做爰中文视频 | 中文字幕乱码无线码在线 | 日韩精品一区二区三区在线观看l | 日韩在线观看视频网站 | 香蕉99国内自产自拍视频 | 97在线播放视频 | 精品成人在线视频 | a级成人毛片免费视频高清 a级高清观看视频在线看 | 久久久久毛片免费观看 | 欧美 亚洲 另类 自拍 在线 | 夜色邦合成福利网站 | 中文字幕区 | 老太婆性杂交毛片 | 国产在线不卡免费播放 | 国产成人在线看 | 在线观看免费视频国产 |