簡體   English   中英

從 map plot 中刪除顏色條

[英]Remove colorbar from map plot

我有一個 plot 的 map 包含顏色編碼的區域。 我需要從 plot 中刪除自動顏色條並替換為圖例。 但是,我找不到刪除顏色條的方法。 Xarray 用於創建數據數組。

map_crs = ccrs.OSGB()
fig = plt.figure(figsize=(10, 15))
cmap = mpl.colors.ListedColormap(colours)
norm = mpl.colors.BoundaryNorm(boundaries=bins, ncolors=len(cmap.colors)-1 )
stamen_terrain = cigmt.Stamen('terrain')
ax = plt.axes(projection=stamen_terrain.crs)

data_array.plot(transform=map_crs, vmin=0, vmax=np.max(data_array), cmap=cmap)

plt.gca().coastlines()
plt.tight_layout()
plt.savefig(plotname, bbox_inches="tight", pad_inches=0.1)
plt.clf()

我嘗試添加命令,例如colorbar=Falsecbar=FalseColorbar=False 但是,只是不斷收到相應的錯誤;

AttributeError: 'QuadMesh' object has no property 'Colorbar'

關於如何擺脫顏色條的任何想法?

首先,使用 mpl.legend.Legend function 創建圖例,然后使用 add_artist function 將圖例添加到 plot。最后,通過將 data_array object 的 colorbar 屬性設置為 None 來移除自動顏色條

map_crs = ccrs.OSGB()
fig = plt.figure(figsize=(10, 15))
cmap = mpl.colors.ListedColormap(colours)
norm = mpl.colors.BoundaryNorm(boundaries=bins, ncolors=len(cmap.colors)-1 )
stamen_terrain = cigmt.Stamen('terrain')
ax = plt.axes(projection=stamen_terrain.crs)

# Create a patch for each color in the color map
patches = [mpl.patches.Patch(color=color, label=f'{label:.1f} - {label + interval:.1f}') for label, color in zip(bins, colours)]

# Create the legend with the patches
legend = mpl.legend.Legend(ax, patches, title='Value Range')

# Add the legend to the plot
ax.add_artist(legend)

# Remove the automatic color bar
data_array.colorbar = None

data_array.plot(transform=map_crs, vmin=0, vmax=np.max(data_array), cmap=cmap)

plt.gca().coastlines()
plt.tight_layout()
plt.savefig(plotname, bbox_inches="tight", pad_inches=0.1)
plt.clf()

完畢!

只需要將add_colorbar=False放入繪圖命令中。

現在添加圖例!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM