2

One of the most frustrating things is when you follow a tutorial on a book, and it doesn't work the way it should. I have little idea as to why this is not working. I'm using IPython right now to make my plots. I have this code:

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)

Which gives the following graph:

enter image description here

Which is the graph expected in the book. But then when I add this additional code:

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)

And I get the same graph, even though I'm setting the graph range. I've tried doing it parameter by parameter, as I found in another question - ax.ymin = 0, ax.ymax = 1.1 , etc....

Why is this happening, and what can I do to view the "zoomed in" graph?

5
  • From which tutorial did you copied the second piece of code ? A would say ax.plot() doesnt have xmin, xmax, etc parameters. Commented Feb 15, 2014 at 2:58
  • From "python for signal processing" book, sampling theorem part. Link: python-for-signal-processing.blogspot.com/2012/09/… The code in the book is a bit different from the one in the website, but neither of them work. Commented Feb 15, 2014 at 3:10
  • I think you copied wrongly the code in that link. It says axis(xmin=, ....) not plot(xmin=,...). Probalby you should not blame the tutorial but someone else... Commented Feb 15, 2014 at 3:14
  • The code I posted is control C+ control V from the book I have in pdf format. The blog I posted I found later, googling the book to see if could post here, where the author seemed to have corrected himself. I blame the book! Commented Feb 15, 2014 at 3:17
  • According to the blog, all of the material is available as downloadable IPython notebooks from the indicated site. So, you could also just use the IPython notebooks directly and save yourself some typing. Commented Feb 17, 2014 at 22:14

1 Answer 1

3

To set limits to the axes you may want to use ax.set_xlim and ax.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() doesn't have xmin, etc keyword arguments, so that they pass unnoticed to the plot method. What you see is the result from the previous plot ax.plot(t,x,'o-').


A second method to set limits, the one in the link you indicate, is 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 set both axis at the same time. see docstring for more information.

Sign up to request clarification or add additional context in comments.

2 Comments

Your suggestion worked. I experimented around a bit, and found that this command: ax.plot(xlim(1/(4*f)-1/fs*3,1/(4*f)+1/fs*3), ylim(0,1.1) ) Gives me the result I want, but with a weird line {y = x} crossing the graph, so it seems that it does have some sort of similar parameters. Any clue what's happened in that example?
No, you are not setting the limits but plotting the result of both methods, that is: plot((xmin,xmax), (ymin,ymax))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.