2012-02-02 117 views
21

我一直在试图将一个椭圆绘制到imshow图中。它的工作原理,但图像绘制之后绘制椭圆似乎增加XLIM和ylim,导致边界,我想它摆脱:matplotlib:在同一轴上使用plot和imshow时的限制

注意,有直接NO白色边框仅在调用imshow之后。

我的代码如下:

self.dpi = 100 
self.fig = Figure((6.0, 6.0), dpi=self.dpi) 
self.canvas = FigureCanvas(self.fig) 
self.canvas.setMinimumSize(800, 400) 
self.cax = None 
self.axes = self.fig.add_subplot(111) 
self.axes.imshow(channel1, interpolation="nearest") 
self.canvas.draw() 
self.axes.plot(dat[0], dat[1], "b-") 

我已经尝试过,并呼吁“暗算”后设置的限制,没有效果

# get limits after calling imshow 
xlim, ylim = pylab.xlim(), pylab.ylim() 
... 
# set limits before/after calling plot 
self.axes.set_xlim(xlim) 
self.axes.set_ylim(ylim) 

我怎么能强迫情节不增加现有数字限制?

解决方案(感谢乔):

#for newer matplotlib versions 
self.axes.imshow(channel1, interpolation="nearest") 
self.axes.autoscale(False) 
self.axes.plot(dat[0], dat[1], "b-") 

#for older matplotlib versions (worked for me using 0.99.1.1) 
self.axes.imshow(channel1, interpolation="nearest") 
self.axes.plot(dat[0], dat[1], "b-", scalex=False, scaley=False) 

回答

29

发生了什么事是,轴自动缩放,以匹配每一个项目,你图的范围。图像自动缩放比线等要紧得多(imshow基本上称为ax.axis('image'))。

之前获取轴极限值并设置它们应该已经工作。 (虽然之前只做limits = axes.axis()axes.axis(limits)之后更干净。)

但是,如果您不想自动缩放,最好在初始绘图之后关闭自动缩放。绘制图像后尝试axes.autoscale(False)

作为一个例子,比较这:

import matplotlib.pyplot as plt 
import numpy as np 

fig, ax = plt.subplots() 
ax.imshow(np.random.random((10,10))) 
ax.plot(range(11)) 
plt.show() 

enter image description here


有了这个:

import matplotlib.pyplot as plt 
import numpy as np 

fig, ax = plt.subplots() 
ax.imshow(np.random.random((10,10))) 
ax.autoscale(False) 
ax.plot(range(11)) 
plt.show() 

enter image description here

+3

乔您好,感谢您的详细EXP lanation!我的matplotlib版本似乎太旧了(matplotlib .__ version__ ='0.99.1.1'),因为它既不支持plt.subplots()也不支持ax.autoscale,但是当您指出autoscale = False时,我发现了[替代解决方案]( http://stackoverflow.com/questions/7386872/make-matplotlib-autoscaling-ignore-some-of-the-plots)修复了我的问题:每次我在最初调用imshow之后使用plot时,我都使用关键字参数scalex =假,scaley = False,这是正确的!谢谢! – soramimo 2012-02-03 20:30:22