1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
|
.. Objectives
.. ----------
.. By the end of this tutorial you will be able to
.. * Modify the attributes of the plot -- color, line style, linewidth
.. * Add a title to the plot with embedded LaTeX.
.. * Label x and y axes.
.. * Add annotations to the plot.
.. * Set and Get the limits of axes.
.. Prerequisites
.. -------------
.. 1. Using the ``plot`` command interactively
.. Author : Nishanth Amuluru
Internal Reviewer : Anoop
External Reviewer :
Language Reviewe : Bhanukiran
Checklist OK? : <15-11-2010, Anand, OK> [2010-10-05]
Script
------
.. L1
{{{ Show the first slide containing title, name of the production
team along with the logo of MHRD }}}
.. R1
Hello friends and welcome to the tutorial on Embellishing Plots.
.. L2
{{{ Show the slide containing objectives }}}
.. R2
At the end of this tutorial, you will be able to,
1. Modify the attributes of the plot -- color, line style,linewidth.
#. Add a title to the plot with embedded LaTeX.
#. Label x and y axes.
#. Add annotations to the plot.
#. Set and Get the limits of axes.
.. R3
Let us start ipython with pylab loaded,open the terminal and type
::
ipython -pylab
.. L3
{{{ open the terminal and type ipython -pylab }}}
.. R4
We shall first make a simple plot and start decorating it.
.. L4
::
x = linspace(-2, 4, 20)
plot(x, sin(x))
.. R5
As we can see, the default colour and the default thickness of the
line is as decided by pylab. Wouldn't it be nice if we could control
these parameters in the plot? This is possible by passing additional
arguments to the plot command.
The additional argument that we shall be passing in here now is the
colour argument. We shall first clear the figure and plot the same now in
red colour.
.. L5
::
clf()
plot(x, sin(x), 'r')
.. R6
As we can see we have the same plot but now in red colour.
.. L6
{{{Move the mouse pointer on the plot}}}
.. R7
To alter the thickness of the line, we use the ``linewidth`` argument
in the plot command.
.. L7
::
plot(x, cos(x), linewidth=2)
.. R8
This produces a plot with a thicker line, to be more precise plot with line
thickness 2.
.. L8
{{{ Show the plot and compare the sine and cos plots }}}
.. R9
Let's try out the following exercise
Plot sin(x) in blue colour and with linewidth as 3.
.. L9
{{{ Show slide with Question1 }}}
.. R10
A combination of colour and linewidth would do the job for us.
.. L10
::
clf()
plot(x, sin(x), 'b', linewidth=3)
.. R11
Occasionally we would also want to alter the style of line. Sometimes
all we want is just a bunch of points not joined. This is possible by
passing the linestyle argument along with or instead of the colour
argument.
.. L11
::
clf()
plot(x, sin(x), '.')
.. R12
Hence we get a plot with only points.
.. L12
{{{Point at the dots on the plot}}}
.. R13
We can produce the same plot but now in blue colour.
.. L13
::
clf()
plot(x, sin(x), 'b.')
.. R14
Other available options can be seen in the documentation of plot.
.. L14
::
plot?
{{{ Run through the documentation and show the options available }}}
{{{ Show the options available for line style and colors }}}
.. L15
{{{Show slide containing question 2}}}
.. R15
Try out the following exercises
Plot the sine curve with green filled circles.
.. R16
All we have to do is use a combination of linestyle and colour to acheive this.
.. L16
{{{ Switch to terminal and type the following commands }}}
::
clf()
plot(x, cos(x), 'go')
.. L17
{{{Show slide containing question 3}}}
.. R17
Let us try out one more exercise
Plot the curve of x vs tan(x) in red dashed line and linewidth 3.
.. L18
{{{ Switch to terminal and type the following commands }}}
::
clf()
plot(x, cos(x), 'r--')
.. R19
Now that we know how to produce a bare minimum plot with colour, style
and thickness of our interest, we shall look at decorating the plot.
.. R20
Let us start with a plot for the function -x^2 + 4x - 5.
.. L20
::
plot(x, -x*x + 4*x - 5, 'r', linewidth=2)
{{{ Show the plot window and switch back to terminal }}}
.. R21
We now have the plot in a colour and linewidth of our interest. As you
can see, the figure does not have any description describing the plot.
We will now add a title to the plot by using the ``title`` command.
.. L21
::
title("Parabolic function -x^2+4x-5")
{{{ Show the plot window and point to the title }}}
.. R22
The figure now has a title which describes what the plot is. The
``title`` command as you can see, takes a string as an argument and sets
the title accordingly.
The formatting in the title is messed and it does not look clean. You can imagine
what would be the situation if there were fractions and more complex functions
like log and exp. Wouldn't it be good if there was LaTeX like formatting?
That is also possible by adding a ``$`` sign before and after the part of the
string that should be in LaTeX style.
.. L22
::
title("Parabolic function $-x^2+4x-5$")
.. R23
As we can see we get the polynomial formatted properly.
.. L23
{{{ Point at the polynomial }}}
.. L24
{{{Show slide containing question 4}}}
.. R24
{{{ Let us try out the following exercise }}}
Change the title of the figure such that the whole title is formatted
in LaTeX style.
.. R25
The solution is to enclose the whole string in between $.
.. L25
::
title("$Parabolic function -x^2+4x-5$")
.. R26
Hence it gives a title that looks neatly formatted.
Although we have title, the plot is not complete without labelling x
and y axes. Hence we shall label x-axis to "x" and y-axis to "f(x)".
.. L26
::
xlabel("x")
.. L27
{{{ Switch to plot window and show the xlabel }}}
.. R27
As you can see, ``xlabel`` command takes a string as an argument,
similar to the ``title`` command and sets it as the label to x-axis.
.. R28
Similarly, ``ylabel("f(x)")`` sets the name of the y-axis as "f(x)"
.. L28
::
ylabel("f(x)")
{{{ Show the plot window and point to ylabel and switch back to the terminal }}}
.. L29
{{{Show slide containing question 5}}}
.. R29
Now lets try out this exercise.
Set the x and y labels as "x" and "f(x)" in LaTeX style.
Since we need LaTeX style formatting, all we have to do is enclose the string
in between two $.
.. L30
{{{ Pause for some time and then Show slide with answer 5 }}}
.. R30
{{{ Read out the commands on the slides }}}
xlabel("$x$")
ylabel("$f(x)$")
does the job for us.
.. L31
{{{ Show the plot window with clean labels }}}
.. R31
The plot is now almost complete. Except that we have still not seen how to
name the points. For example the point (2, -1) is the local maxima. We would
like to name the point accordingly. We can do this by using the function ``annotate``.
.. L31
{{{ Switch to terminal }}}
::
annotate("local maxima", xy=(2, -1))
{{{ Show the annotation that has appeared on the plot }}}
.. R32
As you can see, the first argument to ``annotate`` command is the name we would
like to mark the point as, and the second argument is the co-ordinates of the
point at which the name should appear. It is a tuple containing two numbers.
The first is x co-ordinate and second is y co-ordinate.
.. L32
{{{ Point at the annotate command while explaining}}}
.. R33
Let's try out this exercise.
Make an annotation called "root" at the point (-4, 0).
What happens to the first annotation ?
.. L33
{{{Show slide containing question 6}}}
.. L34
{{{Show slide with answer 6}}}
{{{ Switch to the terminal and type the command }}}
::
annotate("root", xy=(-4,0))
.. R34
As we can see, every annotate command makes a new annotation on the figure.
Now we have everything we need to decorate a plot. but the plot would be
incomplete if we can not set the limits of axes. This is possible using the
button on the plot window.
we shall look at how to get and set them from the terminal.We use "xlim()" and "ylim()" functions.
.. L35
::
xlim()
ylim()
.. R35
We see that ``xlim`` function returns the current x axis limits and ``ylim``
function returns the current y-axis limits.
Let us look at how to set the limits.
.. L36
::
xlim(-4, 5)
.. R36
We see the limits of x-axis are now set to -4 and 5.
.. R37
Similarly we set the limits of y-axis appropriately.
.. L37
::
ylim(-15, 2)
.. L38
{{{ Pause here and try out the following exercises }}}
{{{Show slide containing question 7 and read it out }}}
Set the limits of axes such that the area of interest is the rectangle (-1, -15) and (3, 0)
{{{ continue from paused state }}}
.. R38
As we can see, the lower upper limits of x-axis in the question are -1 and 3.
The limits of y-axis are -15 and 0.
.. L39
{{{Show slide with answer 7}}}
::
xlim(-1, 3)
ylim(-15, 0)
.. R39
Hence xlim with limits -1 & 3 and ylim with limits -15 & 0 gives us the required rectangle.
.. L40
{{{ Show summary slide }}}
.. R40
let's revise quickly what we have learnt today.
1. to Modify the attributes of plot by passing additional arguments.
#. to add title to a plot.
#. to incorporate LaTeX style formatting.
#. to label x and y axes.
#. to add annotations to a plot.
#. to set the limits of axes.
.. L41
{{{ Show the 'self assesment questions' slide}}}
.. R41
Here are some self assessment questions for you to solve.
1. Draw a plot of cosine graph between -2pi to 2pi with line thickness 4.
2. Read thorugh the documentation and find out is there a way to modify the
alignment of text in the command ``ylabel``.
- Yes
- No
3. How do you set the title as x^2-5x+6 in LaTex style formatting.
.. L42
{{{ solutions for the self assessment questions }}}
.. R42
And the answers,
1. In order to plot a cosine graph between the points -2pi and 2pi with line thickness 3,we use
the ``linspace`` and ``plot`` command as,
::
x = linspace(-2*pi, 2*pi)
plot(x, cos(x), linewidth=4)
2. No. We do not have an option to modify the alignment of text in the command ``ylabel``.
3. To set the title in LaTex style formatting,we write the equation between two dollar signs as,
::
title("$x^2-5x+6$")
.. L43
{{{ a thank you slide }}}
.. R43
Hope you have enjoyed and found it useful.
Thankyou!
|