unevenly centered subplots in matplotlib in Python?

Posted by user248237 on Stack Overflow See other posts from Stack Overflow or by user248237
Published on 2010-06-14T03:42:25Z Indexed on 2010/06/14 3:52 UTC
Read the original article Hit count: 366

Filed under:
|
|
|

I am plotting a simple pair of subplots in matplotlib that are for some reason unevenly centered. I plot them as follows:

plt.figure()
# first subplot
s1 = plt.subplot(2, 1, 1)
plt.bar([1, 2, 3], [4, 5, 6])
# second subplot
s2 = plt.subplot(2, 1, 2)
plt.pcolor(rand(5,5))

# add colorbar
plt.colorbar()

# square axes
axes_square(s1)
axes_square(s2)

where axes_square is simply:

def axes_square(plot_handle):
  plot_handle.axes.set_aspect(1/plot_handle.axes.get_data_ratio()) 

The plot I get is attached. The top and bottom plots are unevenly centered. I'd like their yaxis to be aligned and their boxes to be aligned.

If I remove the plt.colorbar() call, the plots become centered. How can I have the plots centered while the colorbar of pcolor is still shown? I want the axes to be centered and have the colorbar be outside of that alignment, either to the left or to the right of the pcolor matrix.

image of plots link

Image of uncentered subplots in matplotlib

thanks.

© Stack Overflow or respective owner

Related posts about python

Related posts about numpy