Python-3.4.3/C2/Multiple-plots/English
Python/C2/Multiple plots/English
Title of script: Multiple plots
Author: Aditya Palaparthy
Keywords: Python, IPython, plot, legend, figure, savefig
|
|
Show Slide
containing title, name of the production team along with the logo of MHRD
|
Hello Friends. Welcome to the spoken tutorial on "Multiple plots". |
Show Slide
Objectives
|
At the end of this tutorial, you will be able to,
|
Show Slide
System Requirements |
To record this tutorial, I am using
|
Show Slide
Pre-requisites |
To practise this tutorial, you should know how to
If not, see the pre-requisite Python tutorials on this website. |
[Terminal]
ipython3
|
Let us first open the Terminal by pressing Ctrl+Alt+T keys simultaneously.
|
[IPython console]
%pylab and press Enter. |
Let us initialise the pylab package
|
[IPython Terminal]
x = linspace(0, 50, 10)
|
Let us create set of points for our plot by using command linspace
|
[IPython Terminal]
plot(x, sin(x)) |
Now let us draw a simple sine curve using these points.
|
[Plot Window] | As we can see, this sine curve is not a smooth curve. What really caused this? |
Pause for a while
|
This happened because we selected few points that is 10 for a large interval of 0 to 50.
It plots the points given by the analytical function. |
[IPython Terminal]
plot(y, sin(y))
|
Now, let us use linspace command to get 500 points between 0 and 50 and draw the sine curve again.
y equals to linspace inside the brackets 0 comma 50 comma 500. plot inside the brackets y comma sin(y) |
[Plot Window] | Now we see a sine curve with a smooth curve.
|
[IPython Terminal]
|
To distinguish between two overlaid plots we use legend command.
Type legend inside the brackets square brackets commas sin(x) comma sin(y).
|
[Plot Window]
|
Now we can see the legends being displayed for the two sine curves on the plot area. |
[IPython Terminal]
|
In the IPython terminal now type clf() to clear the plot window. |
Show Slide
Draw two plots for the given form
|
Pause the video. Try this exercise and then resume the video.
Use legends to indicate what each plot is doing. |
[IPython Terminal]
x = linspace(-5, 5, 100) plot(x, 4 * (x * x)) plot(x, (2 * x) + 3) |
<Switch to the terminal for solution.>
Type x equals to linspace inside the brackets minus 5 comma 5 comma 100)
Type plot inside the brackets x comma 4 multiplied by inside the brackets x multiplied by x. plot inside the brackets x comma (2 multiplied by x) plus 3) |
[IPython Terminal]
legend([r'$y = 4(x ^ 2)$', r'$y = 2x + 3$']) |
Now, we will add a legend to identify the plots
Type legend inside the brackets inside square brackets r inside inverted commas dollar y equals to 4 x square dollar comma r inside inverted commas dollar y equals to 2x plus 3 dollar. |
[Plot Window] | We can see the legend added to the plot.
|
[IPython Terminal]
clf()
|
Next we will learn to switch between the plots and perform operations such as saving the plots etc.
Type clf() |
[IPython Terminal]
x = linspace(0, 50, 500) figure(1) plot(x, sin(x), 'b') figure(2) plot(x, cos(x), 'g')
|
Switch to terminal
Type x equals to linspace inside the brackets 0 comma 50 comma 500
Type figure(1) plot inside the brackets x comma sin(x) comma inisde inverted commas b figure(2) plot inside the brackets x comma cos(x) comma inisde inverted commas g |
[Plot Window]
|
Now we have two plots, a sine curve and a cosine curve in two different figures. |
[Ipython Terminal]
|
The figure command takes an integer as an argument.
|
[IPython Terminal]
title('cos(x)') savefig('cosine.png') figure(1) title('sin(x)') savefig('sine.png') |
For example, we can save each plot separately.
title inside brackets inisde inverted commas cos(x) savefig inside brackets inside inverted commas cosine.png figure(1) title inside brackets inisde inverted commas sin(x) savefig inside brackets inside inverted commas sine.png The figures will be saved in current working directory. Now close both the plot windows. |
Show Slide
Exercise2 |
Pause the video.Try this exercise and then resume the video.
Save each of them. |
[IPython Terminal]
figure(1) x = linspace(-5, 5, 100) plot(x, x) |
Switch to the terminal for solution.
To solve this problem we will use the figure command to create first plotting area Type figure(1) x equals to linspace inside the brackets minus 5 comma 5 comma 100 plot inside the brackets x comma x |
[IPython Terminal]
figure(2) plot(x, ((2 * x) + 3)) |
Now use the figure command to create second plotting area and plot the figure
Type figure(2) plot inside the brackets x comma 2x plus 3 |
[IPython Terminal]
savefig('plot1.png') figure(2) savefig('plot2.png') |
Now to switch between the figures we can use figure command.
Type
savefig inside brackets inside inverted commas plot1.png figure(2) savefig inside brackets inside inverted commas plot2.png |
Show Slide
Summary slide
|
This brings us to the end of this tutorial. In this tutorial, we have learnt to,
|
Show Slide
Evaluation
|
Here are some self assessment questions for you to solve
|
Show Slide
Solutions
|
And the answers,
|
Show Slide
Forum |
Please post your timed queries in this forum. |
Show Slide
Fossee Forum |
Please post your general queries on Python in this forum.
|
Show Slide
Textbook Companion |
FOSSEE team coordinates the TBC project. |
Show Slide
Acknowledgement |
Spoken-tutorial is funded by NMEICT, MHRD, Govt. of India.
For more details, visit this website. |
Show Slide
Thank You |
This is _________ from IIT Bombay signing off.
Thank You |