2012-04-24 59 views
2

我试图在使用pylab的subplot()函数进行排列的一些简单线图上绘制图像。但是,当我调用imshow时,似乎会改变子图的边界,即使使用set_position函数,我也无法更改这些边界。当覆盖图像时,pylab子图边界发生了变化

基本上,我想顶部的subplot与this image的底部宽度相同。

我试过关闭自动缩放按照this post和我没有区别。

这里是我的源:

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() 

回答

1

只要指定aspect='auto'imshow

默认情况下,imshow如果在imshow指定一个标量,以纵横kwarg将设置轴到1的纵横比(或不同数量。

例如

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

+0

当然它的长宽比 I认为这是我没有检查的财产。它总是你想到的最后一个。谢谢您的帮助。 – 2012-04-24 02:32:22

1

您可以在AX1顶部创建anonther斧:

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

+0

这很烦人,不得不重置所有的新轴的属性,否则这是一个很好的解决方法。谢谢你的帮助。 – 2012-04-24 02:06:46