Hello friends! Welcome to the tutorial on using plot command interactively in IPython.
At the end of this tutorial, you will be able to: create simple plots of mathematical functions, use the Plot window to study plots better.
To record this tutorial, I am using: Ubuntu Linux 14.04 operating system, Python 3.4.3 and IPython 5.1.0
Pre-requisites to practise this tutorial are - you should know how to run basic Python commands on the ipython console.
If not, for relevant Python tutorials, please visit this website.
Let us first open the Terminal by pressing Ctrl+Alt+T keys simultaneously.
Now, type ipython3 and press Enter.
Let us initialise the pylab package. Type: percentage pylab and press Enter.
Let's first understand what is Pylab.
Pylab is a convenient Python module which provides- plotting functionality and has mathematical and scientific functions.
After running percentage pylab in the iPython console, you will see a message- “Using matplotlib backend: TkAgg ”.
This means matplotlib is running.
But sometimes you may get an error that says- “ImportError: No module named matplotlib”.
In such cases, you have to install matplotlib and run this command again.
Let's come back to the ipython console. Type 'linspace' followed by a question mark in ipython console. Press Enter.
Please note that the command is linspace and not linespace.
The displayed information says that linspace returns evenly spaced numbers which are calculated over the interval start and stop.
Press q to exit the documentation and return to the console.
Let's try to generate 100 points from 1 to 100. Type: linspace brackets 1 comma 100 comma 100
Here, 1 is the start , 100 is the stop and the next 100 is the number of points. Now, press Enter.
As you can see, a sequence of numbers from 1 to 100 is displayed.
Now let's try to generate 200 points between 0 and 1.
We do that by typing linspace brackets 0 comma 1 comma 200 and press Enter.
Here is the expected sequence of numbers.
In linspace, the start and stop points can be integers, decimals or constants.
Let us now learn about the len function.
First we will generate 100 points between minus pi and pi.
So, type: linspace brackets minus pi comma pi comma 100 and press Enter.
Here 'pi' is a constant defined by pylab.
Now, let us save this to a variable, say t. Press Enter.
If we now type: len bracket t and press Enter, we will get the number of points between minus pi and pi.
len function gives the no. of elements present in a given sequence.
Next, let's try and plot a cosine curve between minus pi and pi.
For this, we use the plot command. Type: plot brackets t comma cos(t) and press Enter.
As you can see from the cosine plot, cos(t) gets the cosine value at every point corresponding to point t.
We can also assign the value of cos(t) to a variable cosine by typing cosine equals to cos(t). Press Enter.
And then plot by typing plot(t comma cosine) and press Enter.
To clear the plot, we have to use the clf() function. This avoids overlapping of new plots over older plots.
In the console, type: clf() and press Enter. The previous plot is cleared and a blank plot window is displayed.
Now, let's try to plot a sine plot.
Type: plot brackets t comma sin(t) and press Enter. A sine plot is displayed.
To study the plot better on the plot window, we can use various options that are available on it. Let us have a look at these options.
Moving the mouse pointer along the plot gives us the location of each point on the plot.
Notice here, at 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.
Let us save the plot by the name sin underscore curve in pdf format.
Click on the dropdown here. As you can see, there are many formats in which to save the file. Formats like png, eps, pdf, and ps are available.
We have to specify the format that we prefer at the time of saving.
To the left of the save button, is the slider button. Using this button, we can specify the margins of the plot window.
To the left of the slider button is the zoom button . It is used to zoom into the plot. Just specify the region to zoom into.
The button to the left of zoom, can be used to move the axes of the plot.
The next two buttons with left and right arrow icons, change the state of the plot.
It takes us to the previous or next state of the plot. It acts like the back and forward button in a browser.
The last one is 'home', referring to the initial plot.
Pause the video here, try out the following exercise and then resume the video.
Plot (sin(x) multiplied by sin(x)) divided by x. 1. Save the plot as sinsquarebyx dot pdf. 2. Zoom and find the maxima. 3. Bring it back to initial position.
This brings us to the end of this tutorial. In this tutorial, we have learnt to: start IPython with pylab, use the linspace function to create equally spaced points in a region,
find the length of sequences using len function, plot mathematical functions using plot, clear drawing area using clf.
usage of buttons in the UI of the plot window such as - save, zoom, move axis, back and forward, Home.
Here are some self assessment questions for you to solve - 1. Create 100 equally spaced points between minus pi by 2 and pi by 2.
2. How can we find the length of a sequence ?
3. What will the command linspace(minus pi comma pi comma 100) do?
And the answers are- We use the command linspace(minus pi by 2 comma pi by 2 comma 100) to create 100 equally spaced lines between the points minus pi by 2 and pi by 2.
len(sequence underscore name) is the function used to find out the length of a sequence.
The command linspace(minus pi comma pi comma 100) will return 100 evenly spaced samples from -pi to pi including both minus pi and pi.
The video at the following link summarises the Spoken Tutorial project. If you do not have good bandwidth, you can download and watch it.
We conduct workshops using Spoken Tutorials and give certificates. Please contact us.
Do you have questions in THIS Spoken Tutorial?
Choose the minute and second where you have the question. Explain your question briefly. Someone from the FOSSEE team will answer them. Please visit this site.
Do you have any general / technical questions? Please visit the forum given in this link.
The FOSSEE team coordinates coding of solved examples of popular books.
We give honorarium and certificates for those who do this. For more details, please visit this site.
The Spoken Tutorial project is funded by NMEICT, MHRD, Govt. of India.
This is Usha from IIT Bombay, signing off. Thanks for watching.