8 add legends to each plot to indicate what each plot represents. We |
8 add legends to each plot to indicate what each plot represents. We |
9 will also learn how to switch between the plots and create multiple |
9 will also learn how to switch between the plots and create multiple |
10 plots with different regular axes which are also called as subplots. |
10 plots with different regular axes which are also called as subplots. |
11 |
11 |
12 .. #[Nishanth]: See diff - edited a grammatical mistake |
12 .. #[Nishanth]: See diff - edited a grammatical mistake |
|
13 .. #[Madhu: Done] |
13 |
14 |
14 {{{ Shift to terminal and start ipython -pylab }}} |
15 {{{ Shift to terminal and start ipython -pylab }}} |
15 |
16 |
16 To begin with let us start ipython with pylab, by typing:: |
17 To begin with let us start ipython with pylab, by typing:: |
17 |
18 |
43 Oh! wait! Is that a nice sine plot? Does a sine plot actually look |
46 Oh! wait! Is that a nice sine plot? Does a sine plot actually look |
44 like that? We know that a sine plot is a smooth curve. Is it not? What |
47 like that? We know that a sine plot is a smooth curve. Is it not? What |
45 really caused this? |
48 really caused this? |
46 |
49 |
47 .. #[Nishanth]: See diff |
50 .. #[Nishanth]: See diff |
|
51 .. #[Madhu: Done] |
48 |
52 |
49 {{{ pause for a while }}} |
53 {{{ pause for a while }}} |
50 |
54 |
51 A small investigation on linspace tells us that we chose too few |
55 A small investigation on linspace tells us that we chose too few |
52 points in a large interval between 0 and 50 for the curve to be |
56 points in a large interval between 0 and 50 for the curve to be |
53 smooth. So now let us use linspace again to get 500 points between 0 |
57 smooth. This should also indicate that the plot command actually plots |
54 and 100 and draw the sine plot |
58 the set of points given by x and sin(x) and it doesn't plot the |
|
59 analytical function itself i.e. it plots the points given by |
|
60 Analytical functions. So now let us use linspace again to get 500 |
|
61 points between 0 and 100 and draw the sine plot |
55 |
62 |
56 .. #[Nishanth]: Here specify that when we do plot(x, sin(x) |
63 .. #[Nishanth]: Here specify that when we do plot(x, sin(x) |
57 it is actually plotting two sets of points |
64 it is actually plotting two sets of points |
58 and not analytical functions. Hence the sharp |
65 and not analytical functions. Hence the sharp |
59 curve. |
66 curve. |
|
67 .. #[Madhu: Incorporated] |
60 |
68 |
61 {{{ Switch to ipython andtype }}} :: |
69 {{{ Switch to ipython andtype }}} :: |
62 |
70 |
63 y = linspace(0, 50, 500) |
71 y = linspace(0, 50, 500) |
64 plot(y, sin(y)) |
72 plot(y, sin(y)) |
67 |
75 |
68 Now we see what we remember as a sine plot. A smooth curve. If we |
76 Now we see what we remember as a sine plot. A smooth curve. If we |
69 carefully notice we also have two plots now one overlaid upon |
77 carefully notice we also have two plots now one overlaid upon |
70 another. In pylab, by default all the plots are overlaid. |
78 another. In pylab, by default all the plots are overlaid. |
71 |
79 |
72 We now know how to draw multiple plots but we would like to have more |
80 Since we have two plots now overlaid upon each other we would like to |
73 control over it. Like switch between them, perform some operations or |
81 have a way to indicate what each plot represents to distinguish |
74 labelling on them individually and so on. Let us see how to accomplish |
82 between them. This is accomplished using legends. Equivalently, the |
75 this. Before we move on, let us clear our screen. |
83 legend command does this for us |
|
84 |
|
85 {{{ Switch to ipython }}}:: |
|
86 |
|
87 legend(['sin(x)', 'cos(x)']) |
|
88 |
|
89 .. #[Nishanth]: This legend may go up in the script. May be before |
|
90 introducing the figure command itself. |
|
91 .. #[Madhu: brought up] |
|
92 |
|
93 The legend command takes a single list of parameters where each |
|
94 parameter is the text indicating the plots in the order of their |
|
95 serial number. |
|
96 |
|
97 {{{ Switch to plot window }}} |
|
98 |
|
99 Now we can see the legends being displayed for the respective sine and |
|
100 cosine plots on the plot area. |
|
101 |
|
102 We have learnt quite a lot of things now, so let us take up an |
|
103 exercise problem. |
|
104 |
|
105 %% 1 %% Draw two plots overlaid upon each other, with the first plot |
|
106 being a parabola of the form y = 4(x ^ 2) and the second being a |
|
107 straight line of the form y = 2x + 3 in the interval -5 to 5. Use |
|
108 colors to differentiate between the plots and use legends to |
|
109 indicate what each plot is doing. |
|
110 |
|
111 {{{ pause for a while and continue from paused state }}} |
|
112 |
|
113 We can obtain the two plots in different colors using the following |
|
114 commands:: |
|
115 |
|
116 x = linspace(-5, 5, 100) |
|
117 plot(x, 4 * (x ^ 2), 'b') |
|
118 plot(x, (2 * x) + 3, 'g') |
|
119 |
|
120 Now we can use the legend command as:: |
|
121 |
|
122 legend(['Parabola', 'Straight Line']) |
|
123 |
|
124 Or we can also just give the equations of the plot:: |
|
125 |
|
126 legend(['y = 4(x ^ 2)', 'y = 2x + 3']) |
|
127 |
|
128 We now know how to draw multiple plots and use legends to indicate |
|
129 which plot represents what function, but we would like to have more |
|
130 control over the plots we draw. Like switch between them, perform some |
|
131 operations or labelling on them individually and so on. Let us see how |
|
132 to accomplish this. Before we move on, let us clear our screen. |
76 |
133 |
77 {{{ Switch to ipython }}}:: |
134 {{{ Switch to ipython }}}:: |
78 |
135 |
79 clf() |
136 clf() |
80 |
137 |
98 So clear the figure and plot cos and sin without |
155 So clear the figure and plot cos and sin without |
99 introducing figure command. Then introduce legend |
156 introducing figure command. Then introduce legend |
100 and finish off the everything on legend. |
157 and finish off the everything on legend. |
101 Then introduce figure command. |
158 Then introduce figure command. |
102 |
159 |
|
160 .. #[Madhu: I have just moved up the text about legend command. I |
|
161 think that should take care of what you suggested. If there is |
|
162 some mistake with it, Punch please let me know in your next |
|
163 review.] |
|
164 |
103 {{{ Have both plot window and ipython side by side }}} |
165 {{{ Have both plot window and ipython side by side }}} |
104 |
166 |
105 The figure command takes an integer as an argument which is the serial |
167 The figure command takes an integer as an argument which is the serial |
106 number of the plot. This selects the corresponding plot. All the plot |
168 number of the plot. This selects the corresponding plot. All the plot |
107 commands we run after this are applied to the selected plot. In this |
169 commands we run after this are applied to the selected plot. In this |
118 {{{ Have both plot window and ipython side by side }}} |
180 {{{ Have both plot window and ipython side by side }}} |
119 |
181 |
120 We also titled the our first plot as 'sin(y)' which we did not do for |
182 We also titled the our first plot as 'sin(y)' which we did not do for |
121 the second plot. |
183 the second plot. |
122 |
184 |
123 Since we have two plots now overlaid upon each other we would like to |
185 Let us attempt another exercise problem |
124 have a way to indicate what each plot represents to distinguish |
186 |
125 between them. This is accomplished using legends. Equivalently, the |
187 %% 2 %% Draw a line of the form y = x as one figure and another line |
126 legend command does this for us |
188 of the form y = 2x + 3. Switch back to the first figure, annotate |
127 |
189 the x and y intercepts. Now switch to the second figure and |
128 {{{ Switch to ipython }}}:: |
190 annotate its x and y intercepts. Save each of them. |
129 |
191 |
130 legend(['sin(x)', 'cos(x)']) |
192 {{{ Pause for a while and continue from the paused state }}} |
131 |
193 |
132 .. #[Nishanth]: This legend may go up in the script. May be before |
194 To solve this problem we should first create the first figure using |
133 introducing the figure command itself. |
195 the figure command. Before that, let us first run clf command to make |
134 |
196 sure all the previous plots are cleared:: |
135 The legend command takes a single list of parameters where each |
197 |
136 parameter is the text indicating the plots in the order of their |
198 clf() |
137 serial number. |
199 figure(1) |
138 |
200 x = linspace(-5, 5, 100) |
139 {{{ Switch to plot window }}} |
201 plot(x, x) |
140 |
202 |
141 Now we can see the legends being displayed for the respective sine and |
203 Now we can use figure command to create second plotting area and plot |
142 cosine plots on the plot area. |
204 the figure:: |
|
205 |
|
206 figure(2) |
|
207 plot(x, ((2 * x) + 3)) |
|
208 |
|
209 Now to switch between the figures we can use figure command. So let us |
|
210 switch to figure 1. We are asked to annotate x and y intercepts of the |
|
211 figure 1 but since figure 1 passes through origin we will have to |
|
212 annotate the origin. We will annotate the intercepts for the second |
|
213 figure and save them as follows:: |
|
214 |
|
215 figure(1) |
|
216 annotate('Origin', xy=(0.0, 0.0) |
|
217 figure(2) |
|
218 annotate('x-intercept', xy=(0, 3)) |
|
219 annotate('y-intercept', xy=(0, -1.5)) |
|
220 savefig('plot2.png') |
|
221 figure(1) |
|
222 savefig('plot1.png') |
143 |
223 |
144 At times we run into situations where we want to compare two plots and |
224 At times we run into situations where we want to compare two plots and |
145 in such cases we want to draw both the plots in the same plotting |
225 in such cases we want to draw both the plots in the same plotting |
146 area. The situation is such that the two plots have different regular |
226 area. The situation is such that the two plots have different regular |
147 axes which means we cannot draw overlaid plots. In such cases we can |
227 axes which means we cannot draw overlaid plots. In such cases we can |
195 automatically erased. It is clear from the two subplots that both have |
275 automatically erased. It is clear from the two subplots that both have |
196 different regular axes. For the cosine plot x-axis varies from 0 to |
276 different regular axes. For the cosine plot x-axis varies from 0 to |
197 100 and y-axis varies from 0 to 1 where as for the parabolic plot the |
277 100 and y-axis varies from 0 to 1 where as for the parabolic plot the |
198 x-axis varies from 0 to 10 and y-axis varies from 0 to 100 |
278 x-axis varies from 0 to 10 and y-axis varies from 0 to 100 |
199 |
279 |
200 .. #[Nishanth]: stress on the similarity between subplot and figure commands |
280 .. #[Nishanth]: stress on the similarity between subplot and figure |
|
281 commands |
|
282 |
|
283 .. #[Madhu: I think they are not really similar. Trying to bring in |
|
284 the similarity will confuse people I think.] |
|
285 |
|
286 %% 3 %% We know that the Pressure, Volume and Temperatures are held by |
|
287 the equation PV = nRT where nR is a constant. Let us assume nR = .01 |
|
288 Joules/Kelvin and T = 200K. V can be in the range from 21cc to |
|
289 100cc. Draw two different plots as subplots, one being the Pressure |
|
290 versus Volume plot and the other being Pressure versus Temparature |
|
291 plot. |
|
292 |
|
293 {{{ Pause for a while and continue }}} |
|
294 |
|
295 To start with, we have been given the range of Volume using which we |
|
296 can define the variable V:: |
|
297 |
|
298 V = linspace(21, 100, 500) |
|
299 |
|
300 Now we can create first subplot and draw Pressure versus Volume graph |
|
301 using this V. We know that nRT is a constant which is equal to 2.0 |
|
302 since nR = 0.01 Joules/Kelvin and T = 200 Kelvin:: |
|
303 |
|
304 subplot(2, 1, 1) |
|
305 plot(V, 2.0/V) |
|
306 |
|
307 Now we can create the second subplot and draw the Pressure versus |
|
308 Temparature plot as follows:: |
|
309 |
|
310 subplot(2, 1, 2) |
|
311 plot(200, 2.0/V) |
|
312 |
|
313 Unfortunately we have an error now, telling x and y dimensions don't |
|
314 match. This is because our V contains a set of values as returned by |
|
315 linspace and hence 2.0/V which is the pressure also contains a set of |
|
316 values. But the first argument to the plot command is a single |
|
317 value. So to plot this data we need to create as many points as there |
|
318 are in Pressure or Volume data for Temperature too, all having the |
|
319 same value. This can be accomplished using:: |
|
320 |
|
321 T = linspace(200, 200, 500) |
|
322 |
|
323 We now have 500 values in T each with the value 200 Kelvin. Plotting |
|
324 this data we get the required plot:: |
|
325 |
|
326 plot(T, 2.0/V) |
|
327 |
|
328 It is left as a homework to label both X and Y axes for each of the |
|
329 two subplots. |
201 |
330 |
202 {{{ Show summary slide }}} |
331 {{{ Show summary slide }}} |
203 |
332 |
204 .. #[Nishanth]: Exercises are missing in the script |
333 .. #[Nishanth]: Exercises are missing in the script |
205 one exercise for overlaid plot and legend |
334 one exercise for overlaid plot and legend |
209 This brings us to the end of another session. In this tutorial session |
338 This brings us to the end of another session. In this tutorial session |
210 we learnt |
339 we learnt |
211 |
340 |
212 * How to draw multiple plots which are overlaid |
341 * How to draw multiple plots which are overlaid |
213 * the figure command |
342 * the figure command |
|
343 * the legend command |
214 * how to switch between the plots and perform some operations on each |
344 * how to switch between the plots and perform some operations on each |
215 of them like saving the plots |
345 of them like saving the plots and |
216 * the legend command and |
|
217 * creating and switching between subplots |
346 * creating and switching between subplots |
218 |
347 |
219 .. #[Nishanth]: legend command can be told right after overlaid plots |
348 .. #[Nishanth]: legend command can be told right after overlaid plots |
|
349 .. #[Madhu: Incorporated] |
220 |
350 |
221 {{{ Show the "sponsored by FOSSEE" slide }}} |
351 {{{ Show the "sponsored by FOSSEE" slide }}} |
222 |
352 |
223 This tutorial was created as a part of FOSSEE project, NME ICT, MHRD India |
353 This tutorial was created as a part of FOSSEE project, NME ICT, MHRD India |
224 |
354 |