Cleanest way to hide every nth tick label in matplotlib colorbar?
For loop the ticklabels, and call set_visible()
:
for label in cbar.ax.xaxis.get_ticklabels()[::2]:
label.set_visible(False)
One-liner for those who are into that!
n = 7 # Keeps every 7th label
[l.set_visible(False) for (i,l) in enumerate(ax.xaxis.get_ticklabels()) if i % n != 0]
Just came across this thread, nice answers. I was looking for a way to hide every tick between the nth ticks. And found the enumerate function. So if anyone else is looking for something similar you can do it like this.
for index, label in enumerate(ax.xaxis.get_ticklabels()):
if index % n != 0:
label.set_visible(False)