Python未在图中正确设置范围

时间:2014-02-15 02:40:37

标签: python plot ipython

最令人沮丧的事情之一就是当你按照书上的教程进行操作时,它不会按预期的方式工作。我不知道为什么这不起作用。 我现在正在使用IPython制作我的情节。我有这段代码:

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等......

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

1 个答案:

答案 0 :(得分: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)

Afaik,ax.plot()没有xmin等关键字参数,因此它们不会被注意到plot方法。你看到的是上一个情节ax.plot(t,x,'o-')的结果。


设置限制的第二种方法是link you indicateplt.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。