embellishing_a_plot.rst
changeset 252 0ff3f1a97068
parent 251 9bc78792904b
parent 238 c507e9c413c6
child 253 8a117c6e75f1
equal deleted inserted replaced
251:9bc78792904b 252:0ff3f1a97068
     1 .. Author              : Nishanth
       
     2    Internal Reviewer 1 : Anoop
       
     3    Internal Reviewer 2 : Madhu
       
     4    External Reviewer   :
       
     5 
       
     6 .. Prerequisites: using ``plot`` command
       
     7 
       
     8 Hello friends and welcome to the tutorial on Embellishing Plots.
       
     9 
       
    10 {{{ Show the slide containing title }}}
       
    11 
       
    12 {{{ Show the slide containing the outline }}}
       
    13 
       
    14 In this tutorial, we shall look at how to modify the colour, thickness and 
       
    15 linestyle of the plot. We shall then learn how to add title to the plot and 
       
    16 then look at adding labels to x and y axes. we shall also look at adding 
       
    17 annotations to the plot and setting the limits of axes.
       
    18 
       
    19 Let us start ipython with pylab loaded, by typing on the terminal
       
    20 
       
    21 {{{ shift to terminal and type ipython -pylab }}}
       
    22 
       
    23 ::
       
    24 
       
    25     ipython -pylab
       
    26 
       
    27 .. #[madhu: I feel the instructions should precede the actual action,
       
    28 
       
    29 since while recording we need to know before hand what we need to do]
       
    30 
       
    31 We shall first make a simple plot and start decorating it.
       
    32 
       
    33 .. #[madhu: start decorating it should be fine, with is not necessary]
       
    34 
       
    35 ::
       
    36 
       
    37     x = linspace(-2, 4, 20)
       
    38     plot(x, sin(x))
       
    39 
       
    40 .. #[madhu: Standard is to choose between -50 to 50 or 0 to 50 with 100
       
    41      points right?]
       
    42 
       
    43 As we can see, the default colour and the default thickness of the
       
    44 line is as decided by pylab. Wouldn't be nice if we could control
       
    45 these parameters in the plot? This is possible by passing additional
       
    46 arguments to the plot command.
       
    47 
       
    48 .. #[[Anoop: I think it will be good to rephrase the sentence]]
       
    49 .. #[madhu: Why "you" here? Shouldn't this be "we" as decided? Also I
       
    50      added "the default" check the diff]
       
    51 
       
    52 The additional argument that we shall be passing in here now is the
       
    53 colour argument. We shall first clear the figure and plot the same in
       
    54 red colour. Hence
       
    55 
       
    56 .. #[Madhu: Note the diff for changes]
       
    57  ::
       
    58 
       
    59     clf()
       
    60     plot(x, sin(x), 'r')
       
    61 
       
    62 As we can see we have the same plot but now in red colour.
       
    63 
       
    64 .. #[Madhu: diff again]
       
    65 
       
    66 To alter the thickness of the line, we use the ``linewidth`` argument in the plot
       
    67 command. Hence
       
    68 ::
       
    69 
       
    70     plot(x, cos(x), linewidth=2)
       
    71 
       
    72 produces a plot with a thicker line, to be more precise plot with line
       
    73 thickness 2.
       
    74 
       
    75 .. #[[Anoop: I guess it will be good if you say that it affects the
       
    76    same plot, as you have not cleared the figure]]
       
    77 .. #[Madhu: To Anoop, not necessary I feel since they can see it?]
       
    78 
       
    79 {{{ Show the plot and compare the sine and cos plots }}}
       
    80 
       
    81 {{{ Pause here and try out the following exercises }}}
       
    82 
       
    83 .. #[[Anoop: is the above a context switch for the person who does the
       
    84    recording, other wise if it an instruction to the person viewing
       
    85    the video, then I guess the three braces can be removed.]]
       
    86 
       
    87 %% 1 %% Plot sin(x) in blue colour and with linewidth as 3
       
    88 
       
    89 {{{ continue from paused state }}}
       
    90 
       
    91 A combination of colour and linewidth would do the job for us. Hence
       
    92 ::
       
    93 
       
    94     clf()
       
    95     plot(x, sin(x), 'b', linewidth=3)
       
    96 
       
    97 .. #[[Anoop: add clf()]]
       
    98 
       
    99 produces the required plot
       
   100 
       
   101 .. #[Nishanth]: I could not think of a SIMPLE recipe approach for
       
   102              introducing linestyle. Hence the naive approach.
       
   103 
       
   104 .. #[[Anoop: I guess the recipe is fine, but would be better if you
       
   105    add the problem statement rather than just saying "let's do a simple
       
   106    plot"]]
       
   107 
       
   108 .. #[Madhu: It is good enough.]
       
   109 
       
   110 Occasionally we would also want to alter the style of line. Sometimes
       
   111 all we want is just a bunch of points not joined. This is possible by
       
   112 passing the linestyle argument along with or instead of the colour
       
   113 argument. Hence ::
       
   114 
       
   115     clf()
       
   116     plot(x, sin(x), '.')
       
   117 
       
   118 produces a plot with only points.
       
   119 
       
   120 To produce the same plot but now in blue colour, we do
       
   121 ::
       
   122 
       
   123     clf()
       
   124     plot(x, sin(x), 'b.')
       
   125 
       
   126 Other available options can be seen in the documentation of plot.
       
   127 ::
       
   128 
       
   129     plot?
       
   130 
       
   131 {{{ Run through the documentation and show the options available }}}
       
   132 
       
   133 {{{ Show the options available for line style and colors }}}
       
   134 
       
   135 .. #[Madhu: The script needs to tell what needs to be shown or
       
   136      explained.]
       
   137 
       
   138 {{{ Pause here and try out the following exercises }}}
       
   139 
       
   140 .. #[[Anoop: same question as above, should it be read out?]]
       
   141 
       
   142 %% 2 %% Plot the sine curve with green filled circles.
       
   143 
       
   144 {{{ continue from paused state }}}
       
   145 
       
   146 All we have to do is use a combination of linestyle and colour to acheive this.
       
   147 Hence
       
   148 ::
       
   149 
       
   150     clf()
       
   151     plot(x, cos(x), 'go')
       
   152 
       
   153 produces the required plot.
       
   154 
       
   155 {{{ Pause here and try out the following exercises }}}
       
   156 
       
   157 %% 3 %% Plot the curve of x vs tan(x) in red dashed line and linewidth 3
       
   158 
       
   159 {{{ continue from paused state }}}
       
   160 
       
   161 .. #[Madhu: I did not understand the question]
       
   162 
       
   163 Now that we know how to produce a bare minimum plot with colour, style
       
   164 and thickness of our interest, we shall look at decorating the plot.
       
   165 
       
   166 Let us start with a plot of the function -x^2 + 4x - 5.
       
   167 ::
       
   168 
       
   169     plot(x, -x*x + 4*x - 5, 'r', linewidth=2)
       
   170 
       
   171 {{{ Show the plot window and switch back to terminal }}}
       
   172 
       
   173 We now have the plot in a colour and linewidth of our interest. As you can see,
       
   174 the figure does not have any description describing the plot.
       
   175 
       
   176 .. #[Madhu: Added "not". See the diff]
       
   177 
       
   178 We will now add a title to the plot by using the ``title`` command.
       
   179 ::
       
   180 
       
   181     title("Parabolic function -x^2+4x-5") 
       
   182 
       
   183 {{{ Show the plot window and point to the title }}}
       
   184 
       
   185 The figure now has a title which describes what the plot is. The
       
   186 ``title`` command as you can see, takes a string as an argument and sets
       
   187 the title accordingly.
       
   188 
       
   189 .. #[Madhu: See the diff]
       
   190 
       
   191 The formatting in title is messed and it does not look clean. You can imagine
       
   192 what would be the situation if there were fractions and more complex functions
       
   193 like log and exp. Wouldn't it be good if there was LaTex like formatting?
       
   194 
       
   195 That is also possible by adding a $ sign before and after the part of the 
       
   196 string that should be in LaTex style.
       
   197 
       
   198 for instance, we can use
       
   199 ::
       
   200 
       
   201     title("Parabolic function $-x^2+4x-5$")
       
   202 
       
   203 and we get the polynomial formatted properly.
       
   204 
       
   205 .. #[Nishanth]: Unsure if I have to give this exercise since enclosing the whole
       
   206              string in LaTex style is not good
       
   207 
       
   208 .. #[[Anoop: I guess you can go ahead with the LaTex thing, it's
       
   209      cool!]]
       
   210 .. #[Madhu: Instead of saying LaTeX style you can say Typeset math
       
   211      since that is how it is called as. I am not sure as well. It
       
   212      doesn't really solve the purpose]
       
   213 
       
   214 {{{ Pause here and try out the following exercises }}}
       
   215 
       
   216 %% 4 %% Change the title of the figure such that the whole title is formatted
       
   217         in LaTex style
       
   218 
       
   219 {{{ continue from the paused state }}}
       
   220 
       
   221 The solution is to enclose the whole string in between $. Hence,
       
   222 ::
       
   223 
       
   224     title("$Parabolic function -x^2+4x-5$")
       
   225 
       
   226 gives a title that looks neatly formatted.
       
   227 
       
   228 Although we have title, the plot is not complete without labelling x
       
   229 and y axes. Hence we shall label x-axis to "x" and y-axis to "f(x)" ::
       
   230 
       
   231     xlabel("x")
       
   232 
       
   233 {{{ Switch to plot window and show the xlabel }}}
       
   234 
       
   235 As you can see, ``xlabel`` command takes a string as an argument,
       
   236 similar to the ``title`` command and sets it as the label to x-axis.
       
   237 
       
   238 .. #[See the diff]
       
   239 
       
   240 Similarly,
       
   241 ::
       
   242 
       
   243     ylabel("f(x)")
       
   244 
       
   245 sets the name of the y-axis as "f(x)"
       
   246 
       
   247 {{{ Show the plot window and point to ylabel and switch back to the terminal }}}
       
   248 
       
   249 {{{ Pause here and try out the following exercises }}}
       
   250 
       
   251 %% 5 %% Set the x and y labels as "x" and "f(x)" in LaTex style.
       
   252 
       
   253 {{{ continue from paused state }}}
       
   254 
       
   255 Since we need LaTex style formatting, all we have to do is enclose the string
       
   256 in between two $. Hence,
       
   257 ::
       
   258 
       
   259     xlabel("$x$")
       
   260     yalbel("$f(x)$")
       
   261 
       
   262 does the job for us.
       
   263 
       
   264 {{{ Show the plot window with clean labels }}}
       
   265 
       
   266 The plot is now almost complete. Except that we have still not seen how to 
       
   267 name the points. For example the point (2, -1) is the local maxima. We would
       
   268 like to name the point accordingly. We can do this by using
       
   269 ::
       
   270 
       
   271     annotate("local maxima", xy=(2, -1))
       
   272 
       
   273 {{{ Show the annotation that has appeared on the plot }}}
       
   274 
       
   275 As you can see, the first argument to ``annotate`` command is the name we would
       
   276 like to mark the point as and the second argument is the co-ordinates of the
       
   277 point at which the name should appear. It is a sequence containing two numbers.
       
   278 The first is x co-ordinate and second is y co-ordinate.
       
   279 
       
   280 .. #[[Anoop: I think we should tell explicitely that xy takes a
       
   281    sequence or a tuple]]
       
   282 .. #[Madhu: Agreed to what anoop says and also that xy= is the point
       
   283      part should be rephrased I think.]
       
   284 
       
   285 {{{ Pause here and try out the following exercises }}}
       
   286 
       
   287 %% 6 %% Make an annotation called "root" at the point (-4, 0)
       
   288         What happens to the first annotation ?
       
   289 
       
   290 {{{ continue from paused state }}}
       
   291 
       
   292 As we can see, every annotate command makes a new annotation on the figure.
       
   293 
       
   294 Now we have everything we need to decorate a plot. but the plot would be
       
   295 incomplete if we can not set the limits of axes. This is possible using the
       
   296 button on the plot window.
       
   297 
       
   298 we shall look at how to get and set them from the script.
       
   299 ::
       
   300 
       
   301     xlim()
       
   302     ylim()
       
   303 
       
   304 We see that ``xlim`` function returns the current x axis limits and ylim
       
   305 function returns the current y-axis limits.
       
   306 
       
   307 Let us look at how to set the limits.
       
   308 ::
       
   309 
       
   310     xlim(-4, 5)
       
   311 
       
   312 We see the limits of x-axis are now set to -4 and 5.
       
   313 Similarly
       
   314 ::
       
   315 
       
   316     ylim(-15, 2)
       
   317 
       
   318 sets the limits of y-axis appropriately.
       
   319 
       
   320 {{{ Pause here and try out the following exercises }}}
       
   321 
       
   322 %% 7 %% Set the limits of axes such that the area of interest is the rectangle
       
   323         (-1, -15) and (3, 0)
       
   324 
       
   325 {{{ continue from paused state }}}
       
   326 
       
   327 As we can see, the lower upper limits of x-axis in the question are -1 and 3.
       
   328 The limits of y-axis are -15 and 0.
       
   329 
       
   330 ::
       
   331 
       
   332     xlim(-1, 3)
       
   333     ylim(-15, 0)
       
   334 
       
   335 Gives us the required rectangle.
       
   336 
       
   337 {{{ Show summary slide }}}
       
   338 
       
   339 we have looked at 
       
   340 
       
   341  * Modifying the attributes of plot by passing additional arguments
       
   342  * How to add title
       
   343  * How to incorporate LaTex style formatting
       
   344  * How to label x and y axes
       
   345  * How to add annotations
       
   346  * How to set the limits of axes
       
   347 
       
   348 {{{ Show the "sponsored by FOSSEE" slide }}}
       
   349 
       
   350 .. #[Nishanth]: Will add this line after all of us fix on one.
       
   351 
       
   352 This tutorial was created as a part of FOSSEE project, NME ICT, MHRD India
       
   353 
       
   354 Hope you have enjoyed and found it useful.
       
   355 Thankyou
       
   356 
       
   357 Questions
       
   358 =========
       
   359 
       
   360  1. Draw a plot of cosine graph between -2pi to 2pi with line thickness 4
       
   361 
       
   362     Answer::
       
   363 
       
   364     x = linspace(-2*pi, 2*pi)
       
   365     plot(x, cos(x), linewidth=4)
       
   366 
       
   367  2. Draw a plot of the polynomial x^2-5x+6 in the range 0 to 5 in blue dotted
       
   368     line
       
   369 
       
   370     Answer::
       
   371 
       
   372     x = linspace(-2*pi, 2*pi)
       
   373     plot(x, x**2 - 5*x + 6, 'r.')
       
   374 
       
   375  3. Which marker is used to get circles
       
   376 
       
   377     a. '.'
       
   378     #. '^'
       
   379     #. 'o'
       
   380     #. '--'
       
   381 
       
   382  4. What does the '^' marker produce
       
   383 
       
   384     Answer: Triangle up marker
       
   385 
       
   386  5. How do you set the title as x^2-5x+6 in LaTex style formatting
       
   387 
       
   388     Answer: title("$x^2-5x+6$")
       
   389 
       
   390 6. What happens when the following code is executed::
       
   391 
       
   392     xlabel("First label")
       
   393     xlabel("Second label")
       
   394 
       
   395    Answer: The label of x-axis is set to "Second label"
       
   396 
       
   397  7. Read thorugh the documentation and find out is there a way to modify the
       
   398     alignment of text in the command ``ylabel``
       
   399 
       
   400    a. Yes
       
   401    #. No
       
   402 
       
   403    Answer: No
       
   404 
       
   405  8. How to add the annotation "Maxima" at the point (1, 2)
       
   406 
       
   407    Answer: annotate("Maxima", xy=(1, 2))
       
   408 
       
   409  9. Is the command ``annotate("max", (1, 2))`` same as ``annotate("max",
       
   410     xy=(1, 2)``
       
   411 
       
   412     a. True
       
   413     b. False
       
   414 
       
   415     Answer: True
       
   416 
       
   417  10. When a new annotation is made at a point, what happens to the old one
       
   418 
       
   419     a. It is replaced
       
   420     b. It is overwritten
       
   421     c. The new annotation is combined with old one
       
   422 
       
   423     Answer: It is overwritten
       
   424 
       
   425  11. What happens when xlim is used without arguments
       
   426 
       
   427     Answer: It gives the current limits of x-axis
       
   428 
       
   429  12. What happens when ``ylim(0, 5)`` is used
       
   430 
       
   431     Answer: It sets the lower and upper limits of y-axis to 0 and 5
       
   432 
       
   433  13. Draw a cosine plot from 0 to 2*pi with green dots. annotate the origin as
       
   434      "origin" and set x and y labels to "x" and cos(x) and x limits to 0 and
       
   435      2pi and y limits to -1.2 and 1.2
       
   436 
       
   437     Answer::
       
   438 
       
   439       x = linspace(0, 2*pi)
       
   440       plot(x, cos(x), 'g.')
       
   441       annotate("origin", (0, 0))
       
   442       xlabel("$x$")
       
   443       ylabel("$cos(x)$")
       
   444       xlim(0, 2*pi)
       
   445       ylim(-1.2, 1.2)
       
   446