2014-02-15 35 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