1 Hello friends and welcome to the tutorial on Embellishing Plots |
1 Hello friends and welcome to the tutorial on Embellishing Plots. |
2 |
2 |
3 {{{ Show the slide containing title }}} |
3 {{{ Show the slide containing title }}} |
4 |
4 |
5 {{{ Show the slide containing the outline slide }}} |
5 {{{ Show the slide containing the outline }}} |
6 |
6 |
7 In this tutorial, we shall look at how to modify the colour, thickness and |
7 In this tutorial, we shall look at how to modify the colour, thickness and |
8 linestyle of the plot. We shall then learn how to add title to the plot and |
8 linestyle of the plot. We shall then learn how to add title to the plot and |
9 then look at adding labels to x and y axes. we shall also look at adding |
9 then look at adding labels to x and y axes. we shall also look at adding |
10 annotations to the plot. |
10 annotations to the plot. |
16 |
16 |
17 on the terminal |
17 on the terminal |
18 |
18 |
19 {{{ shift to terminal and type ipython -pylab }}} |
19 {{{ shift to terminal and type ipython -pylab }}} |
20 |
20 |
|
21 #[madhu: I feel the instructions should precede the actual action, |
|
22 since while recording we need to know before hand what we need to do] |
|
23 |
21 We shall first make a simple plot and start with decorating it. |
24 We shall first make a simple plot and start with decorating it. |
|
25 |
|
26 .. #[madhu: start decorating it should be fine, with is not necessary] |
|
27 |
22 :: |
28 :: |
23 |
29 |
24 x = linspace(-2, 4, 20) |
30 x = linspace(-2, 4, 20) |
25 plot(x, sin(x)) |
31 plot(x, sin(x)) |
26 |
32 |
27 As you can see, the colour and thickness of line as decided by pylab. It would |
33 .. #[madhu: Standard is to choose between -50 to 50 or 0 to 50 with 100 |
28 be nice if we could control these parameters in the plot. This is possible by |
34 points right?] |
29 passing additional arguments to the plot command. |
35 |
|
36 As you can see, the default colour and the default thickness of the |
|
37 line is as decided by pylab. Wouldn't be nice if we could control |
|
38 these parameters in the plot? Yes, this is possible by passing additional |
|
39 arguments to the plot command. |
30 |
40 |
31 .. #[[Anoop: I think it will be good to rephrase the sentence]] |
41 .. #[[Anoop: I think it will be good to rephrase the sentence]] |
32 |
42 .. #[madhu: Why "you" here? Shouldn't this be "we" as decided? Also I |
33 The second argument that we shall be passing is colour. We shall first clear |
43 added "the default" check the diff] |
34 the figure and plot the same in red colour. Hence |
44 |
35 :: |
45 The additional argument that we shall be passing in here now is the |
|
46 colour argument. We shall first clear the figure and plot the same in |
|
47 red colour. Hence |
|
48 |
|
49 .. #[Madhu: Note the diff for changes] |
|
50 :: |
36 |
51 |
37 clf() |
52 clf() |
38 plot(x, sin(x), 'r') |
53 plot(x, sin(x), 'r') |
39 |
54 |
40 Plots the same curve but now in red colour. |
55 As we can see we have the same plot but now in red colour. |
|
56 |
|
57 .. #[Madhu: diff again] |
41 |
58 |
42 To alter the thickness of the line, we use the =linewidth= argument in the plot |
59 To alter the thickness of the line, we use the =linewidth= argument in the plot |
43 command. Hence |
60 command. Hence |
44 :: |
61 :: |
45 |
62 |
46 plot(x, cos(x), linewidth=2) |
63 plot(x, cos(x), linewidth=2) |
47 |
64 |
48 produces a plot with a thicker line. |
65 produces a plot with a thicker line, to be more precise plot with line |
|
66 thickness 2. |
49 |
67 |
50 .. #[[Anoop: I guess it will be good if you say that it affects the |
68 .. #[[Anoop: I guess it will be good if you say that it affects the |
51 same plot, as you have not cleared the figure]] |
69 same plot, as you have not cleared the figure]] |
|
70 .. #[Madhu: To Anoop, not necessary I feel since they can see it?] |
52 |
71 |
53 {{{ Show the plot and compare the sine and cos plots }}} |
72 {{{ Show the plot and compare the sine and cos plots }}} |
54 |
73 |
55 {{{ Pause here and try out the following exercises }}} |
74 {{{ Pause here and try out the following exercises }}} |
56 |
75 |
57 .. #[[Anoop: is the above a context switch for the person who does the |
76 .. #[[Anoop: is the above a context switch for the person who does the |
58 recording, other wise if it an instruction to the person viewing the |
77 recording, other wise if it an instruction to the person viewing |
59 video, then I guess the three braces can be removed.]] |
78 the video, then I guess the three braces can be removed.]] |
60 |
79 |
61 %% 1 %% Plot sin(x) in blue colour and with linewidth as 3 |
80 %% 1 %% Plot sin(x) in blue colour and with linewidth as 3 |
62 |
81 |
63 {{{ continue from paused state }}} |
82 {{{ continue from paused state }}} |
64 |
83 |
69 |
88 |
70 .. #[[Anoop: add clf()]] |
89 .. #[[Anoop: add clf()]] |
71 |
90 |
72 produces the required plot |
91 produces the required plot |
73 |
92 |
74 #[Nishanth]: I could not think of a SIMPLE recipe approach for introducing |
93 #[Nishanth]: I could not think of a SIMPLE recipe approach for |
75 linestyle. Hence the naive approach. |
94 introducing linestyle. Hence the naive approach. |
76 |
95 |
77 .. #[[Anoop: I guess the recipe is fine, but would be better if you |
96 .. #[[Anoop: I guess the recipe is fine, but would be better if you |
78 add the problem statement rather than just saying "let's do a simple |
97 add the problem statement rather than just saying "let's do a simple |
79 plot"]] |
98 plot"]] |
80 |
99 |
81 Occasionally we would also want to alter the style of line. Sometimes all we |
100 .. #[Madhu: It is good enough.] |
82 want is just a bunch of points not joined. This is possible by passing the |
101 |
83 linestyle argument along with or instead of the colour argument.Hence |
102 Occasionally we would also want to alter the style of line. Sometimes |
84 :: |
103 all we want is just a bunch of points not joined. This is possible by |
|
104 passing the linestyle argument along with or instead of the colour |
|
105 argument. Hence :: |
85 |
106 |
86 clf() |
107 clf() |
87 plot(x, sin(x), '.') |
108 plot(x, sin(x), '.') |
88 |
109 |
89 produces a plot with only points. |
110 produces a plot with only points. |
122 |
146 |
123 %% 3 %% Produce a plot of tangent curve with red dashed line and linewidth 3 |
147 %% 3 %% Produce a plot of tangent curve with red dashed line and linewidth 3 |
124 |
148 |
125 {{{ continue from paused state }}} |
149 {{{ continue from paused state }}} |
126 |
150 |
127 Now that we know how to produce a bare minimum plot with colour, style and |
151 .. #[Madhu: I did not understand the question] |
128 thickness of our interest, we shall look at decorating the plot. |
152 |
|
153 Now that we know how to produce a bare minimum plot with colour, style |
|
154 and thickness of our interest, we shall look at decorating the plot. |
129 |
155 |
130 Let us start with a plot of the function -x^2 + 4x - 5. |
156 Let us start with a plot of the function -x^2 + 4x - 5. |
131 :: |
157 :: |
132 |
158 |
133 plot(x, -x*x + 4*x - 5, 'r', linewidth=2) |
159 plot(x, -x*x + 4*x - 5, 'r', linewidth=2) |
134 |
160 |
135 {{{ Show the plot window and switch back to terminal }}} |
161 {{{ Show the plot window and switch back to terminal }}} |
136 |
162 |
137 We now have the plot in a colour and linewidth of our interest. As you can see, |
163 We now have the plot in a colour and linewidth of our interest. As you can see, |
138 the figure does have any description describing the plot. |
164 the figure does not have any description describing the plot. |
|
165 |
|
166 .. #[Madhu: Added "not". See the diff] |
139 |
167 |
140 We will now add a title to the plot by using the =title= command. |
168 We will now add a title to the plot by using the =title= command. |
141 :: |
169 :: |
142 |
170 |
143 title("Parabolic function -x^2+4x-5") |
171 title("Parabolic function -x^2+4x-5") |
144 |
172 |
145 {{{ Show the plot window and point to the title }}} |
173 {{{ Show the plot window and point to the title }}} |
146 The figure now has a title which describes what the plot is. |
174 |
147 The =title= command as you can see, takes a string as argument and set the |
175 The figure now has a title which describes what the plot is. The |
148 title accordingly. |
176 =title= command as you can see, takes a string as an argument and sets |
|
177 the title accordingly. |
|
178 |
|
179 .. #[Madhu: See the diff] |
149 |
180 |
150 The formatting in title is messed and it does not look clean. You can imagine |
181 The formatting in title is messed and it does not look clean. You can imagine |
151 what would be the situation if there were fractions and more complex functions |
182 what would be the situation if there were fractions and more complex functions |
152 like log and exp. Wouldn't it be good if there was LaTex like formatting. |
183 like log and exp. Wouldn't it be good if there was LaTex like formatting? |
153 |
184 |
154 That is also possible by adding a $ sign before and after the part of the |
185 That is also possible by adding a $ sign before and after the part of the |
155 string that should be LaTex style. |
186 string that should be in LaTex style. |
156 |
187 |
157 for instance, we can use |
188 for instance, we can use |
158 :: |
189 :: |
159 |
190 |
160 title("Parabolic function $-x^2+4x-5$") |
191 title("Parabolic function $-x^2+4x-5$") |
162 and we get the polynomial formatted properly. |
193 and we get the polynomial formatted properly. |
163 |
194 |
164 #[Nishanth]: Unsure if I have to give this exercise since enclosing the whole |
195 #[Nishanth]: Unsure if I have to give this exercise since enclosing the whole |
165 string in LaTex style is not good |
196 string in LaTex style is not good |
166 |
197 |
167 .. #[[Anoop: I guess you can go ahead with the LaTex thing, it's cool!]] |
198 .. #[[Anoop: I guess you can go ahead with the LaTex thing, it's |
|
199 cool!]] |
|
200 .. #[Madhu: Instead of saying LaTeX style you can say Typeset math |
|
201 since that is how it is called as. I am not sure as well. It |
|
202 doesn't really solve the purpose] |
168 |
203 |
169 {{{ Pause here and try out the following exercises }}} |
204 {{{ Pause here and try out the following exercises }}} |
170 |
205 |
171 %% 4 %% Change the title of the figure such that the whole title is formatted |
206 %% 4 %% Change the title of the figure such that the whole title is formatted |
172 in LaTex style |
207 in LaTex style |
178 |
213 |
179 title("$Parabolic function -x^2+4x-5$") |
214 title("$Parabolic function -x^2+4x-5$") |
180 |
215 |
181 gives a title that looks neatly formatted. |
216 gives a title that looks neatly formatted. |
182 |
217 |
183 Although we have title, the plot is not complete without labelling x and y |
218 Although we have title, the plot is not complete without labelling x |
184 axes. Hence we shall label x-axis to "x" and y-axis to "f(x)" |
219 and y axes. Hence we shall label x-axis to "x" and y-axis to "f(x)" :: |
185 :: |
|
186 |
220 |
187 xlabel("x") |
221 xlabel("x") |
188 |
222 |
189 {{{ Switch to plot window and show the xlabel }}} |
223 {{{ Switch to plot window and show the xlabel }}} |
190 |
224 |
191 As you can see, =xlabel= command takes a string as argument, similar to the |
225 As you can see, =xlabel= command takes a string as an argument, |
192 =title= command and sets it to x-axis. |
226 similar to the =title= command and sets it as the label to x-axis. |
|
227 |
|
228 .. #[See the diff] |
193 |
229 |
194 Similarly, |
230 Similarly, |
195 :: |
231 :: |
196 |
232 |
197 ylabel("f(x)") |
233 ylabel("f(x)") |
198 |
234 |
199 sets the name of y-axis as "f(x)" |
235 sets the name of the y-axis as "f(x)" |
200 |
236 |
201 {{{ Show the plot window and point to ylabel and switch back to terminal }}} |
237 {{{ Show the plot window and point to ylabel and switch back to the terminal }}} |
202 |
238 |
203 {{{ Pause here and try out the following exercises }}} |
239 {{{ Pause here and try out the following exercises }}} |
204 |
240 |
205 %% 5 %% Set the x and y labels as "x" and "f(x)" in LaTex style. |
241 %% 5 %% Set the x and y labels as "x" and "f(x)" in LaTex style. |
206 |
242 |
223 :: |
259 :: |
224 |
260 |
225 annotate("local maxima", xy=(2, -1)) |
261 annotate("local maxima", xy=(2, -1)) |
226 |
262 |
227 {{{ Show the annotation that has appeared on the plot }}} |
263 {{{ Show the annotation that has appeared on the plot }}} |
|
264 |
228 As you can see, the first argument to =annotate= command is the name we would |
265 As you can see, the first argument to =annotate= command is the name we would |
229 like to mark the point as and the argument after xy= is the point at which the |
266 like to mark the point as and the argument after xy= is the point at which the |
230 name should appear. |
267 name should appear. |
231 |
268 |
232 .. #[[Anoop: I think we should tell explicitely that xy takes a |
269 .. #[[Anoop: I think we should tell explicitely that xy takes a |
233 sequence or a tuple]] |
270 sequence or a tuple]] |
|
271 .. #[Madhu: Agreed to what anoop says and also that xy= is the point |
|
272 part should be rephrased I think.] |
234 |
273 |
235 {{{ Pause here and try out the following exercises }}} |
274 {{{ Pause here and try out the following exercises }}} |
236 |
275 |
237 %% 6 %% Make an annotation called "root" at the point (-4, 0) |
276 %% 6 %% Make an annotation called "root" at the point (-4, 0) |
238 What happens to the first annotation ? |
277 What happens to the first annotation ? |