隐藏matplotlib颜色栏中的每个第n个刻度标签的最干净方法? [英] Cleanest way to hide every nth tick label in matplotlib colorbar?

查看:147
本文介绍了隐藏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.

我知道必须有一种使用locator对象的干净方法.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆