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

From Script | Spoken-Tutorial
Jump to: navigation, search
Line 220: Line 220:
 
'''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 278: Line 278:
 
Type '''figure(1)'''
 
Type '''figure(1)'''
  
'''plot inside the brackets x comma sin(x) comma inisde inverted commas b'''
+
'''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 inisde inverted commas g'''
+
'''plot inside the brackets x comma cos(x) comma inside inverted commas g'''
  
 
|-
 
|-
Line 324: Line 324:
 
Type  
 
Type  
  
'''title inside brackets inisde inverted commas cos(x)'''
+
'''title inside brackets inside inverted commas cos(x)'''
  
 
'''savefig inside brackets inside inverted commas cosine.png'''
 
'''savefig inside brackets inside inverted commas cosine.png'''
Line 330: Line 330:
 
'''figure(1)'''
 
'''figure(1)'''
  
'''title inside brackets inisde inverted commas sin(x)'''
+
'''title inside brackets inside inverted commas sin(x)'''
  
 
'''savefig inside brackets inside inverted commas sine.png'''
 
'''savefig inside brackets inside inverted commas sine.png'''
Line 448: Line 448:
  
 
# The '''command '''"'''figure()'''" can get us the individual '''plots''' separately.  
 
# The '''command '''"'''figure()'''" can get us the individual '''plots''' separately.  
# '''Legend inside brackets inside square brackets inside inverted commas sin(x) comma cos(x)'''
+
# '''Legend inside brackets inside square brackets inside inverted commas sin(x) comma inside inverted commas cos(x)'''
  
  

Revision as of 11:29, 7 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 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 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)

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 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')

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


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 _________ from IIT Bombay signing off.

Thank You

Contributors and Content Editors

Nancyvarkey, Nirmala Venkat, Vineeta