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