Python-geopandas 中國(guó)地圖繪制
擊上方“Python爬蟲(chóng)與數(shù)據(jù)挖掘”,進(jìn)行關(guān)注
回復(fù)“書(shū)籍”即可獲贈(zèng)Python從入門(mén)到進(jìn)階共10本電子書(shū)
上一期的地圖可視化推文教程R-ggplot2 標(biāo)準(zhǔn)中國(guó)地圖制作中,我們?cè)敿?xì)介紹了使用R-ggplot2 包完美繪制中國(guó)標(biāo)準(zhǔn)地圖,本期推文我們則試著使用Python-geopandas包繪制空間地圖,主要的知識(shí)點(diǎn)如下:
geopandas 繪制中國(guó)地圖 matplotlib add_axes()添加南海小地圖 繪圖文件分享
geopandas 讀取中國(guó)地圖文件
geopandas提供了非常方便的read_file()方法用于讀取geojson文件,我們直接進(jìn)行默認(rèn)投影(WGS84)的繪制,代碼如下:
file?=?r"中國(guó)省級(jí)地圖GS(2019)1719號(hào).geojson"
nine?=?r"九段線GS(2019)1719號(hào).geojson"
china_main?=?gpd.read_file(file)
china_nine?=?gpd.read_file(nine)
fig,?ax?=?plt.subplots(figsize=(12,?8),dpi=80)
ax?=?china_main.plot(ax=ax)
ax?=?china_nine.plot(ax=ax)
可視化結(jié)果如下:
我們進(jìn)行投影轉(zhuǎn)換(epsg=2343)和進(jìn)行一些簡(jiǎn)單的設(shè)置,代碼如下:
fig,?ax?=?plt.subplots(figsize=(12,?8),dpi=80)
ax?=?china_main.geometry.to_crs(epsg=2343).plot(fc="white",ec="black",ax=ax)
ax?=?china_nine.geometry.to_crs(epsg=2343).plot(ec="black",ax=ax)
這里注意to_crs(epsg=2343) 就可以進(jìn)行投影轉(zhuǎn)換了。
繪圖數(shù)據(jù)操作
接下來(lái),我們將我們要繪制的數(shù)據(jù)讀取、轉(zhuǎn)換并繪制在地圖上,數(shù)據(jù)預(yù)覽如下:
我們使用如下代碼將其轉(zhuǎn)換成具有地理信息的geopandas 格式數(shù)據(jù):
scattergdf?=?gpd.GeoDataFrame(
????scatter,?geometry=gpd.points_from_xy(scatter.lon,?scatter.lat),
????crs="EPSG:4326")
scattergdf.head()
結(jié)果如下:
接下來(lái)再將其轉(zhuǎn)換成 epsg=2343 投影下的數(shù)據(jù):
scattergdf_2343?=?scattergdf.to_crs(epsg=2343,?inplace=True)
以上就完成的數(shù)據(jù)的處理操作了
地圖可視化繪制
直接給出繪圖代碼,然后再進(jìn)行解釋。主要代碼如下:
fig,?ax?=?plt.subplots(figsize=(8,5),dpi=200,)
plt.rcParams['font.family']?=?['Times?New?Roman']
ax?=?china_main.geometry.to_crs(epsg=2343).plot(fc="white",ec="black",linewidth=.8,ax=ax)
ax?=?china_nine.geometry.to_crs(epsg=2343).plot(color="gray",linewidth=.9,ax=ax)
for?loc,?size,class_name?in?zip(scattergdf_2343.geometry.representative_point(),\
????????????????????????????????scattergdf_2343["data"],scattergdf_2343["class"]):
????ax.scatter(loc.x,loc.y,s=10*size,fc=class_color[class_name],ec="black",lw=.5,zorder=2)?
#添加刻度線
for?spine?in?['top','left',"bottom","right"]:
????ax.spines[spine].set_color("none")
ax.set_xlim(china_nine_2343.geometry[0].x-500000,?china_nine_2343.geometry[1].x)
ax.set_ylim(china_nine_2343.geometry[0].y,?china_nine_2343.geometry[1].y)
ax.set_xticks([])
ax.set_yticks([])
#單獨(dú)繪制圖例散點(diǎn)
ax.scatter([],?[],?c='#E21C21',?s=30,??label='cluster1',ec="black",lw=.5)?
ax.scatter([],?[],?c='#3A7CB5',?s=30,??label='cluster2',ec="black",lw=.5)
ax.scatter([],?[],?c='#51AE4F',?s=30,??label='cluster3',ec="black",lw=.5)
ax.scatter([],?[],?c='white',?s=1*10,label='1',?edgecolor='black',lw=.5)
ax.scatter([],?[],?c='white',?s=2*10,label='2',?edgecolor='black',lw=.5)
ax.scatter([],?[],?c='white',?s=3*10,label='3',?edgecolor='black',lw=.5)
ax.scatter([],?[],?c='white',?s=4*10,label='4',?edgecolor='black',lw=.5)
ax.scatter([],?[],?c='white',?s=5*10,label='5',?edgecolor='black',lw=.5)
ax.legend(frameon=False,ncol=8,loc="upper?center",
?????????fontsize=9,columnspacing=.2)
ax.text(.91,-0.02,'\nVisualization?by?DataCharm',transform?=?ax.transAxes,
????????ha='center',?va='center',fontsize?=?6,color='black')
#添加南海小地圖????
ax_child?=?fig.add_axes([0.688,?0.125,?0.2,?0.2])
ax_child?=?china_main.geometry.to_crs(epsg=2343).plot(ax=ax_child,
????????????????????????????????????????????????????????fc="white",
????????????????????????????????????????????????????????ec="black",)
ax_child?=?china_nine.geometry.to_crs(epsg=2343).plot(ax=ax_child,
????????????????????????????????????????????????????????color="gray",
????????????????????????????????????????????????????????linewidth=.9,
????????????????????????????????????????????????????????)
for?loc,?size,class_name?in?zip(scattergdf_2343.geometry.representative_point(),\
????????????????????????????????scattergdf_2343["data"],scattergdf_2343["class"]):
????ax_child.scatter(loc.x,loc.y,s=10*size,fc=class_color[class_name],ec="black",lw=.5,zorder=2)
ax_child.set_xlim(china_nine_2343.geometry[2].x,?china_nine_2343.geometry[3].x)
ax_child.set_ylim(china_nine_2343.geometry[2].y,?china_nine_2343.geometry[3].y)
#?移除子圖坐標(biāo)軸刻度,
ax_child.set_xticks([])
ax_child.set_yticks([])
add_axes() 添加南海小地圖
#添加南海小地圖????
ax_child?=?fig.add_axes([0.688,?0.125,?0.2,?0.2])
ax_child?=?china_main.geometry.to_crs(epsg=2343).plot(ax=ax_child,
????????????????????????????????????????????????????????fc="white",
????????????????????????????????????????????????????????ec="black",)
ax_child?=?china_nine.geometry.to_crs(epsg=2343).plot(ax=ax_child,
????????????????????????????????????????????????????????color="gray",
????????????????????????????????????????????????????????linewidth=.9,
????????????????????????????????????????????????????????)
for?loc,?size,class_name?in?zip(scattergdf_2343.geometry.representative_point(),\
????????????????????????????????scattergdf_2343["data"],scattergdf_2343["class"]):
????ax_child.scatter(loc.x,loc.y,s=10*size,fc=class_color[class_name],ec="black",lw=.5,zorder=2)
ax_child.set_xlim(china_nine_2343.geometry[2].x,?china_nine_2343.geometry[3].x)
ax_child.set_ylim(china_nine_2343.geometry[2].y,?china_nine_2343.geometry[3].y)
#?移除子圖坐標(biāo)軸刻度,
ax_child.set_xticks([])
ax_child.set_yticks([])
可以發(fā)現(xiàn),除了顯示范圍的不同外,其他的和繪制主題部分的代碼一致。
單獨(dú)添加圖例
#單獨(dú)繪制圖例散點(diǎn)
ax.scatter([],?[],?c='#E21C21',?s=30,??label='cluster1',ec="black",lw=.5)?
ax.scatter([],?[],?c='#3A7CB5',?s=30,??label='cluster2',ec="black",lw=.5)
ax.scatter([],?[],?c='#51AE4F',?s=30,??label='cluster3',ec="black",lw=.5)
ax.scatter([],?[],?c='white',?s=1*10,label='1',?edgecolor='black',lw=.5)
ax.scatter([],?[],?c='white',?s=2*10,label='2',?edgecolor='black',lw=.5)
ax.scatter([],?[],?c='white',?s=3*10,label='3',?edgecolor='black',lw=.5)
ax.scatter([],?[],?c='white',?s=4*10,label='4',?edgecolor='black',lw=.5)
ax.scatter([],?[],?c='white',?s=5*10,label='5',?edgecolor='black',lw=.5)
ax.legend(frameon=False,ncol=8,loc="upper?center",
?????????fontsize=9,columnspacing=.2)
這部分還是為了更好的定制化圖例,希望大家可以掌握。
最終,我們的可視化效果如下:
注:該數(shù)據(jù)只限于練習(xí)交流,請(qǐng)勿用于科研、出版使用。
總結(jié)
本期推文使用了Python-geopandas進(jìn)行了中國(guó)地圖的繪制,講解了數(shù)據(jù)標(biāo)記,投影轉(zhuǎn)換等內(nèi)容。但需指出的是:
geopandas 的安裝較為麻煩,建議使用 conda install --channel conda-forge geopandas 進(jìn)行安裝。 Python 繪制空間可視化還是存在部分問(wèn)題(無(wú)法較容易的添加如比例尺、指北針等空間繪圖元素),也在進(jìn)一步完善過(guò)程中。
-------------------?End?-------------------
往期精彩文章推薦:
手把手教你使用Python庫(kù)打造一款簡(jiǎn)易黑客工具
手把手教你用Python打造一個(gè)語(yǔ)音合成系統(tǒng)
如何利用Scrapy爬蟲(chóng)框架抓取網(wǎng)頁(yè)全部文章信息(下篇)

歡迎大家點(diǎn)贊,留言,轉(zhuǎn)發(fā),轉(zhuǎn)載,感謝大家的相伴與支持
想加入Python學(xué)習(xí)群請(qǐng)?jiān)诤笈_(tái)回復(fù)【入群】
萬(wàn)水千山總是情,點(diǎn)個(gè)【在看】行不行
/今日留言主題/
隨便說(shuō)一兩句吧~~
