Set Matplotlib colorbar size to match graph

Elliot picture Elliot · Aug 12, 2013 · Viewed 139k times · Source

I cannot get the colorbar on imshow graphs like this one to be the same height as the graph, short of using Photoshop after the fact. How do I get the heights to match?Example of the colorbar size mismatch

Answer

skytaker picture skytaker · Nov 3, 2014

This combination (and values near to these) seems to "magically" work for me to keep the colorbar scaled to the plot, no matter what size the display.

plt.colorbar(im,fraction=0.046, pad=0.04)

It also does not require sharing the axis which can get the plot out of square.