Difference between revisions of "Python-3.4.3/C2/Multiple-plots/English"

From Script | Spoken-Tutorial
Jump to: navigation, search
(Created page with "'''Python/C2/Multiple plots/English''' '''Title of script''': '''Multiple plots''' '''Author: Aditya Palaparthy''' '''Keywords: Python, IPython,''' '''plot, legend, figure,...")
 
 
(7 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'''.  
# '''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'''.  
+
# 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 '''%pylab '''and press''' Enter.'''
+
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 '''set''' of '''points''' for our '''plot '''by using '''command''' '''linspace'''
+
| 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 = linspace inside the brackets (0, 50, 10)'''
+
Type '''x equals to linspace''' inside the '''brackets 0 comma 50 comma 10'''
  
 
|-
 
|-
Line 102: Line 95:
  
  
Type '''plot inside the brackets (x, 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''' '''function''' does not '''plot''' the '''analytical function.'''
+
'''Plot function''' does not '''plot''' the '''analytical function.'''
  
It plots the '''points''' given by '''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''' command to get 500 points between 0 and 50 and draw the '''sine curve''' again.  
+
| 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 = linspace inside the brackets (0, 50, 500)'''
+
'''y equals to linspace''' inside the '''brackets 0 comma 50 comma 500.'''
  
'''plotinside the brackets (y, 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''' '''command.'''
+
| 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['sin(x)', 'sin(y)'])'''
+
Type '''legend''' inside the '''brackets square brackets sin(x) comma sin(y).'''
  
  
The '''legend''' '''command''' takes '''parameter '''as a '''list''' of '''strings.'''
+
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 = 4(x ^ 2)''' and the second being a '''straight line''' of the form '''y = 2x + 3''' in the '''interval''' -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;"| <Switch to the terminal for solution.>
+
| 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 = linspace inside the brackets (-5, 5, 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, 4 * (x * x)) '''
+
'''plot''' inside the '''brackets x comma 4 multiplied by''' inside the '''brackets x multiplied by x. '''
  
'''plot inside the brackets (x, (2 * x) + 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'$y = 4(x ^ 2)$', r'$y = 2x + 3$']) '''
+
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 '''saving''' the '''plots''' etc.
+
| 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 '''clear''' our '''screen'''.  
+
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)'''
  
| 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'''  
+
'''plot''' inside the '''brackets x comma sin(x) comma inside inverted commas b'''
 
+
Type '''x = linspace inside the brackets (0, 50, 500) '''
+
 
+
 
+
To accomplish more control over '''individual plots''' we use the '''figure command '''
+
 
+
Type '''figure(1)'''
+
  
'''plot inside the brackets (x, sin(x), 'b')'''
 
  
 
'''figure(2)'''
 
'''figure(2)'''
  
'''plot inside the brackets (x, cos(x), 'g')'''
+
'''plot''' inside the '''brackets x comma cos(x) comma inside inverted commas g'''
  
 
|-
 
|-
Line 324: Line 320:
 
Type  
 
Type  
  
'''title('cos(x)')'''
+
'''title''' inside '''brackets''' inside '''inverted commas cos(x)'''
 +
 
 +
'''savefig''' inside '''brackets''' inside '''inverted commas cosine.png'''
  
'''savefig('cosine.png')'''
 
  
 
'''figure(1)'''
 
'''figure(1)'''
  
'''title('sin(x)')'''
+
'''title''' inside '''brackets''' inside '''inverted commas sin(x)'''
  
'''savefig('sine.png')'''
+
'''savefig''' inside '''brackets''' inside '''inverted commas sine.png'''
 +
 
 +
 
 +
 
 +
The figures will be saved in current working directory.
  
The figures will be saved in current working directory. Now close both the plot windows.
+
Now close both the '''plot''' windows.
  
 
|-
 
|-
Line 343: Line 344:
  
  
Draw a '''line''' of the form '''y = x''' as one '''figure''' and another '''line''' of the form '''y = 2x + 3'''.  
+
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 = linspace(-5, 5, 100) '''
+
'''x equals to linspace''' inside the '''brackets minus 5 comma 5 comma 100'''
  
'''plot(x, x)'''
+
'''plot''' inside the '''brackets x comma x'''
  
 
|-
 
|-
Line 379: Line 380:
 
'''figure(2) '''
 
'''figure(2) '''
  
'''plot(x, ((2 * x) + 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;"| Now to switch between the '''figures''' we can use '''figure command'''.
+
| 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.
  
We will save the figure as follows
 
  
 
Type  
 
Type  
 
  
 
'''figure(1)'''
 
'''figure(1)'''
  
'''savefig('plot1.png')'''
+
'''savefig''' inside '''brackets''' inside '''inverted commas plot1.png '''
  
 
'''figure(2)'''
 
'''figure(2)'''
  
'''savefig('plot2.png')'''
+
'''savefig''' inside '''brackets''' inside '''inverted commas plot2.png'''
  
 
|-
 
|-
Line 417: Line 417:
 
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0382in;padding-right:0.075in;"| This brings us to the end of this tutorial. In this tutorial, we have learnt to,''' '''
 
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0382in;padding-right:0.075in;"| This brings us to the end of this tutorial. In this tutorial, we have learnt to,''' '''
  
# '''Sw'''Draw '''multiple plots''' which are '''overlaid'''.  
+
# Draw '''multiple plots''' which are '''overlaid'''.  
# Use the '''figure''' command.  
+
# Use the '''figure command'''.  
# Use the '''legend''' command.
+
# Use the '''legend command. '''
# '''itch''' between the '''plots''' and perform some '''operations''' on each of them like '''saving''' the '''plots'''.  
+
# 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 '''"'''figure()'''" can get us the individual '''plots''' separately.  
+
# The '''command "figure()"''' can get us the individual '''plots''' separately.  
# '''legend(['sin(x)','cos(x)'])'''
+
# '''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 _________ from IIT Bombay signing off.
+
| 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


Visual Cue
Narration
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,
  1. Draw multiple plots which are overlaid.
  2. Use the figure command.
  3. Use the legend command.
  4. Switch between the plots and perform some operations on each of them like saving the plots.
Show Slide

System Requirements

To record this tutorial, I am using
  • Ubuntu Linux 14.04 operating system
  • Python 3.4.3
  • IPython 5.1.0
Show Slide

Pre-requisites

To practise this tutorial, you should know how to
  • use Plots interactively
  • Embellish a plot
  • Save plots

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.


Now, type ipython3 and press Enter.

[IPython console]

%pylab and press Enter.

Let us initialise the pylab package.


Type percentage pylab and press Enter.

[IPython Terminal]

x = linspace(0, 50, 10)


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

[IPython Terminal]

plot(x, sin(x))

Now let us draw a simple sine curve using these points.


Type plot inside the brackets x comma sin(x)

[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.


Plot function does not plot the analytical function.

It plots the points given by the analytical function.

[IPython Terminal]


y = linspace(0, 50, 500)

plot(y, sin(y))


Now, let us use linspace command to get 500 points between 0 and 50 and draw the sine curve again.


Type

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.


Notice we also have two plots one overlaid upon another.


In pylab, by default all the plots are overlaid.

[IPython Terminal]


legend(['sin(x)', 'sin(y)'])

To distinguish between two overlaid plots we use legend command.

Type legend inside the brackets square brackets sin(x) comma sin(y).


The legend command takes parameter as a list of strings.


Then it assigns strings to plots in the order they were created.

[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


Exercise 1

Draw two plots for the given form

  1. First plot is a parabola of the form y = 4(x ^ 2)
  2. Second plot is a straight line of the form y = 2x + 3 in the interval -5 to 5.
  3. Use legends to indicate each plot.


Pause the video. Try this exercise and then resume the video.


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.

[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


We can obtain the two plots in different colours using the following commands.

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.


Let us see how to accomplish this.


But before we move on, let us clear our screen.

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


figure(2)

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.


This is the serial number of the plot to select corresponding plot.


All the plot commands we run hereafter are applied to the selected plot.


In this example, figure 1 is the sine plot and figure 2 is the cosine plot.

[IPython Terminal]

title('cos(x)')

savefig('cosine.png')

figure(1)

title('sin(x)')

savefig('sine.png')

For example, we can save each plot separately.


Type

title inside brackets inside inverted commas cos(x)

savefig inside brackets inside inverted commas cosine.png


figure(1)

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.


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.

[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]


figure(1)

savefig('plot1.png')

figure(2)

savefig('plot2.png')

We will save the figure as follows.


Type

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,
  1. Draw multiple plots which are overlaid.
  2. Use the figure command.
  3. Use the legend command.
  4. Switch between the plots and perform some operations on each of them like saving the plots.
Show Slide

Evaluation


Here are some self assessment questions for you to solve
  1. What command is used to get individual plots separately?
  2. What will be the command to identify sine and cosine curve?
Show Slide

Solutions


And the answers,
  1. The command "figure()" can get us the individual plots separately.
  2. legend inside brackets inside square brackets inside inverted commas sin(x) comma inside inverted commas cos(x)
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

Contributors and Content Editors

Nancyvarkey, Nirmala Venkat, Vineeta