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.
To hide or remove X-axis labels, use set(xlabel=None). To display the figure, use show() method.
MatPlotLib with Python To display all label values, we can use set_xticklabels() and set_yticklabels() methods.
For loop the ticklabels, and call set_visible()
:
for label in cbar.ax.xaxis.get_ticklabels()[::2]: label.set_visible(False)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With