multiple-plots/script.rst
author anand
Mon, 15 Nov 2010 15:38:44 +0530
changeset 512 b378844641fd
parent 511 caf39009044d
permissions -rw-r--r--
checklist not ok; 1 short answer question and 2 long answer questions need to be added.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
311
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
     1
.. Objectives
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
     2
.. ----------
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
     3
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
     4
..  * How to draw multiple plots which are overlaid
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
     5
..  * the figure command
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
     6
..  * the legend command
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
     7
..  * how to switch between the plots and perform some operations on each
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
     8
..    of them like saving the plots and
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
     9
..  * creating and switching between subplots
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    10
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    11
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    12
.. Prerequisites
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    13
.. -------------
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    14
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    15
.. 1. using the plot command interactively
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    16
.. 2. embellishing a plot
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    17
.. 3. saving plots
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    18
     
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    19
.. Author              : Madhu
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    20
   Internal Reviewer 1 :         [potential reviewer: Puneeth]
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    21
   Internal Reviewer 2 : Nishanth
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    22
   External Reviewer   :
511
caf39009044d Language check done for `multiple plots`
bhanu
parents: 311
diff changeset
    23
   Language Reviewer   : Bhanukiran
512
b378844641fd checklist not ok; 1 short answer question and 2 long answer questions
anand
parents: 511
diff changeset
    24
   Checklist OK?       : <, not OK> []
b378844641fd checklist not ok; 1 short answer question and 2 long answer questions
anand
parents: 511
diff changeset
    25
311
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    26
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    27
Script
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    28
------
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    29
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    30
{{{ Show the slide containing the title }}}
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    31
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    32
Hello friends. Welcome to this spoken tutorial on Multiple plots.
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    33
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    34
{{{ Show the slide containing the outline }}}
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    35
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    36
In this tutorial, we will learn how to draw more than one plot, how to
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    37
add legends to each plot to indicate what each plot represents. We
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    38
will also learn how to switch between the plots and create multiple
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    39
plots with different regular axes which are also called as subplots.
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    40
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    41
.. #[Nishanth]: See diff - edited a grammatical mistake
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    42
.. #[Madhu: Done]
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    43
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    44
{{{ Shift to terminal and start ipython -pylab }}}
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    45
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    46
To begin with let us start ipython with pylab, by typing::
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    47
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    48
  ipython -pylab
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    49
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    50
on the terminal
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    51
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    52
Let us first create set of points for our plot. For this we will use
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    53
the command called linspace::
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    54
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    55
  x = linspace(0, 50, 10)
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    56
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    57
linspace command creates 10 points in the interval between 0 and 50
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    58
both inclusive. We assign these values to a variable called x.
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    59
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    60
.. #[Nishanth]: pre requisite for this LO is basic plotting which
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    61
                covers linspace and plot. So you may not need to 
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    62
                specify all that again. But not a problem if it is
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    63
                there also.
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    64
.. #[Madhu: Since I thought the LOs are disconnected, I thought it is
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    65
     better to give a very short intro to it]
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    66
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    67
Now let us draw a plot simple sine plot using these points::
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    68
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    69
  plot(x, sin(x))
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    70
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    71
This should give us a nice sine plot.
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    72
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    73
{{{ Switch to the plot window }}}
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    74
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    75
Oh! wait! Is that a nice sine plot? Does a sine plot actually look
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    76
like that? We know that a sine plot is a smooth curve. Is it not? What
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    77
really caused this?
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    78
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    79
.. #[Nishanth]: See diff
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    80
.. #[Madhu: Done]
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    81
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    82
{{{ pause for a while }}}
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    83
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    84
A small investigation on linspace tells us that we chose too few
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    85
points in a large interval between 0 and 50 for the curve to be
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    86
smooth. This should also indicate that the plot command actually plots
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    87
the set of points given by x and sin(x) and it doesn't plot the
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    88
analytical function itself i.e. it plots the points given by
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    89
Analytical functions. So now let us use linspace again to get 500
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    90
points between 0 and 100 and draw the sine plot
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    91
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    92
.. #[Nishanth]: Here specify that when we do plot(x, sin(x) 
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    93
                it is actually plotting two sets of points
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    94
                and not analytical functions. Hence the sharp 
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    95
                curve.
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    96
.. #[Madhu: Incorporated]
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    97
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    98
{{{ Switch to ipython andtype }}} ::
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    99
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   100
  y = linspace(0, 50, 500)
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   101
  plot(y, sin(y))
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   102
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   103
{{{ Change to the plot window }}}
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   104
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   105
Now we see what we remember as a sine plot. A smooth curve. If we
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   106
carefully notice we also have two plots now one overlaid upon
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   107
another. In pylab, by default all the plots are overlaid.
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   108
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   109
Since we have two plots now overlaid upon each other we would like to
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   110
have a way to indicate what each plot represents to distinguish
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   111
between them. This is accomplished using legends. Equivalently, the
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   112
legend command does this for us
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   113
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   114
{{{ Switch to ipython }}}::
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   115
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   116
  legend(['sin(x)', 'cos(x)'])
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   117
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   118
.. #[Nishanth]: This legend may go up in the script. May be before 
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   119
                introducing the figure command itself.
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   120
.. #[Madhu: brought up]
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   121
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   122
The legend command takes a single list of parameters where each
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   123
parameter is the text indicating the plots in the order of their
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   124
serial number.
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   125
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   126
{{{ Switch to plot window }}}
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   127
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   128
Now we can see the legends being displayed for the respective sine and
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   129
cosine plots on the plot area.
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   130
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   131
We have learnt quite a lot of things now, so let us take up an
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   132
exercise problem.
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   133
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   134
%% 1 %% Draw two plots overlaid upon each other, with the first plot
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   135
   being a parabola of the form y = 4(x ^ 2) and the second being a
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   136
   straight line of the form y = 2x + 3 in the interval -5 to 5. Use
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   137
   colors to differentiate between the plots and use legends to
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   138
   indicate what each plot is doing.
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   139
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   140
{{{ pause for a while and continue from paused state }}}
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   141
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   142
We can obtain the two plots in different colors using the following
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   143
commands::
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   144
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   145
  x = linspace(-5, 5, 100)
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   146
  plot(x, 4 * (x * x), 'b')
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   147
  plot(x, (2 * x) + 3, 'g')
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   148
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   149
Now we can use the legend command as::
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   150
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   151
  legend(['Parabola', 'Straight Line'])
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   152
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   153
Or we can also just give the equations of the plot::
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   154
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   155
  legend(['y = 4(x ^ 2)', 'y = 2x + 3'])
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   156
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   157
We now know how to draw multiple plots and use legends to indicate
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   158
which plot represents what function, but we would like to have more
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   159
control over the plots we draw. Like switch between them, perform some
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   160
operations or labelling on them individually and so on. Let us see how
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   161
to accomplish this. Before we move on, let us clear our screen.
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   162
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   163
{{{ Switch to ipython }}}::
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   164
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   165
  clf()
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   166
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   167
To accomplishing more control over individual plots we use the figure
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   168
command::
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   169
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   170
  x = linspace(0, 50, 500)
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   171
  figure(1)
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   172
  plot(x, sin(x), 'b')
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   173
  figure(2)
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   174
  plot(x, cos(x), 'g')
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   175
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   176
{{{ Switch to plot window }}}
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   177
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   178
Now we have two plots, a sine plot and a cosine plot in two different
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   179
figures.
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   180
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   181
.. #[Nishanth]: figure(1) and figure(2) give two different plots.
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   182
                The remaining script moves on the fact that they 
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   183
                give overlaid plots which is not the case.
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   184
                So clear the figure and plot cos and sin without
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   185
                introducing figure command. Then introduce legend
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   186
                and finish off the everything on legend.
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   187
                Then introduce figure command.
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   188
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   189
.. #[Madhu: I have just moved up the text about legend command. I
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   190
     think that should take care of what you suggested. If there is
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   191
     some mistake with it, Punch please let me know in your next
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   192
     review.]
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   193
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   194
{{{ Have both plot window and ipython side by side }}}
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   195
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   196
The figure command takes an integer as an argument which is the serial
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   197
number of the plot. This selects the corresponding plot. All the plot
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   198
commands we run after this are applied to the selected plot. In this
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   199
example figure 1 is the sine plot and figure 2 is the cosine plot. We
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   200
can, for example, save each plot separately
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   201
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   202
{{{ Switch to ipython }}}::
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   203
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   204
  savefig('/home/user/cosine.png')
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   205
  figure(1)
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   206
  title('sin(y)')
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   207
  savefig('/home/user/sine.png')
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   208
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   209
{{{ Have both plot window and ipython side by side }}}
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   210
511
caf39009044d Language check done for `multiple plots`
bhanu
parents: 311
diff changeset
   211
We also titled our first plot as 'sin(y)' which we did not do for
311
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   212
the second plot.
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   213
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   214
Let us attempt another exercise problem
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   215
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   216
%% 2 %% Draw a line of the form y = x as one figure and another line
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   217
   of the form y = 2x + 3. Switch back to the first figure, annotate
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   218
   the x and y intercepts. Now switch to the second figure and
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   219
   annotate its x and y intercepts. Save each of them.
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   220
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   221
{{{ Pause for a while and continue from the paused state }}}
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   222
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   223
To solve this problem we should first create the first figure using
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   224
the figure command. Before that, let us first run clf command to make
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   225
sure all the previous plots are cleared::
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   226
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   227
  clf()
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   228
  figure(1)
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   229
  x = linspace(-5, 5, 100)
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   230
  plot(x, x)
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   231
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   232
Now we can use figure command to create second plotting area and plot
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   233
the figure::
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   234
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   235
  figure(2)
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   236
  plot(x, ((2 * x) + 3))
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   237
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   238
Now to switch between the figures we can use figure command. So let us
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   239
switch to figure 1. We are asked to annotate x and y intercepts of the
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   240
figure 1 but since figure 1 passes through origin we will have to
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   241
annotate the origin. We will annotate the intercepts for the second
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   242
figure and save them as follows::
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   243
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   244
  figure(1)
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   245
  annotate('Origin', xy=(0.0, 0.0)
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   246
  figure(2)
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   247
  annotate('x-intercept', xy=(0, 3))
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   248
  annotate('y-intercept', xy=(0, -1.5))
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   249
  savefig('/home/fossee/plot2.png')
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   250
  figure(1)
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   251
  savefig('/home/fossee/plot1.png')
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   252
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   253
At times we run into situations where we want to compare two plots and
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   254
in such cases we want to draw both the plots in the same plotting
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   255
area. The situation is such that the two plots have different regular
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   256
axes which means we cannot draw overlaid plots. In such cases we can
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   257
draw subplots.
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   258
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   259
We use subplot command to accomplish this
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   260
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   261
{{{ Switch to ipython }}}::
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   262
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   263
  subplot(2, 1, 1)
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   264
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   265
subplot command takes three arguments, the first being the number of
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   266
rows of subplots that must be created,
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   267
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   268
{{{ Have both plot window and ipython side by side }}}
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   269
511
caf39009044d Language check done for `multiple plots`
bhanu
parents: 311
diff changeset
   270
in this case we have 2 as the first argument so it spilts the plotting area horizontally for
311
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   271
two subplots. The second argument specifies the number of coloumns of
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   272
subplots that must be created. We passed 1 as the argument so the
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   273
plotting area won't be split vertically and the last argument
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   274
specifies what subplot must be created now in the order of the serial
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   275
number. In this case we passed 1 as the argument, so the first subplot
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   276
that is top half is created. If we execute the subplot command as
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   277
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   278
{{{ Switch to ipython }}}::
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   279
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   280
  subplot(2, 1, 2)
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   281
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   282
{{{ Switch to plot window }}}
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   283
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   284
The lower subplot is created. Now we can draw plots in each of the
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   285
subplot area using the plot command.
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   286
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   287
{{{ Switch to ipython }}}::
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   288
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   289
  x = linspace(0, 50, 500)
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   290
  plot(x, cos(x))
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   291
  subplot(2, 1, 1)
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   292
  y = linspace(0, 5, 100)
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   293
  plot(y, y ** 2)
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   294
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   295
{{{ Have both plot window and ipython side by side }}}
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   296
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   297
This created two plots one in each of the subplot area. The top
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   298
subplot holds a parabola and the bottom subplot holds a cosine
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   299
curve.
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   300
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   301
As seen here we can use subplot command to switch between the subplot
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   302
as well, but we have to use the same arguments as we used to create
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   303
that subplot, otherwise the previous subplot at that place will be
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   304
automatically erased. It is clear from the two subplots that both have
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   305
different regular axes. For the cosine plot x-axis varies from 0 to
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   306
100 and y-axis varies from 0 to 1 where as for the parabolic plot the
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   307
x-axis varies from 0 to 10 and y-axis varies from 0 to 100
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   308
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   309
.. #[Nishanth]: stress on the similarity between subplot and figure
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   310
     commands
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   311
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   312
.. #[Madhu: I think they are not really similar. Trying to bring in
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   313
     the similarity will confuse people I think.]
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   314
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   315
%% 3 %% We know that the Pressure, Volume and Temperatures are held by
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   316
the equation PV = nRT where nR is a constant. Let us assume nR = .01
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   317
Joules/Kelvin and T = 200K. V can be in the range from 21cc to
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   318
100cc. Draw two different plots as subplots, one being the Pressure
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   319
versus Volume plot and the other being Pressure versus Temparature
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   320
plot.
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   321
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   322
{{{ Pause for a while and continue }}}
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   323
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   324
To start with, we have been given the range of Volume using which we
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   325
can define the variable V::
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   326
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   327
  V = linspace(21, 100, 500)
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   328
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   329
Now we can create first subplot and draw Pressure versus Volume graph
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   330
using this V. We know that nRT is a constant which is equal to 2.0
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   331
since nR = 0.01 Joules/Kelvin and T = 200 Kelvin::
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   332
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   333
  subplot(2, 1, 1)
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   334
  plot(V, 2.0/V)
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   335
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   336
Now we can create the second subplot and draw the Pressure versus
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   337
Temparature plot as follows::
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   338
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   339
  subplot(2, 1, 2)
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   340
  plot(200, 2.0/V)
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   341
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   342
Unfortunately we have an error now, telling x and y dimensions don't
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   343
match. This is because our V contains a set of values as returned by
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   344
linspace and hence 2.0/V which is the pressure also contains a set of
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   345
values. But the first argument to the plot command is a single
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   346
value. So to plot this data we need to create as many points as there
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   347
are in Pressure or Volume data for Temperature too, all having the
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   348
same value. This can be accomplished using::
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   349
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   350
  T = linspace(200, 200, 500)
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   351
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   352
We now have 500 values in T each with the value 200 Kelvin. Plotting
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   353
this data we get the required plot::
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   354
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   355
  plot(T, 2.0/V)
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   356
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   357
It is left as a homework to label both X and Y axes for each of the
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   358
two subplots. 
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   359
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   360
{{{ Show summary slide }}}
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   361
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   362
.. #[Nishanth]: Exercises are missing in the script
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   363
                one exercise for overlaid plot and legend
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   364
                one for figure command
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   365
                one for subplot must do
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   366
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   367
This brings us to the end of another session. In this tutorial session
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   368
we learnt
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   369
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   370
 * How to draw multiple plots which are overlaid
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   371
 * the figure command
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   372
 * the legend command
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   373
 * how to switch between the plots and perform some operations on each
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   374
   of them like saving the plots and
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   375
 * creating and switching between subplots
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   376
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   377
.. #[Nishanth]: legend command can be told right after overlaid plots
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   378
.. #[Madhu: Incorporated]
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   379
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   380
{{{ Show the "sponsored by FOSSEE" slide }}}
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   381
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   382
This tutorial was created as a part of FOSSEE project, NME ICT, MHRD India
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   383
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   384
Hope you have enjoyed and found it useful.
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   385
Thank you!
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   386