How to shift the colorbar position to right in matplotlib?

bigbug picture bigbug · Mar 11, 2013 · Viewed 19.4k times · Source

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 ?

Answer

Robbert picture Robbert · Mar 11, 2013

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