2014-02-15 32 views
2

最令人沮丧的事情之一是,当您按照书上的教程进行操作时,它无法正常工作。我不知道为什么这不起作用。 我现在使用IPython来制作我的情节。我有这样的代码:Python在剧情中没有正确设置范围

from __future__ import division 
fig,ax =subplots() 
f=1.0# Hz, signal frequency 
fs = 5.0 # Hz, sampling rate (ie. >= 2*f) 
t= arange(-1,1+1/fs,1/fs) # sample interval, symmetric 
# for convenience later 
x= sin(2*pi*f*t) 
ax.plot(t,x,'o-') 
ax.set_xlabel('Time' ,fontsize=18) 
ax.set_ylabel(' Amplitude' ,fontsize=18) 

这样做具有以下图表:

enter image description here

预计书中的图表。但后来当我添加此额外的代码:

fig,ax = subplots() 
ax.plot(t,x,'o-') 
ax.plot(xmin = 1/(4*f)-1/fs*3, 
     xmax = 1/(4*f)+1/fs*3, 
     ymin = 0, 
     ymax = 1.1) 
ax.set_xlabel('Time',fontsize =18) 
ax.set_ylabel('Amplitude',fontsize = 18) 

我也得到在同一张图,即使我设置了图形范围。 我试着按参数做参数,因为我发现在另一个问题 - ax.ymin = 0,ax.ymax = 1.1等....

为什么会发生这种情况,我该怎么做查看“放大”图?

+0

从哪个教程复制了第二段代码? A会说ax.plot()没有xmin,xmax等参数。 – joaquin

+0

来自“python for signal processing”一书,采样定理部分。链接:http://python-for-signal-processing.blogspot.com/2012/09/investigating-sampling-theorem-in-this.html 书中的代码与网站中的代码有点不同,但他们都没有工作。 – triplebig

+0

我认为你错误地复制了该链接中的代码。它说'axis(xmin =,....)'不是'plot(xmin =,...)'。 Probalby你不应该责怪该教程,但别人... – joaquin

回答

3

要设置轴的限制,您可能需要使用ax.set_xlimax.set_ylim

fig, ax = subplots() 
ax.plot(t, x, 'o-') 
ax.set_xlim(1/(4*f)-1/fs*3, 1/(4*f)+1/fs*3) 
ax.set_ylim(0, 1.1) 
ax.set_xlabel('Time',fontsize =18) 
ax.set_ylabel('Amplitude',fontsize = 18) 

据我所知,ax.plot()没有xmin等关键字参数,使他们不被察觉到plot方法。你看到的是前面阴谋的结果ax.plot(t,x,'o-')


设置限制的第二种方法中,一个在link you indicate,是plt.axis()

axis(*v, **kwargs) 
Convenience method to get or set axis properties. 

Calling with no arguments:: 

    >>> axis() 

returns the current axes limits ``[xmin, xmax, ymin, ymax]``.:: 

    >>> axis(v) 

sets the min and max of the x and y axes, with 
``v = [xmin, xmax, ymin, ymax]``.:: 

axis同时设置两个轴。有关更多信息,请参阅docstring。

+0

你的建议奏效了。我试了一下,发现这个命令: ax.plot(xlim(1 /(4 * f)-1/fs * 3,1 /(4 * f)+ 1/fs * 3), ylim(0,1.1)) 为我提供了我想要的结果,但是有一条奇怪的线{y = x}越过图,所以它似乎确实有某种类似的参数。任何线索在那个例子中发生了什么? – triplebig

+1

不,你没有设定极限,而是绘制两种方法的结果,即:plot((xmin,xmax),(ymin,ymax))' – joaquin