Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set number of ticks in plt.colorbar?

Tags:

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...

like image 750
FrankTheTank Avatar asked Feb 25 '14 10:02

FrankTheTank


People also ask

How do you put a tick on Colorbar?

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.

How do you change the color bar label in Matplotlib?

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.


1 Answers

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() 
like image 169
Bonlenfum Avatar answered Sep 18 '22 18:09

Bonlenfum