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,...")
 
Line 93: Line 93:
  
  
Type '''x = linspace inside the brackets (0, 50, 10)'''
+
Type '''x = linspace inside the brackets 0 comma 50 comma 10'''
  
 
|-
 
|-
Line 102: Line 102:
  
  
Type '''plot inside the brackets (x, sin(x))'''
+
Type '''plot inside the brackets x comma sin(x)'''
  
  
Line 121: Line 121:
 
'''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 138: Line 138:
 
Type  
 
Type  
  
'''y = linspace inside the brackets (0, 50, 500)'''
+
'''y = linspace inside the brackets 0 comma 50 comma 500.'''
  
'''plotinside the brackets (y, sin(y))'''
+
'''plotinside the brackets y comma sin(y)'''
  
 
|-
 
|-
Line 159: Line 159:
 
| 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 commas sin(x) comma sin(y).'''
  
  
Line 211: Line 211:
 
| 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 = linspace inside the brackets minus 5 comma 5 comma 100)'''
  
  
Line 218: Line 218:
 
Type  
 
Type  
  
'''plot inside the brackets (x, 4 * (x * x)) '''
+
'''plot inside the brackets x comma 4 multiplied by inisde 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 228: Line 228:
 
| 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 inisde inverted commas dollar y equals to 4xsquare dollar comma r inisde inverted commas dollar y equals to 2x plus 3 dollar. '''
  
 
|-
 
|-
Line 271: Line 271:
 
| 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'''  
 
| 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 = linspace inside the brackets (0, 50, 500) '''
+
Type '''x equals to linspace inside the brackets 0 comma 50 comma 500 '''
  
  
Line 278: Line 278:
 
Type '''figure(1)'''
 
Type '''figure(1)'''
  
'''plot inside the brackets (x, sin(x), 'b')'''
+
'''plot inside the brackets x comma sin(x) comma inisde inverted commas b'''
  
 
'''figure(2)'''
 
'''figure(2)'''
  
'''plot inside the brackets (x, cos(x), 'g')'''
+
'''plot inside the brackets x comma cos(x) comma inisde inverted commas g'''
  
 
|-
 
|-
Line 324: Line 324:
 
Type  
 
Type  
  
'''title('cos(x)')'''
+
'''title inside brackets inisde inverted commas cos(x)'''
  
'''savefig('cosine.png')'''
+
'''savefig inside brackets inisde inverted commas cosine.png'''
  
 
'''figure(1)'''
 
'''figure(1)'''
  
'''title('sin(x)')'''
+
'''title inside brackets inisde inverted commas sin(x)'''
  
'''savefig('sine.png')'''
+
'''savefig inside brackets inisde inverted commas sine.png'''
  
 
The figures will be saved in current working directory. Now close both the plot windows.
 
The figures will be saved in current working directory. Now close both the plot windows.
Line 363: Line 363:
 
'''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 379:
 
'''figure(2) '''
 
'''figure(2) '''
  
'''plot(x, ((2 * x) + 3))'''
+
'''plot inside the brackets x comma (2 * x) plus 3))'''
  
 
|-
 
|-
Line 402: Line 402:
 
'''figure(1)'''
 
'''figure(1)'''
  
'''savefig('plot1.png')'''
+
'''savefig inside brackets inisde inverted commas plot1.png '''
  
 
'''figure(2)'''
 
'''figure(2)'''
  
'''savefig('plot2.png')'''
+
'''savefig inside brackets inisde 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'''.  
  
  

Revision as of 13:39, 15 February 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 %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 = 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 = linspace inside the brackets 0 comma 50 comma 500.

plotinside 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 commas 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 = 4(x ^ 2) and the second being a straight line of the form y = 2x + 3 in the interval -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 = 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 inisde 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 inisde inverted commas dollar y equals to 4xsquare dollar comma r inisde 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)

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


To accomplish more control over individual plots we use the figure command

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.


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 inisde inverted commas cos(x)

savefig inside brackets inisde inverted commas cosine.png

figure(1)

title inside brackets inisde inverted commas sin(x)

savefig inside brackets inisde 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 = x as one figure and another line of the form y = 2x + 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 (2 * x) plus 3))

[IPython Terminal]


figure(1)

savefig('plot1.png')

figure(2)

savefig('plot2.png')

Now to switch between the figures we can use figure command.


We will save the figure as follows

Type


figure(1)

savefig inside brackets inisde inverted commas plot1.png

figure(2)

savefig inside brackets inisde 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(['sin(x)','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 _________ from IIT Bombay signing off.

Thank You

Contributors and Content Editors

Nancyvarkey, Nirmala Venkat, Vineeta