繁体   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