3

I want to have two plots be the same width, however the resulting code shrinks the imshow plot.

xx = np.linspace(0.0,255.5,512)
yy = np.linspace(0.0,255.5,512)
Func = np.random.rand(len(xx),len(yy))

f, axarr = plt.subplots(2,1)
f.tight_layout()

im = axarr[0].imshow(Func, cmap = 'jet', interpolation = 'lanczos',origin = 'lower')
pos = axarr[0].get_position()
colorbarpos = [pos.x0+1.05*pos.width,pos.y0,0.02,pos.height]
cbar_ax = f.add_axes(colorbarpos)
cbar = f.colorbar(im,cax=cbar_ax)

axarr[1].plot(xx,Func[:,255],yy,Func[255,:])

plt.show()
plt.close('all')

EDIT: I would also like to keep imshow's plot from looking stretched (essentially, I need the width and length stretched appropriately so the aspect ratio's are still equal).

4
  • Add aspect='auto' to the parameters of imshow. Commented Apr 12, 2017 at 15:06
  • This stretches the plot, I guess I need to edit my question, I would like it to be stretched to agree in length on x, but have the correct aspect be maintained. Commented Apr 12, 2017 at 15:08
  • This would require the height ratios betzween the plots to be different, so you either need a larger figure or you need to shrink the second plot. Is that what you want? Commented Apr 12, 2017 at 15:22
  • I would prefer a larger figure rather than shrinking the second plot. Commented Apr 12, 2017 at 15:27

1 Answer 1

6

Some options:

A. `aspect="auto"

Use `aspect="auto" on the imshow plot

    plt.imshow(...,  aspect="auto")

enter image description here

B. adjust the figure margings

Adjust the figure margings or the figure size, such that the lower axes will have the same size as the imshow plot, e.g.

    plt.subplots_adjust(left=0.35, right=0.65)

enter image description here

C. using a divider

You can use make_axes_locatable functionality from mpl_toolkits.axes_grid1 to divide the image axes to make space for the other axes.

import matplotlib.pyplot as plt
from mpl_toolkits.axes_grid1 import make_axes_locatable
import numpy as np

xx = np.linspace(0.0,255.5,512)
yy = np.linspace(0.0,255.5,512)
Func = np.random.rand(len(xx),len(yy))

fig, ax = plt.subplots(figsize=(4,5))

im = ax.imshow(Func, cmap = 'jet', interpolation = 'lanczos',origin = 'lower')

divider = make_axes_locatable(ax)
ax2 = divider.append_axes("bottom", size=0.8, pad=0.3)
cax = divider.append_axes("right", size=0.08, pad=0.1)

ax2.plot(xx,Func[:,255],yy,Func[255,:])
cbar = fig.colorbar(im,cax=cax)

plt.show()

enter image description here

Sign up to request clarification or add additional context in comments.

2 Comments

I edited my question slightly, either solution stretches and what I really want is to make imshow larger while keeping its aspect ratio equal.
Just wanted to add that option C works excellently for all flavors of plots, not just imshow. Excellent post.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.