Difference between revisions of "Python-3.4.3/C2/Multiple-plots/English"
Nancyvarkey (Talk | contribs) |
|||
(4 intermediate revisions by 2 users not shown) | |||
Line 16: | Line 16: | ||
containing title, name of the production team along with the logo of MHRD | containing title, name of the production team along with the logo of MHRD | ||
− | |||
− | |||
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0382in;padding-right:0.075in;"| Hello Friends. Welcome to the spoken tutorial on "'''Multiple plots'''". | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0382in;padding-right:0.075in;"| Hello Friends. Welcome to the spoken tutorial on "'''Multiple plots'''". | ||
Line 30: | Line 28: | ||
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0382in;padding-right:0.075in;"| At the end of this tutorial, you will be able to, | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0382in;padding-right:0.075in;"| At the end of this tutorial, you will be able to, | ||
− | + | # Draw multiple '''plots''' which are '''overlaid'''. | |
− | # | + | |
# Use the '''figure command'''. | # Use the '''figure command'''. | ||
# Use the '''legend command'''. | # Use the '''legend command'''. | ||
− | # | + | # Switch between the '''plots''' and perform some '''operations''' on each of them like saving the '''plots'''. |
− | + | ||
− | + | ||
|- | |- | ||
Line 47: | Line 42: | ||
* '''Python 3.4.3''' | * '''Python 3.4.3''' | ||
* '''IPython 5.1.0''' | * '''IPython 5.1.0''' | ||
− | |||
− | |||
|- | |- | ||
Line 78: | Line 71: | ||
'''%pylab '''and press '''Enter.''' | '''%pylab '''and press '''Enter.''' | ||
− | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0382in;padding-right:0.075in;"| Let us initialise the '''pylab package''' | + | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0382in;padding-right:0.075in;"| Let us initialise the '''pylab package'''. |
− | Type '''percentage | + | Type '''percentage pylab '''and press''' Enter.''' |
|- | |- | ||
Line 90: | Line 83: | ||
− | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0382in;padding-right:0.075in;"| Let us create | + | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0382in;padding-right:0.075in;"| Let us create set of '''points''' for our '''plot '''by using '''command linspace''' |
− | Type '''x equals to linspace inside the brackets 0 comma 50 comma 10''' | + | Type '''x equals to linspace''' inside the '''brackets 0 comma 50 comma 10''' |
|- | |- | ||
Line 102: | Line 95: | ||
− | Type '''plot inside the brackets x comma sin(x)''' | + | Type '''plot''' inside the '''brackets x comma sin(x)''' |
− | + | ||
− | + | ||
− | + | ||
|- | |- | ||
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0382in;padding-right:0.075in;"| [Plot Window] | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0382in;padding-right:0.075in;"| [Plot Window] | ||
− | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0382in;padding-right:0.075in;"| As we can see, this '''sine curve''' is not a '''smooth curve. '''What really caused this? | + | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0382in;padding-right:0.075in;"| As we can see, this '''sine curve''' is not a '''smooth curve. ''' What really caused this? |
|- | |- | ||
Line 119: | Line 109: | ||
− | '''Plot | + | '''Plot function''' does not '''plot''' the '''analytical function.''' |
It plots the '''points''' given by the '''analytical function'''. | It plots the '''points''' given by the '''analytical function'''. | ||
Line 133: | Line 123: | ||
− | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0382in;padding-right:0.075in;"| Now, let us use '''linspace''' | + | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0382in;padding-right:0.075in;"| Now, let us use '''linspace command''' to get 500 points between 0 and 50 and draw the '''sine curve''' again. |
Type | Type | ||
− | '''y equals to linspace inside the brackets 0 comma 50 comma 500.''' | + | '''y equals to linspace''' inside the '''brackets 0 comma 50 comma 500.''' |
− | '''plot inside the brackets y comma sin(y)''' | + | '''plot''' inside the '''brackets y comma sin(y)''' |
|- | |- | ||
Line 157: | Line 147: | ||
'''legend(['sin(x)', 'sin(y)'])''' | '''legend(['sin(x)', 'sin(y)'])''' | ||
− | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0382in;padding-right:0.075in;"| To '''distinguish''' between two '''overlaid plots''' we use '''legend | + | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0382in;padding-right:0.075in;"| To '''distinguish''' between two '''overlaid plots''' we use '''legend command.''' |
− | Type '''legend inside the brackets square brackets | + | Type '''legend''' inside the '''brackets square brackets sin(x) comma sin(y).''' |
− | The '''legend | + | The '''legend command''' takes '''parameter '''as a '''list''' of '''strings.''' |
Line 179: | Line 169: | ||
− | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0382in;padding-right:0.075in;"| In the IPython terminal now type clf() to clear the plot window. | + | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0382in;padding-right:0.075in;"| In the '''IPython terminal''' now type '''clf()''' to clear the '''plot''' window. |
|- | |- | ||
Line 189: | Line 179: | ||
Draw two plots for the given form | Draw two plots for the given form | ||
− | # First plot is a parabola of the form '''y = 4(x ^ 2)''' | + | # First '''plot''' is a '''parabola''' of the form '''y = 4(x ^ 2)''' |
− | # Second plot is a '''straight line''' of the form '''y = 2x + 3''' in the '''interval''' -5 to 5. | + | # Second '''plot''' is a '''straight line''' of the form '''y = 2x + 3''' in the '''interval''' -5 to 5. |
− | # Use legends to indicate each plot. | + | # Use '''legends''' to indicate each '''plot'''. |
Line 197: | Line 187: | ||
− | Draw two '''plots''' first plot being a '''parabola''' of the form '''y equals to 4x square''' and the second being a '''straight line''' of the form '''y equals to 2x plus 3''' in the '''interval''' '''minus '''5 to 5. | + | Draw two '''plots''' first '''plot''' being a '''parabola''' of the form '''y equals to 4x square''' |
+ | |||
+ | and | ||
+ | |||
+ | the second being a '''straight line''' of the form '''y equals to 2x plus 3''' in the '''interval''' '''minus '''5 to 5. | ||
Use '''legends''' to indicate what each '''plot''' is doing. | Use '''legends''' to indicate what each '''plot''' is doing. | ||
Line 209: | Line 203: | ||
'''plot(x, (2 * x) + 3)''' | '''plot(x, (2 * x) + 3)''' | ||
− | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0382in;padding-right:0.075in;"| | + | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0382in;padding-right:0.075in;"| Switch to the terminal for solution. |
− | Type '''x equals to linspace inside the brackets minus 5 comma 5 comma 100 | + | Type '''x equals to linspace''' inside the '''brackets minus 5 comma 5 comma 100''' |
Line 218: | Line 212: | ||
Type | Type | ||
− | '''plot inside the brackets x comma 4 multiplied by inside the brackets x multiplied by x. ''' | + | '''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 | + | '''plot''' inside the '''brackets x comma (2 multiplied by x) plus 3''' |
|- | |- | ||
Line 226: | Line 220: | ||
'''legend([r'$y = 4(x ^ 2)$', r'$y = 2x + 3$']) ''' | '''legend([r'$y = 4(x ^ 2)$', r'$y = 2x + 3$']) ''' | ||
− | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0382in;padding-right:0.075in;"| Now, we will add a legend to identify the plots | + | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0382in;padding-right:0.075in;"| 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. ''' | + | 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. ''' |
|- | |- | ||
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0382in;padding-right:0.075in;"| [Plot Window] | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0382in;padding-right:0.075in;"| [Plot Window] | ||
− | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0382in;padding-right:0.075in;"| We can see the '''legend''' added to the plot. | + | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0382in;padding-right:0.075in;"| We can see the '''legend''' added to the '''plot'''. |
− | + | ||
<Pause> | <Pause> | ||
Line 244: | Line 237: | ||
− | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0382in;padding-right:0.075in;"| Next we will learn to '''switch''' between the '''plots''' and perform '''operations''' such as | + | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0382in;padding-right:0.075in;"| Next we will learn to '''switch''' between the '''plots''' and perform '''operations''' such as saving the '''plots''' etc. |
Line 250: | Line 243: | ||
− | But before we move on, let us | + | But before we move on, let us clear our screen. |
Type '''clf()''' | Type '''clf()''' | ||
Line 258: | Line 251: | ||
'''x = linspace(0, 50, 500) ''' | '''x = linspace(0, 50, 500) ''' | ||
+ | |||
+ | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0382in;padding-right:0.075in;"| Switch to '''terminal''' | ||
+ | |||
+ | Type '''x equals to linspace''' inside the '''brackets 0 comma 50 comma 500 ''' | ||
+ | |||
+ | |- | ||
+ | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0382in;padding-right:0.075in;"| [IPython Terminal] | ||
'''figure(1)''' | '''figure(1)''' | ||
Line 266: | Line 266: | ||
'''plot(x, cos(x), 'g')''' | '''plot(x, cos(x), 'g')''' | ||
+ | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0382in;padding-right:0.075in;"| | ||
+ | To accomplish more control over '''individual plots''' we use the '''figure command ''' | ||
+ | Type | ||
+ | '''figure(1)''' | ||
− | + | '''plot''' inside the '''brackets x comma sin(x) comma inside inverted commas b''' | |
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | + | ||
− | |||
'''figure(2)''' | '''figure(2)''' | ||
− | '''plot inside the brackets x comma cos(x) comma | + | '''plot''' inside the '''brackets x comma cos(x) comma inside inverted commas g''' |
|- | |- | ||
Line 324: | Line 320: | ||
Type | Type | ||
− | '''title inside brackets | + | '''title''' inside '''brackets''' inside '''inverted commas cos(x)''' |
+ | |||
+ | '''savefig''' inside '''brackets''' inside '''inverted commas cosine.png''' | ||
− | |||
'''figure(1)''' | '''figure(1)''' | ||
− | '''title inside brackets | + | '''title''' inside '''brackets''' inside '''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. | |
|- | |- | ||
Line 343: | Line 344: | ||
− | Draw a | + | Draw a line of the form '''y equals to x''' as one '''figure''' and another line of the form '''y equals to 2x plus 3'''. |
Save each of them. | Save each of them. | ||
Line 363: | Line 364: | ||
'''figure(1) ''' | '''figure(1) ''' | ||
− | '''x equals to linspace inside the brackets minus 5 comma 5 comma 100''' | + | '''x equals to linspace''' inside the '''brackets minus 5 comma 5 comma 100''' |
− | '''plot inside the brackets x comma x''' | + | '''plot''' inside the '''brackets x comma x''' |
|- | |- | ||
Line 379: | Line 380: | ||
'''figure(2) ''' | '''figure(2) ''' | ||
− | '''plot inside the brackets x comma 2x plus 3''' | + | '''plot''' inside the '''brackets x comma 2x plus 3''' |
|- | |- | ||
Line 392: | Line 393: | ||
'''savefig('plot2.png')''' | '''savefig('plot2.png')''' | ||
− | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0382in;padding-right:0.075in;"| | + | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0382in;padding-right:0.075in;"| |
+ | We will save the '''figure''' as follows. | ||
− | |||
Type | Type | ||
− | |||
'''figure(1)''' | '''figure(1)''' | ||
− | '''savefig inside brackets inside inverted commas plot1.png ''' | + | '''savefig''' inside '''brackets''' inside '''inverted commas plot1.png ''' |
'''figure(2)''' | '''figure(2)''' | ||
− | '''savefig inside brackets inside inverted commas plot2.png''' | + | '''savefig''' inside '''brackets''' inside '''inverted commas plot2.png''' |
|- | |- | ||
Line 418: | Line 418: | ||
# Draw '''multiple plots''' which are '''overlaid'''. | # Draw '''multiple plots''' which are '''overlaid'''. | ||
− | # Use the '''figure''' | + | # Use the '''figure command'''. |
− | # Use the '''legend''' | + | # Use the '''legend command. ''' |
− | # | + | # Switch between the '''plots''' and perform some '''operations''' on each of them like saving the '''plots'''. |
− | + | ||
− | + | ||
|- | |- | ||
Line 435: | Line 433: | ||
# What '''command''' is used to get individual '''plots''' separately? | # What '''command''' is used to get individual '''plots''' separately? | ||
# What will be the '''command''' to identify '''sine''' and '''cosine curve'''? | # What will be the '''command''' to identify '''sine''' and '''cosine curve'''? | ||
− | |||
− | |||
|- | |- | ||
Line 447: | Line 443: | ||
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0382in;padding-right:0.075in;"| And the answers,''' ''' | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0382in;padding-right:0.075in;"| And the answers,''' ''' | ||
− | # The '''command | + | # The '''command "figure()"''' can get us the individual '''plots''' separately. |
− | # ''' | + | # '''legend''' inside '''brackets''' inside '''square brackets''' inside '''inverted commas sin(x) comma inside inverted commas cos(x)''' |
− | + | ||
− | + | ||
|- | |- | ||
Line 463: | Line 457: | ||
Fossee Forum | Fossee Forum | ||
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0382in;padding-right:0.075in;"| Please post your general queries on Python in this forum. | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0382in;padding-right:0.075in;"| Please post your general queries on Python in this forum. | ||
− | |||
− | |||
− | |||
|- | |- | ||
Line 485: | Line 476: | ||
Thank You | Thank You | ||
− | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0382in;padding-right:0.075in;"| This is | + | | style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0382in;padding-right:0.075in;"| This is Usha from IIT Bombay signing off. |
Thank You | Thank You | ||
|} | |} |
Latest revision as of 13:00, 8 March 2017
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 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.
and the second being a straight line of the form y equals to 2x plus 3 in the interval minus 5 to 5. 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.
<Pause> |
[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) |
Switch to terminal
Type x equals to linspace inside the brackets 0 comma 50 comma 500 |
[IPython Terminal]
figure(1) plot(x, sin(x), 'b') figure(2) plot(x, cos(x), 'g') |
To accomplish more control over individual plots we use the figure command Type figure(1) plot inside the brackets x comma sin(x) comma inside inverted commas b
plot inside the brackets x comma cos(x) comma inside 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 inside inverted commas cos(x) savefig inside brackets inside inverted commas cosine.png
title inside brackets inside 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') |
We will save the figure as follows.
figure(1) 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 Usha from IIT Bombay signing off.
Thank You |