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