PCOLORMESH颜色棒定制刻度位置

问题描述 投票:0回答:1
# Add a colorbar with commune names cbar = plt.colorbar(mesh, ax=ax, ticks=np.arange(1, len(commune_names) + 1)) # Set the tick labels to the commune names cbar.set_ticklabels(commune_names)

我测试了许多事情,也使用了AI,但无法弄清楚。 # Add a colorbar with commune names cbar = plt.colorbar(mesh, ax=ax) # Set up tick labels and positions k = len(commune_names) labels = np.arange(1, k + 1) # Commune IDs, from 1 to k loc = labels - 0.5 # Shift ticks by 0.5 to center them in the color segments # Set the ticks and labels cbar.set_ticks(loc) cbar.set_ticklabels(commune_names) # Adjust the tick size (optional) cbar.ax.tick_params(labelsize=10)

感谢您的帮助。
	

I确实找到了使用虚拟案例(和AI)的解决方案,请参见下面的示例,其中五个点(网格单元)用PcoLormesh绘制。 我错过了以下内容:

from matplotlib.colors import ListedColormap, BoundaryNorm
...

bounds = np.arange(0.5, 6.5, 1)  # Boundaries between colors
norm = BoundaryNorm(bounds, cmap.N)
...

c = ax.pcolormesh(lon, lat, data, cmap=cmap, norm=norm, shading='auto')

对于定义界限和规范是必要的。

position colorbar pcolormesh
1个回答
0
投票
最新问题
© www.soinside.com 2019 - 2025. All rights reserved.