When I plot a matrix with a colorbar, then the colorbar has 10 ticks. Since the colorbar has to be pretty small, the ticklabels overlap. Therefore I want to reduce the number of ticks from 10 to 5. I do not want to reduce the font size!
Is there an easy way to do this? I do not want to set the ticks manually...
Display the data as an image, i.e., on a 2D regular raster. Make a colorbar using colorbar() method with an image scalar mappable object. Set the ticks and tick labels of the colorbar using set_ticks() and set_ticklabels() methods. To display the figure, use show() method.
Method 2: Change and Rotate the position of the label To rotate the colorbar labels we will use set_xticklabels() and set_yticklabels() methods for horizontal and vertical.
The MaxNLocator ticker might suit your purposes?
class matplotlib.ticker.MaxNLocator
Select no more than N intervals at nice locations
For example:
from matplotlib import ticker # (generate plot here) cb = plt.colorbar() tick_locator = ticker.MaxNLocator(nbins=5) cb.locator = tick_locator cb.update_ticks() plt.show()
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