Cleanest way to hide every nth tick label in matplotlib colorbar?
matplotlib, python
Solution
For loop the ticklabels, and call `set_visible()`:
for label in cbar.ax.xaxis.get_ticklabels()[::2]:
label.set_visible(False)
Problem
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. Most examples I've found pass a user-specified list of strings to cbar.set_ticklabels(). I'm looking for a general solution. I played around with variations of ``` cbar.set_ticklabels(cbar.get_ticklabels()[::2]) ``` and ``` 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.