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