[英]Geopandas plot shapefile on xarray with same legend
我正在嘗試創建一些降水數據地圖(xarray),頂部有感興趣區域的 shapefile。 但是,當 Python 繪制這些數字時,我得到兩個單獨的數字:
當我在 QGIS 中打開數據時,它們確實會出現在彼此之上,因此坐標系會檢查出來。 然后我有一個額外的獎勵問題:我必須創建多個降水圖,對於視覺分析,如果我可以為每張地圖擁有相同的圖例(因此顏色條的最小/最大值相同),那將是理想的。 有人知道如何進一步進行嗎?
到目前為止我的代碼:
def chirps_to_map(input1, input2, title):
projection = input1 + input2
plt.figure(figsize=(9, 9))
projection['pr'].plot()
watershed.plot()
plt.title(title)
plt.show()
plt.close()
projection.to_netcdf(str(path)+str(title)+".nc")
return projection
這是使用 Matplotlib 面向對象 API 更簡單的情況。
一個不錯的通用工作流程可能是
fig, ax = plt.subplot()
gdf.plot(ax=ax) # Plot the vector data on the subplot
raster.plot(ax=ax) # Plot the raster data on the same subplot
首先,我們得到一些樣本柵格+矢量數據
import xarray as xr
import geopandas as gpd
import matplotlib.pyplot as plt
da = xr.tutorial.load_dataset('ROMS_example').zeta.isel(ocean_time=0)
gdf = gpd.read_file(gpd.datasets.get_path('naturalearth_lowres'))
usa = gdf.loc[gdf['name'].eq('United States of America')]
接下來,我們在同一個AxesSubplot
上繪制兩個數據
fig, ax = plt.subplots(figsize=(15, 10))
da.plot.pcolormesh(x='lon_rho', y='lat_rho', ax=ax)
usa.plot(ax=ax, edgecolor='red', color='none')
# Focus on the raster extent
ax.set_xlim(-95, -87)
ax.set_ylim(26, 32)
hvPlot
方式 hvPlot為與pandas
、 xarray
和許多其他庫的交互式繪圖提供了一個很好的統一 API,並且可能對偶然發現這個答案的人感興趣。
繪制矢量和柵格數據相當容易,只需使用*
運算符。
import hvplot.pandas
import hvplot.xarray
usa.hvplot(geo=True) * da.hvplot.quadmesh(x='lon_rho', y='lat_rho', geo=True)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.