Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to shift the colorbar position to right in matplotlib?

I draw a scatter chart as below : enter image description here

The code is :

sc = plt.scatter(x, y, marker='o', s=size_r, c=clr, vmin=lb, vmax=ub, cmap=mycm, alpha=0.65)
cbar = plt.colorbar(sc, shrink=0.9)

And I want to shift the colorbar to right a little bit to extend the drawing area. How to do that ?

like image 206
bigbug Avatar asked Mar 11 '13 02:03

bigbug


People also ask

How do I change the location of my color bar?

To move the colorbar to a different tile, set the Layout property of the colorbar. To display the colorbar in a location that does not appear in the table, use the Position property to specify a custom location. If you set the Position property, then MATLAB sets the Location property to 'manual' .

What is the Matplotlib Pyplot function that plots a Colorbar on the side of a plot?

The colorbar() function in pyplot module of matplotlib adds a colorbar to a plot indicating the color scale.


2 Answers

Use the pad attribute.

cbar = plt.colorbar(sc, shrink=0.9, pad = 0.05)

The documentation of make_axes() describes how to use pad: "pad: 0.05 if vertical, 0.15 if horizontal; fraction of original axes between colorbar and new image axes".

like image 102
Robbert Avatar answered Nov 06 '22 01:11

Robbert


Actually you can put the colorbar anywhere you want.

fig1=figure()
sc = plt.scatter(x, y, marker='o', s=size_r, c=clr, vmin=lb, vmax=ub, cmap=mycm, alpha=0.65)
position=fig1.add_axes([0.93,0.1,0.02,0.35])  ## the parameters are the specified position you set 

fig1.colorbar(sc,cax=position) ## 
like image 25
Zipo Avatar answered Nov 06 '22 03:11

Zipo