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