embellishing_a_plot/script.rst
changeset 273 1639ef25a231
parent 235 80e4016d747a
child 328 9ab73cbdd5a8
equal deleted inserted replaced
272:08638b1e211e 273:1639ef25a231
     3 
     3 
     4 .. A - Students and teachers from Science and engineering backgrounds
     4 .. A - Students and teachers from Science and engineering backgrounds
     5    B - 
     5    B - 
     6    C - 
     6    C - 
     7    D - 
     7    D - 
       
     8 
       
     9 .. By the end of this tutorial you will be able to 
       
    10 
       
    11 ..  * Modify the attributes of the plot -- color, line style, linewidth
       
    12 ..  * Add a title to the plot with embedded LaTeX.
       
    13 ..  * Label x and y axes. 
       
    14 ..  * Add annotations to the plot. 
       
    15 ..  * Set and Get the limits of axes. 
       
    16 
     8 
    17 
     9 .. Prerequisites
    18 .. Prerequisites
    10 .. -------------
    19 .. -------------
    11 
    20 
    12 ..   1. Using the ``plot`` command interactively
    21 ..   1. Using the ``plot`` command interactively
   172 
   181 
   173 {{{ continue from paused state }}}
   182 {{{ continue from paused state }}}
   174 
   183 
   175 .. #[Madhu: I did not understand the question]
   184 .. #[Madhu: I did not understand the question]
   176 
   185 
       
   186 ::
       
   187     clf()
       
   188     plot(x, cos(x), 'r--')
       
   189 
   177 Now that we know how to produce a bare minimum plot with colour, style
   190 Now that we know how to produce a bare minimum plot with colour, style
   178 and thickness of our interest, we shall look at decorating the plot.
   191 and thickness of our interest, we shall look at decorating the plot.
   179 
   192 
   180 Let us start with a plot of the function -x^2 + 4x - 5.
   193 Let us start with a plot of the function -x^2 + 4x - 5.
   181 ::
   194 ::
   182 
   195 
   183     plot(x, -x*x + 4*x - 5, 'r', linewidth=2)
   196     plot(x, -x*x + 4*x - 5, 'r', linewidth=2)
   184 
   197 
   185 {{{ Show the plot window and switch back to terminal }}}
   198 {{{ Show the plot window and switch back to terminal }}}
   186 
   199 
   187 We now have the plot in a colour and linewidth of our interest. As you can see,
   200 We now have the plot in a colour and linewidth of our interest. As you
   188 the figure does not have any description describing the plot.
   201 can see, the figure does not have any description describing the plot.
   189 
   202 
   190 .. #[Madhu: Added "not". See the diff]
   203 .. #[Madhu: Added "not". See the diff]
   191 
   204 
   192 We will now add a title to the plot by using the ``title`` command.
   205 We will now add a title to the plot by using the ``title`` command.
   193 ::
   206 ::
   202 
   215 
   203 .. #[Madhu: See the diff]
   216 .. #[Madhu: See the diff]
   204 
   217 
   205 The formatting in title is messed and it does not look clean. You can imagine
   218 The formatting in title is messed and it does not look clean. You can imagine
   206 what would be the situation if there were fractions and more complex functions
   219 what would be the situation if there were fractions and more complex functions
   207 like log and exp. Wouldn't it be good if there was LaTex like formatting?
   220 like log and exp. Wouldn't it be good if there was LaTeX like formatting?
   208 
   221 
   209 That is also possible by adding a $ sign before and after the part of the 
   222 That is also possible by adding a $ sign before and after the part of the 
   210 string that should be in LaTex style.
   223 string that should be in LaTeX style.
   211 
   224 
   212 for instance, we can use
   225 for instance, we can use
   213 ::
   226 ::
   214 
   227 
   215     title("Parabolic function $-x^2+4x-5$")
   228     title("Parabolic function $-x^2+4x-5$")
   216 
   229 
   217 and we get the polynomial formatted properly.
   230 and we get the polynomial formatted properly.
   218 
   231 
   219 .. #[Nishanth]: Unsure if I have to give this exercise since enclosing the whole
   232 .. #[Nishanth]: Unsure if I have to give this exercise since enclosing the whole
   220              string in LaTex style is not good
   233              string in LaTeX style is not good
   221 
   234 
   222 .. #[[Anoop: I guess you can go ahead with the LaTex thing, it's
   235 .. #[[Anoop: I guess you can go ahead with the LaTeX thing, it's
   223      cool!]]
   236      cool!]]
   224 .. #[Madhu: Instead of saying LaTeX style you can say Typeset math
   237 .. #[Madhu: Instead of saying LaTeX style you can say Typeset math
   225      since that is how it is called as. I am not sure as well. It
   238      since that is how it is called as. I am not sure as well. It
   226      doesn't really solve the purpose]
   239      doesn't really solve the purpose]
   227 
   240 
   228 {{{ Pause here and try out the following exercises }}}
   241 {{{ Pause here and try out the following exercises }}}
   229 
   242 
   230 %% 4 %% Change the title of the figure such that the whole title is formatted
   243 %% 4 %% Change the title of the figure such that the whole title is formatted
   231         in LaTex style
   244         in LaTeX style
   232 
   245 
   233 {{{ continue from the paused state }}}
   246 {{{ continue from the paused state }}}
   234 
   247 
   235 The solution is to enclose the whole string in between $. Hence,
   248 The solution is to enclose the whole string in between $. Hence,
   236 ::
   249 ::
   260 
   273 
   261 {{{ Show the plot window and point to ylabel and switch back to the terminal }}}
   274 {{{ Show the plot window and point to ylabel and switch back to the terminal }}}
   262 
   275 
   263 {{{ Pause here and try out the following exercises }}}
   276 {{{ Pause here and try out the following exercises }}}
   264 
   277 
   265 %% 5 %% Set the x and y labels as "x" and "f(x)" in LaTex style.
   278 %% 5 %% Set the x and y labels as "x" and "f(x)" in LaTeX style.
   266 
   279 
   267 {{{ continue from paused state }}}
   280 {{{ continue from paused state }}}
   268 
   281 
   269 Since we need LaTex style formatting, all we have to do is enclose the string
   282 Since we need LaTeX style formatting, all we have to do is enclose the string
   270 in between two $. Hence,
   283 in between two $. Hence,
   271 ::
   284 ::
   272 
   285 
   273     xlabel("$x$")
   286     xlabel("$x$")
   274     yalbel("$f(x)$")
   287     yalbel("$f(x)$")
   301 %% 6 %% Make an annotation called "root" at the point (-4, 0)
   314 %% 6 %% Make an annotation called "root" at the point (-4, 0)
   302         What happens to the first annotation ?
   315         What happens to the first annotation ?
   303 
   316 
   304 {{{ continue from paused state }}}
   317 {{{ continue from paused state }}}
   305 
   318 
       
   319 ::
       
   320 
       
   321   annotate("root", xy=(-4,0))  
       
   322 
   306 As we can see, every annotate command makes a new annotation on the figure.
   323 As we can see, every annotate command makes a new annotation on the figure.
   307 
   324 
   308 Now we have everything we need to decorate a plot. but the plot would be
   325 Now we have everything we need to decorate a plot. but the plot would be
   309 incomplete if we can not set the limits of axes. This is possible using the
   326 incomplete if we can not set the limits of axes. This is possible using the
   310 button on the plot window.
   327 button on the plot window.
   352 
   369 
   353 we have looked at 
   370 we have looked at 
   354 
   371 
   355  * Modifying the attributes of plot by passing additional arguments
   372  * Modifying the attributes of plot by passing additional arguments
   356  * How to add title
   373  * How to add title
   357  * How to incorporate LaTex style formatting
   374  * How to incorporate LaTeX style formatting
   358  * How to label x and y axes
   375  * How to label x and y axes
   359  * How to add annotations
   376  * How to add annotations
   360  * How to set the limits of axes
   377  * How to set the limits of axes
   361 
   378 
   362 {{{ Show the "sponsored by FOSSEE" slide }}}
   379 {{{ Show the "sponsored by FOSSEE" slide }}}