在 matplotlib 颜色栏中隐藏每个第 n 个刻度标签的最干净方法? [英] Cleanest way to hide every nth tick label in matplotlib colorbar?
本文介绍了在 matplotlib 颜色栏中隐藏每个第 n 个刻度标签的最干净方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的水平颜色条上的标签靠得太近,我不想进一步减小文本大小:
The labels on my horizontal colorbar are too close together and I don't want to reduce text size further:
cbar = plt.colorbar(shrink=0.8, orientation='horizontal', extend='both', pad=0.02)
cbar.ax.tick_params(labelsize=8)
我想保留所有刻度,但删除所有其他标签.
I'd like to preserve all ticks, but remove every other label.
我发现的大多数示例都将用户指定的字符串列表传递给 cbar.set_ticklabels().我正在寻找一个通用的解决方案.
Most examples I've found pass a user-specified list of strings to cbar.set_ticklabels(). I'm looking for a general solution.
我玩弄了
cbar.set_ticklabels(cbar.get_ticklabels()[::2])
和
cbar.ax.xaxis.set_major_locator(matplotlib.ticker.MaxNLocator(nbins=4))
但我还没有找到神奇的组合.
but I haven't found the magic combination.
我知道使用定位器对象必须有一种干净的方法来做到这一点.
I know there must be a clean way to do this using a locator object.
推荐答案
For 循环标记标签,并调用 set_visible()
:
For loop the ticklabels, and call set_visible()
:
for label in cbar.ax.xaxis.get_ticklabels()[::2]:
label.set_visible(False)
这篇关于在 matplotlib 颜色栏中隐藏每个第 n 个刻度标签的最干净方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文