Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I adjust (offset) colorbar title in matplotlib

Given the following code:

imshow(np.arange(16*16).reshape(16,16)) cb = colorbar() cb.set_label("Foo") cb.set_ticks([0,255]) 

Which produces:

enter image description here

How do I adjust the colorbar text "Foo" so that it is offset to the left, betwen the 0 and 255, closer to the colorbar, reducing the un-needed whitespace?

like image 979
mankoff Avatar asked Jul 04 '13 17:07

mankoff


People also ask

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.

How do I shrink the Colorbar in Matplotlib?

To decrease colorbar width in Matplotlib, we can use shrink in colorbar() method.

How do you add a Colorbar to a subplot?

To have one color bar for all subplots with Python, we can use matplotlib's subplots_adjust and colorbar methods. to call subplots_adjust on the fig subplot with the right argument to adjust the position of the subplots.

What is Matplotlib Colorbar?

The colorbar() function in pyplot module of matplotlib adds a colorbar to a plot indicating the color scale. Syntax:matplotlib.pyplot.colorbar(mappable=None, cax=None, ax=None, **kwarg) Parameters: ax: This parameter is an optional parameter and it contains Axes or list of Axes.


2 Answers

cb.set_label("Foo", labelpad=-1) 

Negative labelpad values will move closer to the bar, positive away.

like image 187
bsf10 Avatar answered Sep 28 '22 12:09

bsf10


cb.set_label("Foo",horizontalalignment='right') 

The label control with this function is very poor...


You could do:

cb = colorbar() cb.set_ticks([0,255]) ax = cb.ax ax.text(1.3,0.5,'Foo',rotation=90) 
like image 43
Pablo Avatar answered Sep 28 '22 11:09

Pablo