2

I'm trying to stick an image on top of some simple line plots which are arranged using the subplot() function of pylab. However when I call imshow it appears that the bounds of the subplot are altered and I am unable to change these bounds even using the set_position function.

Basically, I would like the top subplot to be the same width as the bottom in this image.

I have tried turning off autoscale as per this post and I get no difference.

Here's my source:

import pylab as pl

#Plotting results
F=pl.figure()

#First plot the unzoomed plot
ax1=pl.subplot(211)
ax2=pl.subplot(212)

#Not relevant to problem... ax1.plot() & ax2.plot() commands
for bl in range(len(bondLengths)):
    ls=styles[bl]
    lw=widths[bl]
    for cf in range(len(chgcarfiles)):
        c=colors[cf]
        avgi=avgIBLs[cf][bl]
        L=len(avgi)
        ax1.plot([bondLengths[bl]*(x+0.5)/L for x in range(-1,L/2,1)],avgi[len(avgi)/2-1:],c=c,ls=ls,lw=lw)
        ax2.plot([bondLengths[bl]*(x+0.5)/L for x in range(-1,L/2,1)],avgi[len(avgi)/2-1:],c=c,ls=ls,lw=lw)

ax1.set_xlim([0,2.5])
ax1.set_ylim([0.5,4.9])
ax2.set_xlim([0,1.2])
ax2.set_ylim([0.88,0.96])

#Load up & insert an image
slice=pl.loadtxt("somedata1")
ax1.autoscale(False)
ax1.imshow(slice,extent=[0.05,0.75,3.4,4.1])    

pl.figtext(0.45,0.03,r"Distance ($\AA$)")
pl.figtext(0.05,0.65,r"Partial Charge Density ($\rho / rho_{avg}$)",rotation='vertical')

pl.show()
Community
  • 1
  • 1
Adam Cadien
  • 1,137
  • 9
  • 19

2 Answers2

1

You can create anonther axe on top of ax1:

import pylab as pl

F=pl.figure()

ax1=pl.subplot(211)
ax2=pl.subplot(212)

ax1.plot(pl.randn(100))
ax2.plot(pl.randn(100))

img = pl.randn(100, 100)
ax3 = pl.axes([0.2, 0.65, 0.2, 0.2])
ax3.imshow(img)

pl.show()

enter image description here

HYRY
  • 94,853
  • 25
  • 187
  • 187
  • It's annoying having to reset all the properties for the new axes, otherwise this is a great work around. Thanks for the help. – Adam Cadien Apr 24 '12 at 02:06
1

Just specify aspect='auto' to imshow.

By default, imshow will set the aspect ratio of the axes to 1 (or a different number if you specify a scalar to the aspect kwarg in imshow.

E.g.

import matplotlib.pyplot as plt
import numpy as np

fig, axes = plt.subplots(nrows=2)

for ax in axes:
    ax.plot(np.random.random(100))

axes[1].autoscale(False)
imdata = np.random.random((10,10))
axes[1].imshow(imdata, aspect='auto', extent=[5, 20, 0.3, 0.8])

plt.show()

enter image description here

Joe Kington
  • 275,208
  • 71
  • 604
  • 463
  • Of course its the aspect ratio I think that is the one property I didn't check. Its always the last one you think of. Thanks for the help. – Adam Cadien Apr 24 '12 at 02:32