multiple-plots.rst
changeset 165 53df732199a1
child 184 a005328abdf0
equal deleted inserted replaced
164:1709bdcea9d1 165:53df732199a1
       
     1 Hello friends. Welcome to this spoken tutorial on Multiple plots.
       
     2 
       
     3 {{{ Show the slide containing the title }}}
       
     4 
       
     5 {{{ Show the slide containing the outline }}}
       
     6 
       
     7 In this tutorial, we will learn how to draw more than one plot, how to
       
     8 add legends to each plot to indicate what each plot represents. We
       
     9 will also learn how to switch between the plots and creating multiple
       
    10 plots with different regular axes which are also called as subplots.
       
    11 
       
    12 {{{ Shift to terminal and start ipython -pylab }}}
       
    13 
       
    14 To begin with let us start ipython with pylab, by typing::
       
    15 
       
    16   ipython -pylab
       
    17 
       
    18 on the terminal
       
    19 
       
    20 Let us first create set of points for our plot. For this we will use
       
    21 the command called linspace::
       
    22 
       
    23   x = linspace(0, 50, 10)
       
    24 
       
    25 linspace command creates 10 points in the interval between 0 and 50
       
    26 both inclusive. We assign these values to a variable called x.
       
    27 
       
    28 Now let us draw a plot simple sine plot using these points::
       
    29 
       
    30   plot(x, sin(x))
       
    31 
       
    32 This should give us a nice sine plot.
       
    33 
       
    34 {{{ Switch to the plot window }}}
       
    35 
       
    36 Oh! wait! Is that a nice sine plot? Does a sine plot actually look
       
    37 like that? We know that a sine plot is a smooth curve is it not? What
       
    38 really caused this?
       
    39 
       
    40 {{{ pause for a while }}}
       
    41 
       
    42 A small investigation on linspace tells us that we chose too few
       
    43 points in a large interval between 0 and 50 for the curve to be
       
    44 smooth. So now let us use linspace again to get 500 points between 0
       
    45 and 100 and draw the sine plot
       
    46 
       
    47 {{{ Switch to ipython andtype }}} ::
       
    48 
       
    49   y = linspace(0, 50, 500)
       
    50   plot(y, sin(y))
       
    51 
       
    52 {{{ Change to the plot window }}}
       
    53 
       
    54 Now we see what we remember as a sine plot. A smooth curve. If we
       
    55 carefully notice we also have two plots now one overlaid upon
       
    56 another. In pylab, by default all the plots are overlaid.
       
    57 
       
    58 We now know how to draw multiple plots but we would like to have more
       
    59 control over it. Like switch between them, perform some operations or
       
    60 labelling on them individually and so on. Let us see how to accomplish
       
    61 this. Before we move on, let us clear our screen.
       
    62 
       
    63 {{{ Switch to ipython }}}::
       
    64 
       
    65   clf()
       
    66 
       
    67 To accomplishing more control over individual plots we use the figure
       
    68 command::
       
    69 
       
    70   x = linspace(0, 50, 500)
       
    71   figure(1)
       
    72   plot(x, sin(x), 'b')
       
    73   figure(2)
       
    74   plot(x, cos(x), 'g')
       
    75 
       
    76 {{{ Switch to plot window }}}
       
    77 
       
    78 Now we have two plots, a sine plot and a cosine plot one overlaid upon
       
    79 the other.
       
    80 
       
    81 {{{ Have both plot window and ipython side by side }}}
       
    82 
       
    83 The figure command takes an integer as an argument which is the serial
       
    84 number of the plot. This selects the corresponding plot. All the plot
       
    85 commands we run after this are applied to the selected plot. In this
       
    86 example figure 1 is the sine plot and figure 2 is the cosine plot. We
       
    87 can, for example, save each plot separately
       
    88 
       
    89 {{{ Switch to ipython }}}::
       
    90 
       
    91   savefig('/home/user/cosine.png')
       
    92   figure(1)
       
    93   title('sin(y)')
       
    94   savefig('/home/user/sine.png')
       
    95 
       
    96 {{{ Have both plot window and ipython side by side }}}
       
    97 
       
    98 We also titled the our first plot as 'sin(y)' which we did not do for
       
    99 the second plot.
       
   100 
       
   101 Since we have two plots now overlaid upon each other we would like to
       
   102 have a way to indicate what each plot represents to distinguish
       
   103 between them. This is accomplished using legends. Equivalently, the
       
   104 legend command does this for us
       
   105 
       
   106 {{{ Switch to ipython }}}::
       
   107 
       
   108   legend(['sin(x)', 'cos(x)'])
       
   109 
       
   110 The legend command takes a single list of parameters where each
       
   111 parameter is the text indicating the plots in the order of their
       
   112 serial number.
       
   113 
       
   114 {{{ Switch to plot window }}}
       
   115 
       
   116 Now we can see the legends being displayed for the respective sine and
       
   117 cosine plots on the plot area.
       
   118 
       
   119 At times we run into situations where we want to compare two plots and
       
   120 in such cases we want to draw both the plots in the same plotting
       
   121 area. The situation is such that the two plots have different regular
       
   122 axes which means we cannot draw overlaid plots. In such cases we can
       
   123 draw subplots.
       
   124 
       
   125 We use subplot command to accomplish this
       
   126 
       
   127 {{{ Switch to ipython }}}::
       
   128 
       
   129   subplot(2, 1, 1)
       
   130 
       
   131 subplot command takes three arguments, the first being the number of
       
   132 rows of subplots that must be created,
       
   133 
       
   134 {{{ Have both plot window and ipython side by side }}}
       
   135 
       
   136 in this case we have 2 so it spilts the plotting area horizontally for
       
   137 two subplots. The second argument specifies the number of coloumns of
       
   138 subplots that must be created. We passed 1 as the argument so the
       
   139 plotting area won't be split horizontally and the last argument
       
   140 specifies what subplot must be created now in the order of the serial
       
   141 number. In this case we passed 1 as the argument, so the first subplot
       
   142 that is top half is created. If we execute the subplot command as
       
   143 
       
   144 {{{ Switch to ipython }}}::
       
   145 
       
   146   subplot(2, 1, 2)
       
   147 
       
   148 {{{ Switch to plot window }}}
       
   149 
       
   150 The lower subplot is created. Now we can draw plots in each of the
       
   151 subplot area using the plot command.
       
   152 
       
   153 {{{ Switch to ipython }}}::
       
   154 
       
   155   x = linspace(0, 50, 500)
       
   156   plot(x, cos(x))
       
   157   subplot(2, 1, 1)
       
   158   y = linspace(0, 5, 100)
       
   159   plot(y, y ** 2)
       
   160 
       
   161 {{{ Have both plot window and ipython side by side }}}
       
   162 
       
   163 This created two plots one in each of the subplot area. The top
       
   164 subplot holds a parabola and the bottom subplot holds a cosine
       
   165 curve.
       
   166 
       
   167 As seen here we can use subplot command to switch between the subplot
       
   168 as well, but we have to use the same arguments as we used to create
       
   169 that subplot, otherwise the previous subplot at that place will be
       
   170 automatically erased. It is clear from the two subplots that both have
       
   171 different regular axes. For the cosine plot x-axis varies from 0 to
       
   172 100 and y-axis varies from 0 to 1 where as for the parabolic plot the
       
   173 x-axis varies from 0 to 10 and y-axis varies from 0 to 100
       
   174 
       
   175 {{{ Show summary slide }}}
       
   176 
       
   177 This brings us to the end of another session. In this tutorial session
       
   178 we learnt
       
   179 
       
   180  * How to draw multiple plots which are overlaid
       
   181  * the figure command
       
   182  * how to switch between the plots and perform some operations on each
       
   183    of them like saving the plots
       
   184  * the legend command and
       
   185  * creating and switching between subplots
       
   186 
       
   187 {{{ Show the "sponsored by FOSSEE" slide }}}
       
   188 
       
   189 This tutorial was created as a part of FOSSEE project, NME ICT, MHRD India
       
   190 
       
   191 Hope you have enjoyed and found it useful.
       
   192 Thankyou
       
   193  
       
   194 .. Author              : Madhu
       
   195    Internal Reviewer 1 :         [potential reviewer: Puneeth]
       
   196    Internal Reviewer 2 :         [potential reviewer: Nishanth]
       
   197    External Reviewer   :
       
   198