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