您好,登錄后才能下訂單哦!
這期內容當中小編將會給大家帶來有關Python中怎么繪制矢量數據,文章內容豐富且以專業的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。
學習目標:
為多個矢量數據集繪制地圖,并根據屬性進行配色
自定義地圖圖例
在本節中,將學習如何自定義地圖符號和用于在Python中表示矢量數據的顏色和符號,使用geopandas和matplotlib進行地圖繪制
首先導入需要使用到的包:
import os import matplotlib.pyplot as plt import numpy as np from shapely.geometry import box import geopandas as gpd import earthpy as et
# 下載數據 # data = et.data.get_data('spatial-vector-lidar') os.chdir(os.path.join(et.io.HOME, 'learning','python_data_plot'))
# 導入數據 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']
可以看出道路類型中有一些缺失的值,由于需要繪制所有的道路類型,甚至那些設置為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()
繪制數據,當設置了column =
參數后,則geopandas將為線條自動選擇顏色,可以使用legend = True
參數添加圖例
fig, ax = plt.subplots(figsize=(14,6)) sjer_roads.plot(column='RTTYP', categorical=True, legend=True, ax=ax ) # 調整圖例位置 leg = ax.get_legend() leg.set_bbox_to_anchor((1.15,0.5)) # 隱藏邊框 ax.set_axis_off() plt.show()
為了按屬性值繪制一個矢量圖層,這樣每條道路圖層就會根據它各自的屬性值來著色,所以圖例也代表了同樣的符號,需要三個步驟:
創建一個將特定顏色與特定屬性值關聯的字典
循環遍歷并將該顏色應用于每個屬性值
最后,在繪圖中添加一個label
參數,以便調用ax.legend()
生成最終的圖例
下面,先創建一個字典來定義您希望使用哪種顏色繪制每種道路類型:
# 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'}
接下來,循環遍歷每個屬性值,并使用字典中指定的顏色用該屬性值繪制線條
fig, ax = plt.subplots(figsize=(10,10)) # 根據道路類型分組進行繪制 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()
可以通過linewidth =
屬性對線條寬度進行設置,
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()
與著色相同,先創建線條寬度與類型的映射關系,然后分組進行循環繪制
# 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()
在上面的實驗中,使用label=True
顯示圖例,ax.legend()
的loc=
參數可以對圖例位置進行調整,ax.legend()
的常用參數有:
loc=(how-far-right,how-far-above)
fontsize=
,設置圖例字體大小
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()
觀察當將圖例frameon
屬性設置為False
并調整線寬時會發生什么情況,注意loc = ()
參數被賦予一個元組,它定義了圖例相對于繪圖區域的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()
同時對線寬和顏色進行調整
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()
接下來,向地圖添加另一個圖層,看看如何創建一個更復雜的地圖,添加SJER_plot_centroids shapefile,并同時表示兩個圖層的圖例
該點圖層包含三種類型:grass,soil,trees
# 導入點圖層 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>
就像上面所做的一樣,創建一個字典來指定與每個圖形類型相關聯的顏色
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()
接下來,在道路圖層上疊加繪制點數據,然后創建一個包含線和點的自定義圖例
注意: 在這個例子中,兩個圖層的投影信息必須匹配
# Reproject the data # 數據投影 sjer_roads_utm = sjer_roads.to_crs(sjer_plots.crs)
fig, ax = plt.subplots(figsize=(10, 10)) # 點圖層繪制 for ctype, data in sjer_plots.groupby('plot_type'): color = pointsPalette[ctype] label = ctype # label參數對于圖例的生成很重要 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中怎么繪制矢量數據了,如果剛好有類似的疑惑,不妨參照上述分析進行理解。如果想知道更多相關知識,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。