Difference between revisions of "Python-3.4.3/C2/Subplots/English"

From Script | Spoken-Tutorial
Jump to: navigation, search
 
(2 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 "'''Subplots'''".  
 
| 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 "'''Subplots'''".  
 
 
  
  
Line 29: Line 25:
 
Objectives  
 
Objectives  
  
 
+
| 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 create and switch between '''subplots'''.
 
+
| 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,
+
 
+
 
+
# Create and '''switch''' between '''subplots'''.
+
 
+
 
+
  
 
|-
 
|-
Line 47: Line 36:
 
* '''Python 3.4.3'''
 
* '''Python 3.4.3'''
 
* '''IPython 5.1.0'''
 
* '''IPython 5.1.0'''
 
 
  
 
|-
 
|-
Line 56: Line 43:
 
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0382in;padding-right:0.075in;"| To practise this tutorial, you should know how to  
 
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0382in;padding-right:0.075in;"| To practise this tutorial, you should know how to  
  
* run basic '''Python '''commands on the '''ipython console'''
+
* Run basic '''Python '''commands on the '''ipython console'''
* use '''Plots''' interactively
+
* Use '''Plots''' interactively
 
* Embellish a '''plot'''
 
* Embellish a '''plot'''
  
Line 66: Line 53:
  
 
'''ipython3'''
 
'''ipython3'''
 
 
  
 
| 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 first open the '''Terminal '''by pressing '''Ctrl+Alt+T '''keys simultaneously.  
 
| 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 first open the '''Terminal '''by pressing '''Ctrl+Alt+T '''keys simultaneously.  
Line 77: Line 62:
 
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0382in;padding-right:0.075in;"| [IPython console]
 
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0382in;padding-right:0.075in;"| [IPython console]
  
'''%pylab '''and press '''Enter.'''
+
'''percentage 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 95: Line 80:
  
 
'''plot(y, y ** 2)'''
 
'''plot(y, y ** 2)'''
 
 
  
 
| 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;"|   
Line 103: Line 86:
  
  
Let us now create two plots, a cosine plot and an exponential curve in the same plotting area.
+
Let us now create two '''plots'''- a '''cosine plot''' and an '''exponential curve''' in the same '''plotting area'''.
  
  
 
Type
 
Type
  
'''x equals to linspace inside brackets 0 comma 50 comma 500 '''and press enter
+
'''x equals to linspace''' inside '''brackets 0 comma 50 comma 500 '''and press '''Enter'''.
  
'''plot inside brackets x comma cos(x).'''
+
'''plot''' inside '''brackets x comma cos(x)'''
  
'''y equals to linspace inside brackets 0 comma 5 comma 100.'''
 
  
'''  '''
+
Type
  
'''plot inside brackets y comma y square.'''
+
'''y equals to linspace''' inside '''brackets 0 comma 5 comma 100.'''
  
  
  
 +
'''plot''' inside '''brackets y comma y square.'''
  
 
|-
 
|-
Line 125: Line 108:
  
 
[Plot Window]
 
[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;"|   
 
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0382in;padding-right:0.075in;"|   
  
Here the two '''plots''' have different regular '''axes''' so we cannot draw '''overlaid plots. '''
+
Here the two '''plots''' have different regular '''axes.''' So we cannot draw '''overlaid plots. '''
 
+
 
+
 
+
  
 
|-
 
|-
Line 144: Line 122:
  
 
'''subplot(2, 1, 1)'''
 
'''subplot(2, 1, 1)'''
 
 
  
 
| 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;"|   
Line 151: Line 127:
 
In such cases we can draw '''subplots'''.
 
In such cases we can draw '''subplots'''.
  
 +
We use '''subplot command''' to accomplish this.
  
We use '''subplot command''' to accomplish this
 
  
 
Type
 
Type
Line 158: Line 134:
 
'''clf()'''
 
'''clf()'''
  
'''subplot inside the brackets 2 comma 1 comma 1'''
+
'''subplot''' inside the '''brackets 2 comma 1 comma 1'''
 
+
 
+
 
+
  
 
|-
 
|-
Line 168: Line 141:
  
 
|-
 
|-
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0382in;padding-right:0.075in;"| Pause for a while
+
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0382in;padding-right:0.075in;"| Pause for a while and highlight
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0382in;padding-right:0.075in;"| The '''subplot''' '''command''' takes three '''arguments.'''
+
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0382in;padding-right:0.075in;"| The '''subplot command''' takes three '''arguments.'''
  
  
 
The first '''argument''' is the number of '''rows''' of '''subplots''' that must be created.
 
The first '''argument''' is the number of '''rows''' of '''subplots''' that must be created.
  
Here the first '''argument''' is 2, to split''' '''the''' plot horizontally'''.
+
Here the first '''argument''' is 2, to split the '''plot horizontally'''.
  
 +
|-
 +
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0382in;padding-right:0.075in;"| Pause for a while and highlight
 +
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0382in;padding-right:0.075in;"|The second '''argument''' is the number of '''columns''' of '''subplots''' that must be created.
  
The second '''argument''' is the number of '''columns''' of '''subplots''' that must be created.
+
Here the second '''argument '''is 1.  So '''plot''' will not split '''vertically.'''
 
+
 
+
Here the second '''argument '''is 1, so '''plot''' will not split '''vertically.'''
+
 
+
 
+
The last '''argument''' specifies the '''serial number '''for''' subplot'''.
+
  
 +
|-
 +
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0382in;padding-right:0.075in;"| Pause for a while and highlight
 +
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0382in;padding-right:0.075in;"|The last '''argument''' specifies the '''serial number '''for the''' subplot'''.
  
 
Here we passed 1 as the '''argument''', which creates the '''upper subplot'''  
 
Here we passed 1 as the '''argument''', which creates the '''upper subplot'''  
Line 195: Line 168:
 
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0382in;padding-right:0.075in;"| If we execute the '''subplot command''' as  
 
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0382in;padding-right:0.075in;"| If we execute the '''subplot command''' as  
  
'''subplot inside the brackets 2 comma 1 comma 2'''
+
*'''subplot''' inside the '''brackets 2 comma 1 comma 2'''
  
The '''lower subplot''' is created.  
+
the '''lower subplot''' is created.  
  
 
|-
 
|-
Line 219: Line 192:
  
  
 +
| 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 can draw '''plots''' in each of the '''subplot area''' using the '''plot command'''.
  
| 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 can draw plots in each of the '''subplot area''' using the '''plot command'''.
+
Type
  
 +
'''subplot''' inside '''brackets 2 comma 1 comma 1'''
  
Type
+
'''plot''' inside '''brackets x comma cos(x)'''
  
'''subplot inside brackets 2 comma 1 comma 1'''
 
  
'''plot inside brackets x, cos(x)'''
+
Now type
  
'''subplot inside brackets 2 comma 1 comma 2 '''
+
'''subplot''' inside '''brackets 2 comma 1 comma 2 '''
  
'''plot inside brackets y comma y square'''
+
'''plot y comma y square'''
  
 
|-
 
|-
 
| 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;"| This created two '''plots''' one in each of the''' subplot''' area.  
 
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0382in;padding-right:0.075in;"| This created two '''plots''' one in each of the''' subplot''' area.  
 +
*The top '''subplot''' holds a '''cosine curve'''
 +
*And the bottom '''subplot''' holds a '''parabola'''.
  
 +
|-
 +
| 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;"|It is clear from the two '''subplots''' that both have different '''regular axes'''.
  
The top '''subplot''' holds a '''cosine curve''' and the bottom '''subplot''' holds a '''parabola'''.  
+
|-
 +
| 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;"|For the '''cosine curve, x-axis''' varies from 0 to 50 and '''y-axis''' varies from '''minus '''1 to 1.
  
 
+
|-
It is clear from the two '''subplots''' that both have different '''regular axes'''.  
+
| 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;"|For the '''parabolic curve, x-axis''' varies from 0 to 5 and '''y-axis''' varies from 0 to 25.
For the '''cosine curve x-axis''' varies from 0 to 50 and '''y-axis''' varies from '''minus '''1 to 1.
+
 
+
For the '''parabolic curve x-axis''' varies from 0 to 5 and '''y-axis''' varies from 0 to 25.
+
  
 
|-
 
|-
 
| 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]
 
| 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]
 
 
 
'''clf()'''
 
'''clf()'''
 
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0382in;padding-right:0.075in;"| Now 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;"| Now clear the plot window
Line 258: Line 234:
  
 
Exercise 1
 
Exercise 1
 
  
 
* '''Pressure, Volume '''and''' Temperatures '''are held by the '''equation PV = nRT''' where '''nR''' is a '''constant'''.
 
* '''Pressure, Volume '''and''' Temperatures '''are held by the '''equation PV = nRT''' where '''nR''' is a '''constant'''.
  
* A'''ssume''' '''nR =0.01 Joules/Kelvin''' and '''T = 200K'''. V can be in the '''range''' from '''21cc to 100cc'''.
+
* Assume '''nR =0.01 Joules/Kelvin''' and '''T = 200K'''. V can be in the '''range''' from '''21cc to 100cc'''.
  
  
Line 268: Line 243:
  
  
'''Pressure, Volume '''and''' Temperatures '''are held by the '''equation Pv equals to nRT''' where '''nR''' is a '''constant'''.
+
#'''Pressure, Volume '''and''' Temperatures '''are held by the '''equation Pv equals to nRT''' where '''nR''' is a '''constant'''.
 
+
#Assume '''nR equals to 0.01 Joules per Kelvin''' and '''T equals to 200K'''. '''V''' can be in the '''range''' from '''21cc to 100cc'''.
A'''ssume''' '''nR equals to 0.01 Joules per Kelvin''' and '''T equals to 200K'''. V can be in the '''range''' from '''21cc to 100cc'''.
+
  
  
Line 276: Line 250:
  
 
* '''Pressure v/s Volume plot '''and
 
* '''Pressure v/s Volume plot '''and
 
 
* '''Pressure v/s Temperature plot'''.  
 
* '''Pressure v/s Temperature plot'''.  
 
 
  
 
|-
 
|-
Line 289: Line 260:
  
 
'''v = linspace(21, 100, 500)'''
 
'''v = linspace(21, 100, 500)'''
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0382in;padding-right:0.075in;"| To start with, we have been given the '''range''' of '''Volume''' using which we can define the variable '''v.'''
+
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0382in;padding-right:0.075in;"| To start with, we have been given the '''range''' of '''Volume''' from which we can define the variable '''v.'''
  
'''v equals to linspace inside brackets 21 comma 100 comma 500'''
+
'''v equals to linspace''' inside '''brackets 21 comma 100 comma 500'''
  
 
|-
 
|-
Line 300: Line 271:
 
'''plot(v, 2.0/v)'''
 
'''plot(v, 2.0/v)'''
  
 +
| 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 create first '''subplot''' and draw '''Pressure''' v/s '''Volume''' '''graph '''using this '''v'''.
  
 +
*'''subplot(2 comma 1 comma 1) '''
  
| 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 create first '''subplot''' and draw '''Pressure''' v/s '''Volume''' '''graph '''using this '''v'''.
+
*'''plot(v comma 2 point 0 by v)'''
  
'''subplot(2 comma 1 comma 1) '''
+
We know that '''nRT''' is a '''constant''' which is equal to 2.0
  
'''plot(v comma 2.0 by v)'''
+
since
 +
*'''nR''' = equals to 0.01 '''Joules per Kelvin'''  
  
 
+
* and '''T''' equals to 200 '''Kelvin '''
We know that '''nRT''' is a '''constant''' which is equal to 2.0 since '''nR''' = equals to 0.01 '''Joules per Kelvin''' and '''T equals to '''200 '''Kelvin '''
+
  
 
|-
 
|-
Line 317: Line 290:
  
 
'''plot(200, 2.0/v)'''
 
'''plot(200, 2.0/v)'''
 
 
  
 
| 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 can create the second '''subplot''' and draw the '''Pressure''' v/s '''Temperature plot''' as follows  
 
| 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 can create the second '''subplot''' and draw the '''Pressure''' v/s '''Temperature plot''' as follows  
  
 +
*'''subplot(2 comma 1 comma 2)'''
  
'''subplot(2 comma 1 comma 2)'''
+
*'''plot(200 comma 2 point 0 divided by v)'''
 
+
'''plot(200 comma 2.0 divided by v)'''
+
  
 
|-
 
|-
Line 332: Line 302:
 
'''t = linspace(200, 200, 500) '''
 
'''t = linspace(200, 200, 500) '''
  
 +
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0382in;padding-right:0.075in;"| We have an error now, telling '''x''' and '''y dimensions''' don't match.
  
 +
So we will create same number of '''points''' for '''temperature.'''
  
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0382in;padding-right:0.075in;"| We have an '''error''' now, telling x and y '''dimensions''' don't match.
 
 
 
So we will create same number of '''points''' for '''temperature.'''
 
  
 
Type  
 
Type  
  
'''t equals to linspace inside brackets 200 comma 200 comma 500. '''
+
'''t equals to linspace''' inside '''brackets 200 comma 200 comma 500. '''
  
 
|-
 
|-
Line 349: Line 317:
 
'''plot(t, 2.0/v) '''
 
'''plot(t, 2.0/v) '''
  
 +
| 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 have 500 values in '''t '''and each with the value 200 '''Kelvin'''.
  
 +
'''Plotting''' this data, we get the required '''plot. '''
  
| 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 have 500 values in '''t '''and''' '''each with the value 200 '''Kelvin'''. '''Plotting''' this data, we get the required '''plot. '''
 
  
 
Type
 
Type
  
'''plot inside brackets t comma 2.0 divided by v'''  
+
'''plot''' inside '''brackets t comma 2 point 0 divided by v'''  
  
 
|-
 
|-
Line 366: Line 335:
 
Summary slide  
 
Summary slide  
  
 +
| 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.
  
  
| 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,''' '''
+
In this tutorial, we have learnt to create '''subplots '''and to '''switch''' between them.  
 
+
# Create '''subplots '''and to '''switch''' between them.  
+
 
+
 
+
  
 
|-
 
|-
Line 381: Line 347:
  
  
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0382in;padding-right:0.075in;"| Here are some self assessment questions for you to solve  
+
| style="background-color:#ffffff;border:0.5pt solid #000001;padding-top:0in;padding-bottom:0in;padding-left:0.0382in;padding-right:0.075in;"| Here are some self assessment questions for you to solve.
 
+
 
+
# Which of the following is correct.  
+
 
+
*
+
** '''subplot(numRows comma numCols, plotNum) '''
+
** '''subplot(numRows comma numCols) '''
+
** '''subplot(numCols comma numRows) '''
+
 
+
  
 +
# Which of the following is correct?
 +
#* '''subplot''' inside '''brackets numRows comma numCols, plotNum '''
 +
#* '''subplot''' inside '''brackets numRows comma numCols '''
 +
#* '''subplot''' inside '''brackets numCols comma numRows'''
  
 
|-
 
|-
Line 398: Line 359:
 
Solutions  
 
Solutions  
  
 +
| 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 answer is first option.
  
 
+
# The''' subplot command''' takes three '''arguments''' namely the number of '''rows''', number of '''columns''' and the '''plot number'''.  
| 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 answer is,''' first option.'''
+
 
+
# The''' subplot''' command takes three '''arguments''' namely the number of '''rows''' , number of '''columns''' and the '''plot number'''.  
+
 
+
 
+
  
 
|-
 
|-
Line 436: Line 393:
  
 
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 18:14, 9 March 2017

Python/C2/Subplots/English

Title of script: Subplots

Author: Aditya Palaparthy

Keywords: Python, IPython, plot, subplot


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 "Subplots".


Show Slide

Objectives

At the end of this tutorial, you will be able to create and switch between subplots.
Show Slide

System Specifications

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
  • Run basic Python commands on the ipython console
  • Use Plots interactively
  • Embellish a plot

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]

percentage pylab and press Enter.

Let us initialise the pylab package.


Type percentage pylab and press Enter

[IPython Terminal]

x = linspace(0, 50, 500)

plot(x, cos(x))

y = linspace(0, 5, 100)

plot(y, y ** 2)

To compare two plots we draw the plots in the same plotting area.


Let us now create two plots- a cosine plot and an exponential curve in the same plotting area.


Type

x equals to linspace inside brackets 0 comma 50 comma 500 and press Enter.

plot inside brackets x comma cos(x)


Type

y equals to linspace inside brackets 0 comma 5 comma 100.


plot inside brackets y comma y square.

[Plot Window]

Here the two plots have different regular axes. So we cannot draw overlaid plots.

[IPython Terminal]


clf()

subplot(2, 1, 1)

In such cases we can draw subplots.

We use subplot command to accomplish this.


Type

clf()

subplot inside the brackets 2 comma 1 comma 1

[Plot Window] We can see the first subplot.
Pause for a while and highlight The subplot command takes three arguments.


The first argument is the number of rows of subplots that must be created.

Here the first argument is 2, to split the plot horizontally.

Pause for a while and highlight The second argument is the number of columns of subplots that must be created.

Here the second argument is 1. So plot will not split vertically.

Pause for a while and highlight The last argument specifies the serial number for the subplot.

Here we passed 1 as the argument, which creates the upper subplot

[IPython Terminal]


subplot(2, 1, 2)

If we execute the subplot command as
  • subplot inside the brackets 2 comma 1 comma 2

the lower subplot is created.

[Plot Window] Here, we can see two subplots.
[IPython Terminal]

x = linspace(0, 50, 500)

subplot(2, 1, 1)

plot(x, cos(x))

y = linspace(0, 5, 100)

subplot(2, 1, 2)

plot(y, y ** 2)


Now we can draw plots in each of the subplot area using the plot command.

Type

subplot inside brackets 2 comma 1 comma 1

plot inside brackets x comma cos(x)


Now type

subplot inside brackets 2 comma 1 comma 2

plot y comma y square

[Plot window] This created two plots one in each of the subplot area.
  • The top subplot holds a cosine curve
  • And the bottom subplot holds a parabola.
[Plot window] It is clear from the two subplots that both have different regular axes.
[Plot window] For the cosine curve, x-axis varies from 0 to 50 and y-axis varies from minus 1 to 1.
[Plot window] For the parabolic curve, x-axis varies from 0 to 5 and y-axis varies from 0 to 25.
[IPython Terminal]

clf()

Now clear the plot window
Show Slide

Exercise 1

  • Pressure, Volume and Temperatures are held by the equation PV = nRT where nR is a constant.
  • Assume nR =0.01 Joules/Kelvin and T = 200K. V can be in the range from 21cc to 100cc.


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


  1. Pressure, Volume and Temperatures are held by the equation Pv equals to nRT where nR is a constant.
  2. Assume nR equals to 0.01 Joules per Kelvin and T equals to 200K. V can be in the range from 21cc to 100cc.


Draw two different plots as subplots:

  • Pressure v/s Volume plot and
  • Pressure v/s Temperature plot.
Switch to the terminal for the solution.
[IPython Terminal]

v = linspace(21, 100, 500)

To start with, we have been given the range of Volume from which we can define the variable v.

v equals to linspace inside brackets 21 comma 100 comma 500

[IPython Terminal]

subplot(2, 1, 1)

plot(v, 2.0/v)

We can create first subplot and draw Pressure v/s Volume graph using this v.
  • subplot(2 comma 1 comma 1)
  • plot(v comma 2 point 0 by v)

We know that nRT is a constant which is equal to 2.0

since

  • nR = equals to 0.01 Joules per Kelvin
  • and T equals to 200 Kelvin
[IPython Terminal]

subplot(2, 1, 2)

plot(200, 2.0/v)

Now we can create the second subplot and draw the Pressure v/s Temperature plot as follows
  • subplot(2 comma 1 comma 2)
  • plot(200 comma 2 point 0 divided by v)
[IPython Terminal]

t = linspace(200, 200, 500)

We have an error now, telling x and y dimensions don't match.

So we will create same number of points for temperature.


Type

t equals to linspace inside brackets 200 comma 200 comma 500.

[IPython Terminal]


plot(t, 2.0/v)

Now, we have 500 values in t and each with the value 200 Kelvin.

Plotting this data, we get the required plot.


Type

plot inside brackets t comma 2 point 0 divided by v

[Plot Window] We can see two subplots of Pressure v/s Volume and Pressure v/s Temperature respectively.
Show Slide

Summary slide

This brings us to the end of this tutorial.


In this tutorial, we have learnt to create subplots and to switch between them.

Show Slide

Evaluation


Here are some self assessment questions for you to solve.
  1. Which of the following is correct?
    • subplot inside brackets numRows comma numCols, plotNum
    • subplot inside brackets numRows comma numCols
    • subplot inside brackets numCols comma numRows
Show Slide

Solutions

And the answer is first option.
  1. The subplot command takes three arguments namely the number of rows, number of columns and the plot number.
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