您好,登錄后才能下訂單哦!
這期內(nèi)容當(dāng)中小編將會(huì)給大家?guī)?lái)有關(guān)Python中怎么繪制矢量數(shù)據(jù),文章內(nèi)容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。
學(xué)習(xí)目標(biāo):
為多個(gè)矢量數(shù)據(jù)集繪制地圖,并根據(jù)屬性進(jìn)行配色
自定義地圖圖例
在本節(jié)中,將學(xué)習(xí)如何自定義地圖符號(hào)和用于在Python中表示矢量數(shù)據(jù)的顏色和符號(hào),使用geopandas和matplotlib進(jìn)行地圖繪制
首先導(dǎo)入需要使用到的包:
import os import matplotlib.pyplot as plt import numpy as np from shapely.geometry import box import geopandas as gpd import earthpy as et
# 下載數(shù)據(jù) # data = et.data.get_data('spatial-vector-lidar') os.chdir(os.path.join(et.io.HOME, 'learning','python_data_plot'))
# 導(dǎo)入數(shù)據(jù) sjer_roads_path="data/california/madera-county-roads/tl_2013_06039_roads.shp" sjer_roads = gpd.read_file(sjer_roads_path) print(type(sjer_roads['RTTYP'])) print(sjer_roads['RTTYP'].unique())
<class 'pandas.core.series.Series'> ['M' None 'S' 'C']
可以看出道路類型中有一些缺失的值,由于需要繪制所有的道路類型,甚至那些設(shè)置為None
的道路類型,下面將RTTYP
屬性None
為Unknown
sjer_roads['RTTYP'].replace(np.nan,"Unknown",inplace=True) # sjer_roads.loc[sjer_roads['RTTYP'].isnull(), 'RTTYP'] = 'Unknown' print(sjer_roads['RTTYP'].unique())
['M' 'Unknown' 'S' 'C']
如果使用geopandas.Plot()
繪制數(shù)據(jù),當(dāng)設(shè)置了column =
參數(shù)后,則geopandas將為線條自動(dòng)選擇顏色,可以使用legend = True
參數(shù)添加圖例
fig, ax = plt.subplots(figsize=(14,6)) sjer_roads.plot(column='RTTYP', categorical=True, legend=True, ax=ax ) # 調(diào)整圖例位置 leg = ax.get_legend() leg.set_bbox_to_anchor((1.15,0.5)) # 隱藏邊框 ax.set_axis_off() plt.show()
為了按屬性值繪制一個(gè)矢量圖層,這樣每條道路圖層就會(huì)根據(jù)它各自的屬性值來(lái)著色,所以圖例也代表了同樣的符號(hào),需要三個(gè)步驟:
創(chuàng)建一個(gè)將特定顏色與特定屬性值關(guān)聯(lián)的字典
循環(huán)遍歷并將該顏色應(yīng)用于每個(gè)屬性值
最后,在繪圖中添加一個(gè)label
參數(shù),以便調(diào)用ax.legend()
生成最終的圖例
下面,先創(chuàng)建一個(gè)字典來(lái)定義您希望使用哪種顏色繪制每種道路類型:
# Create a dictionary where you assign each attribute value to a particular color roadPalette = {'M': 'blue', 'S': 'green', 'C': 'purple', 'Unknown': 'grey'} roadPalette
{'M': 'blue', 'S': 'green', 'C': 'purple', 'Unknown': 'grey'}
接下來(lái),循環(huán)遍歷每個(gè)屬性值,并使用字典中指定的顏色用該屬性值繪制線條
fig, ax = plt.subplots(figsize=(10,10)) # 根據(jù)道路類型分組進(jìn)行繪制 for ctype,data in sjer_roads.groupby('RTTYP'): color = roadPalette[ctype] data.plot(color=color, ax=ax, label=ctype ) ax.legend(bbox_to_anchor=(1.0, .5), prop={'size': 12}) ax.set(title='Madera County Roads') ax.set_axis_off() plt.show()
可以通過(guò)linewidth =
屬性對(duì)線條寬度進(jìn)行設(shè)置,
fig, ax = plt.subplots(figsize=(10, 10)) # Loop through each group (unique attribute value) in the roads layer and assign it a color for ctype, data in sjer_roads.groupby('RTTYP'): color = roadPalette[ctype] data.plot(color=color, ax=ax, label=ctype, linewidth=4) # Make all lines thicker # Add title and legend to plot ax.legend() ax.set(title='Madera County Roads') ax.set_axis_off() plt.show()
與著色相同,先創(chuàng)建線條寬度與類型的映射關(guān)系,然后分組進(jìn)行循環(huán)繪制
# Create dictionary to map each attribute value to a line width lineWidths = {'M': 1, 'S': 1, 'C': 4, 'Unknown': .5} # Plot data adjusting the linewidth attribute fig, ax = plt.subplots(figsize=(10, 10)) ax.set_axis_off() for ctype, data in sjer_roads.groupby('RTTYP'): color = roadPalette[ctype] data.plot(color=color, ax=ax, label=ctype, # Assign each group to a line width using the dictionary created above linewidth=lineWidths[ctype]) ax.legend() ax.set(title='Madera County \n Line width varies by TYPE Attribute Value') plt.show()
在上面的實(shí)驗(yàn)中,使用label=True
顯示圖例,ax.legend()
的loc=
參數(shù)可以對(duì)圖例位置進(jìn)行調(diào)整,ax.legend()
的常用參數(shù)有:
loc=(how-far-right,how-far-above)
fontsize=
,設(shè)置圖例字體大小
frameon=
,是否顯示圖例邊框
lineWidths = {'M': 1, 'S': 2, 'C': 1.5, 'Unknown': 3} fig, ax = plt.subplots(figsize=(10, 10)) # Loop through each attribute value and assign each # with the correct color & width specified in the dictionary for ctype, data in sjer_roads.groupby('RTTYP'): color = roadPalette[ctype] label = ctype data.plot(color=color, ax=ax, linewidth=lineWidths[ctype], label=label) ax.set(title='Madera County \n Line width varies by TYPE Attribute Value') # Place legend in the lower right hand corner of the plot ax.legend(loc='lower right', fontsize=15, frameon=True) ax.set_axis_off() plt.show()
觀察當(dāng)將圖例frameon
屬性設(shè)置為False
并調(diào)整線寬時(shí)會(huì)發(fā)生什么情況,注意loc = ()
參數(shù)被賦予一個(gè)元組,它定義了圖例相對(duì)于繪圖區(qū)域的x
和y
的位置
lineWidths = {'M': 1, 'S': 2, 'C': 1.5, 'Unknown': 3} fig, ax = plt.subplots(figsize=(10, 10)) for ctype, data in sjer_roads.groupby('RTTYP'): color = roadPalette[ctype] label = ctype data.plot(color=color, ax=ax, linewidth=lineWidths[ctype], label=label) ax.set(title='Madera County \n Line width varies by TYPE Attribute Value') ax.legend(loc=(1, 0.5), fontsize=15, frameon=False, title="LEGEND") ax.set_axis_off() plt.show()
同時(shí)對(duì)線寬和顏色進(jìn)行調(diào)整
roadPalette = {'M': 'grey', 'S': "blue", 'C': "magenta", 'Unknown': "lightgrey"} lineWidths = {'M': 1, 'S': 2, 'C': 1.5, 'Unknown': 3} fig, ax = plt.subplots(figsize=(10, 10)) for ctype, data in sjer_roads.groupby('RTTYP'): color = roadPalette[ctype] label = ctype data.plot(color=color, ax=ax, linewidth=lineWidths[ctype], label=label) ax.set(title='Madera County Roads \n Pretty Colors') ax.legend(loc='lower right', fontsize=20, frameon=False) ax.set_axis_off() plt.show()
接下來(lái),向地圖添加另一個(gè)圖層,看看如何創(chuàng)建一個(gè)更復(fù)雜的地圖,添加SJER_plot_centroids shapefile,并同時(shí)表示兩個(gè)圖層的圖例
該點(diǎn)圖層包含三種類型:grass,soil,trees
# 導(dǎo)入點(diǎn)圖層 sjer_plots_path ="data/california/neon-sjer-site/vector_data/SJER_plot_centroids.shp" sjer_plots = gpd.read_file(sjer_plots_path) sjer_plots.head(5)
<div> <style scoped> .dataframe tbody tr th:only-of-type { vertical-align: middle; }
.dataframe tbody tr th { vertical-align: top; } .dataframe thead th { text-align: right; }
</style> <table border="1" class="dataframe"> <thead> <tr > <th></th> <th>Plot_ID</th> <th>Point</th> <th>northing</th> <th>easting</th> <th>plot_type</th> <th>geometry</th> </tr> </thead> <tbody> <tr> <th>0</th> <td>SJER1068</td> <td>center</td> <td>4111567.818</td> <td>255852.376</td> <td>trees</td> <td>POINT (255852.376 4111567.818)</td> </tr> <tr> <th>1</th> <td>SJER112</td> <td>center</td> <td>4111298.971</td> <td>257406.967</td> <td>trees</td> <td>POINT (257406.967 4111298.971)</td> </tr> <tr> <th>2</th> <td>SJER116</td> <td>center</td> <td>4110819.876</td> <td>256838.760</td> <td>grass</td> <td>POINT (256838.760 4110819.876)</td> </tr> <tr> <th>3</th> <td>SJER117</td> <td>center</td> <td>4108752.026</td> <td>256176.947</td> <td>trees</td> <td>POINT (256176.947 4108752.026)</td> </tr> <tr> <th>4</th> <td>SJER120</td> <td>center</td> <td>4110476.079</td> <td>255968.372</td> <td>grass</td> <td>POINT (255968.372 4110476.079)</td> </tr> </tbody> </table> </div>
就像上面所做的一樣,創(chuàng)建一個(gè)字典來(lái)指定與每個(gè)圖形類型相關(guān)聯(lián)的顏色
pointsPalette = {'trees': 'chartreuse', 'grass': 'darkgreen', 'soil': 'burlywood'} lineWidths = {'M': .5, 'S': 2, 'C': 2, 'Unknown': .5} fig, ax = plt.subplots(figsize=(10, 10)) for ctype, data in sjer_plots.groupby('plot_type'): color = pointsPalette[ctype] label = ctype data.plot(color=color, ax=ax, label=label, markersize=100) ax.set(title='Study area plot locations\n by plot type (grass, soil and trees)') ax.legend(fontsize=20, frameon=True, loc=(1, .1), title="LEGEND") ax.set_axis_off() plt.show()
接下來(lái),在道路圖層上疊加繪制點(diǎn)數(shù)據(jù),然后創(chuàng)建一個(gè)包含線和點(diǎn)的自定義圖例
注意: 在這個(gè)例子中,兩個(gè)圖層的投影信息必須匹配
# Reproject the data # 數(shù)據(jù)投影 sjer_roads_utm = sjer_roads.to_crs(sjer_plots.crs)
fig, ax = plt.subplots(figsize=(10, 10)) # 點(diǎn)圖層繪制 for ctype, data in sjer_plots.groupby('plot_type'): color = pointsPalette[ctype] label = ctype # label參數(shù)對(duì)于圖例的生成很重要 data.plot(color=color, ax=ax, label=label, markersize=100) # 道路圖層繪制 for ctype, data in sjer_roads_utm.groupby('RTTYP'): color = roadPalette[ctype] label = ctype data.plot(color=color, ax=ax, linewidth=lineWidths[ctype], label=label) ax.set(title='Study area plot locations\n by plot type (grass, soil and trees)') ax.legend(fontsize=15, frameon=False, loc=('lower right'), title="LEGEND") ax.set_axis_off() plt.show()
上述就是小編為大家分享的Python中怎么繪制矢量數(shù)據(jù)了,如果剛好有類似的疑惑,不妨參照上述分析進(jìn)行理解。如果想知道更多相關(guān)知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。