multiple-plots/script.rst
author Puneeth Chaganti <punchagan@fossee.in>
Wed, 13 Oct 2010 11:13:46 +0530
changeset 314 11869b16d86b
parent 311 3f942b8d3f2f
child 511 caf39009044d
permissions -rw-r--r--
Getting started with sagenotebook LO - script and questions.
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   :
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    23
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    24
Script
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    25
------
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
{{{ Show the slide containing the title }}}
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
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
    30
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    31
{{{ Show the slide containing the outline }}}
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    32
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    33
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
    34
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
    35
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
    36
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
    37
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    38
.. #[Nishanth]: See diff - edited a grammatical mistake
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    39
.. #[Madhu: Done]
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
{{{ Shift to terminal and start ipython -pylab }}}
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    42
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    43
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
    44
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    45
  ipython -pylab
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    46
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    47
on the terminal
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    48
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    49
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
    50
the command called linspace::
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
  x = linspace(0, 50, 10)
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    53
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    54
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
    55
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
    56
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    57
.. #[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
    58
                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
    59
                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
    60
                there also.
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    61
.. #[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
    62
     better to give a very short intro to it]
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    63
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    64
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
    65
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    66
  plot(x, sin(x))
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    67
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    68
This should give us a nice sine plot.
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    69
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    70
{{{ Switch to the plot window }}}
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    71
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    72
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
    73
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
    74
really caused this?
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    75
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    76
.. #[Nishanth]: See diff
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    77
.. #[Madhu: Done]
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
{{{ pause for a while }}}
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    80
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    81
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
    82
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
    83
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
    84
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
    85
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
    86
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
    87
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
    88
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    89
.. #[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
    90
                it is actually plotting two sets of points
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    91
                and not analytical functions. Hence the sharp 
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    92
                curve.
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    93
.. #[Madhu: Incorporated]
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    94
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    95
{{{ Switch to ipython andtype }}} ::
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    96
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    97
  y = linspace(0, 50, 500)
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    98
  plot(y, sin(y))
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
{{{ Change to the plot window }}}
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   101
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   102
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
   103
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
   104
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
   105
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   106
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
   107
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
   108
between them. This is accomplished using legends. Equivalently, the
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   109
legend command does this for us
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   110
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   111
{{{ Switch to ipython }}}::
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   112
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   113
  legend(['sin(x)', 'cos(x)'])
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   114
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   115
.. #[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
   116
                introducing the figure command itself.
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   117
.. #[Madhu: brought up]
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   118
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   119
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
   120
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
   121
serial number.
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   122
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   123
{{{ Switch to plot window }}}
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   124
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   125
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
   126
cosine plots on the plot area.
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
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
   129
exercise problem.
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
%% 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
   132
   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
   133
   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
   134
   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
   135
   indicate what each plot is doing.
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   136
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   137
{{{ pause for a while and continue from paused state }}}
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   138
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   139
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
   140
commands::
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
  x = linspace(-5, 5, 100)
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   143
  plot(x, 4 * (x * x), 'b')
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   144
  plot(x, (2 * x) + 3, 'g')
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   145
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   146
Now we can use the legend command as::
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   147
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   148
  legend(['Parabola', 'Straight Line'])
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   149
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   150
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
   151
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   152
  legend(['y = 4(x ^ 2)', 'y = 2x + 3'])
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   153
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   154
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
   155
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
   156
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
   157
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
   158
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
   159
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   160
{{{ Switch to ipython }}}::
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   161
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   162
  clf()
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   163
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   164
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
   165
command::
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
  x = linspace(0, 50, 500)
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   168
  figure(1)
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   169
  plot(x, sin(x), 'b')
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   170
  figure(2)
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   171
  plot(x, cos(x), 'g')
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   172
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   173
{{{ Switch to plot window }}}
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   174
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   175
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
   176
figures.
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
.. #[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
   179
                The remaining script moves on the fact that they 
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   180
                give overlaid plots which is not the case.
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   181
                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
   182
                introducing figure command. Then introduce legend
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   183
                and finish off the everything on legend.
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   184
                Then introduce figure command.
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   185
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   186
.. #[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
   187
     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
   188
     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
   189
     review.]
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   190
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   191
{{{ Have both plot window and ipython side by side }}}
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   192
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   193
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
   194
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
   195
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
   196
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
   197
can, for example, save each plot separately
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   198
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   199
{{{ Switch to ipython }}}::
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   200
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   201
  savefig('/home/user/cosine.png')
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   202
  figure(1)
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   203
  title('sin(y)')
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   204
  savefig('/home/user/sine.png')
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   205
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   206
{{{ Have both plot window and ipython side by side }}}
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   207
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   208
We also titled the our first plot as 'sin(y)' which we did not do for
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   209
the second plot.
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   210
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   211
Let us attempt another exercise problem
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   212
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   213
%% 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
   214
   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
   215
   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
   216
   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
   217
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   218
{{{ 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
   219
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   220
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
   221
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
   222
sure all the previous plots are cleared::
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   223
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   224
  clf()
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   225
  figure(1)
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   226
  x = linspace(-5, 5, 100)
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   227
  plot(x, x)
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   228
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   229
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
   230
the figure::
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
  figure(2)
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   233
  plot(x, ((2 * x) + 3))
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
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
   236
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
   237
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
   238
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
   239
figure and save them as follows::
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   240
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   241
  figure(1)
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   242
  annotate('Origin', xy=(0.0, 0.0)
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   243
  figure(2)
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   244
  annotate('x-intercept', xy=(0, 3))
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   245
  annotate('y-intercept', xy=(0, -1.5))
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   246
  savefig('/home/fossee/plot2.png')
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   247
  figure(1)
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   248
  savefig('/home/fossee/plot1.png')
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   249
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   250
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
   251
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
   252
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
   253
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
   254
draw subplots.
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   255
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   256
We use subplot command to accomplish this
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   257
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   258
{{{ Switch to ipython }}}::
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   259
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   260
  subplot(2, 1, 1)
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   261
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   262
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
   263
rows of subplots that must be created,
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
{{{ Have both plot window and ipython side by side }}}
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   266
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   267
in this case we have 2 so it spilts the plotting area horizontally for
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   268
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
   269
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
   270
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
   271
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
   272
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
   273
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
   274
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   275
{{{ Switch to ipython }}}::
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   276
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   277
  subplot(2, 1, 2)
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   278
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   279
{{{ Switch to plot window }}}
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   280
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   281
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
   282
subplot area using the plot command.
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
{{{ Switch to ipython }}}::
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   285
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   286
  x = linspace(0, 50, 500)
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   287
  plot(x, cos(x))
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   288
  subplot(2, 1, 1)
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   289
  y = linspace(0, 5, 100)
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   290
  plot(y, y ** 2)
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   291
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   292
{{{ Have both plot window and ipython side by side }}}
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   293
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   294
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
   295
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
   296
curve.
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   297
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   298
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
   299
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
   300
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
   301
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
   302
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
   303
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
   304
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
   305
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   306
.. #[Nishanth]: stress on the similarity between subplot and figure
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   307
     commands
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
.. #[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
   310
     the similarity will confuse people I think.]
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
%% 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
   313
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
   314
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
   315
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
   316
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
   317
plot.
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   318
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   319
{{{ Pause for a while and continue }}}
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   320
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   321
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
   322
can define the variable V::
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
  V = linspace(21, 100, 500)
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   325
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   326
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
   327
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
   328
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
   329
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   330
  subplot(2, 1, 1)
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   331
  plot(V, 2.0/V)
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
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
   334
Temparature plot as follows::
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
  subplot(2, 1, 2)
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   337
  plot(200, 2.0/V)
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
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
   340
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
   341
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
   342
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
   343
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
   344
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
   345
same value. This can be accomplished using::
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   346
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   347
  T = linspace(200, 200, 500)
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   348
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   349
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
   350
this data we get the required plot::
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
  plot(T, 2.0/V)
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   353
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   354
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
   355
two subplots. 
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
{{{ Show summary slide }}}
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   358
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   359
.. #[Nishanth]: Exercises are missing in the script
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   360
                one exercise for overlaid plot and legend
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   361
                one for figure command
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   362
                one for subplot must do
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   363
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   364
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
   365
we learnt
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
 * How to draw multiple plots which are overlaid
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   368
 * the figure command
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   369
 * the legend command
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   370
 * 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
   371
   of them like saving the plots and
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   372
 * creating and switching between subplots
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   373
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   374
.. #[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
   375
.. #[Madhu: Incorporated]
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
{{{ Show the "sponsored by FOSSEE" slide }}}
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   378
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   379
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
   380
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   381
Hope you have enjoyed and found it useful.
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   382
Thank you!
3f942b8d3f2f Multiple plots LO - script and questions.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
   383