admin管理员组文章数量:1244304
It seems pcolormesh have a different way of handling colorbar tick position and label than other Python tools. I can't get my labels at the desired position : centered by color segment (see image: current rendering where the ticks are misaligned).
# 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)
I tested many things, also using AI, but couldn't figure it out.
# 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)
Thanks for your help.
本文标签: colorbarpcolormesh color bar customed ticks positionStack Overflow
版权声明:本文标题:colorbar - pcolormesh color bar customed ticks position - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1740121801a2228123.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论