Python/C2/Using-the-plot-command-interactively/English

From Script | Spoken-Tutorial
Jump to: navigation, search
Visual Cue Narration
Show Slide 1

containing title, name of the production team along with the logo of MHRD

Hello Friends and welcome to the tutorial on creating simple plots using iPython. I hope you have IPython running on your computer.
Show Slide 2

Learning objective

At the end of this tutorial, you will be able to,
  1. Create simple plots of mathematical functions.
  2. Use the Figure window to study plots better.


ipython -pylab Lets start ipython. Open the terminal and type ipython -pylab and hit enter.
Show Slide 3

Slide with Error written on it

Pylab is a python library which provides plotting functionality. It provides many other important mathematical and scientific functions. After running IPython -pylab in the shell you will see some information about ipython and pylab followed by the In[1] prompt. But if you get an error like
`ERROR: matplotlib could NOT be imported!  Starting normal
   IPython.`

Then you will have to install the matplotlib and run this command again.

Switch to terminal
linspace?
Now type 'linspace' followed by a '?' mark in your ipython shell
linspace(1,100,100) as the documentation says, it returns num evenly spaced samples, calculated over the interval start and stop. To illustrate this, lets try to generate 100 points. Type linspace(1,100,100) and hit enter. As you can see a sequence of numbers from 1 to 100 appears.
linspace(0,1,200) Now lets try to generate 200 points between 0 and 1,we do that by typing linspace(0,1,200).
p = linspace(-pi,pi,100) Here,0 is the start , 1 the stop and 200 the number of points. In linspace the start and stop points can be integers, decimals , or constants. Let's try and get 100 points between -pi to pi. Here 'pi' is a constant defined by pylab. Save this to the variable,say p.
len(p) If we now type len(p)``we will get the no. of points. ``len function gives the no of elements of a sequence.
plot(p,cos(p)) Let's try and plot a cosine curve between -pi and pi. For this we use the plot command. Here cos(p) gets the cosine value at every point corresponding to point p.
cosine=cos(p)
plot(p,cosine)
We can save cos(p) to variable cosine and then plot it using the plot function.
clf() Now to clear the plot ,we use the clf() function
plot(p,sin(p)) This is done because if we wish to make another plot, it will overlap the previous plot. As we do not wish to clutter the area with overlaid plots , we just clear it with clf(). Now lets try a sine plot.
Show Slide 4

'Plot UI'

We can study the plot better on the plot window by using the various options available on it. Let us have a look at these options.
Move the mouse along the plot As we can observe, moving the mouse pointer along the plot gives us the location of each point on the plot
Save the plot as sin_curve in pdf format To the bottom left of the window,there are a few buttons. The right most among them is for saving the file. Just click on it and type the file name. We will save the plot by the name sin_curve in pdf format. As you can see we can specify the format of file from the dropdown. Formats like png ,eps ,pdf, ps are available.
Point the mouse on the slider button Left to the save button is the slider button by which we can specify the margins.
Show how to zoom. Press zoom button and specify region to zoom Left to this is the zoom button by which we can zoom into the plot. Just specify the region to zoom into.
Press Move button and move the axes. The button to the left of it can be used to move the axes of the plot.
Press Back and Forward Button The next two buttons with left and right arrow icons change the state of the plot and take it to the previous state it was in. It more or less acts like the back and forward button in a browser.
Press home button The last one is 'home' referring to the initial plot.
Show Slide 5

Assignment 1

Pause the video here, try out the following exercise and resume the video.

Plot (sin(x)*sin(x))/x.

  1. Save the plot by the sinsquarebyx.pdf in pdf format.
  2. Zoom and find the maxima.
  3. Bring it back to initial position.


Show Slide 6

Summary Slide

This brings us to the end of this tutorial.In this tutorial,we have learnt to,
  1. Start Ipython with pylab.
  2. Use the linspace function to create num equally spaced points in a region.
  3. Find the length of sequences using len function.
  4. Plot mathematical functions using plot.
  5. Clear drawing area using clf.
  6. Use the UI of plot for studying it better and using functionalities like save,zoom and moving the plots on x and y axis.


Show Slide 7

Self assessment questions slide

Here are some self assessment questions for you to solve
  1. Create 100 equally spaced points between -pi/2 and pi/2?
  2. What will the command linspace(-pi,pi,100) do. - returns 100 evenly spaced samples from -pi to pi - returns 100 evenly spaced samples from -pi to pi excluding pi but
    Unexpected indentation.
    including -pi
    Block quote ends without a blank line; unexpected unindent.
    • returns 100 evenly spaced samples from -pi to pi excluding -pi but including pi
    • returns 100 evenly spaced samples from -pi to pi including both -pi and pi
  1. How do you find the length of a sequence?


Show Slide 8

Solution of self assessment questions slide

And the answers,
  1. We use the command linspace(-pi/2,pi/2,100) to create 100 eually spaced lines between the points -pi/2 and pi/2.
  2. The command linspace(-pi,pi,100) will return 100 evenly spaced samples from -pi to pi including both -pi and pi.
  3. len(sequence_name) is the function used to find out the length of a sequence.


Show Slide 9

Acknowledgment slide

Hope you have enjoyed and found it useful. Thank you!

Contributors and Content Editors

Chandrika, PoojaMoolya, Pravin1389