author | anoop |
Thu, 16 Sep 2010 15:15:01 +0530 | |
changeset 159 | 8efa612b17e1 |
parent 138 | 85ed0d5d28f8 |
child 172 | 438e7bae3cf3 |
permissions | -rw-r--r-- |
129 | 1 |
Hello friends and welcome to the tutorial on Embellishing Plots |
2 |
||
3 |
{{{ Show the slide containing title }}} |
|
4 |
||
5 |
{{{ Show the slide containing the outline slide }}} |
|
6 |
||
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 |
|
9 |
then look at adding labels to x and y axes. we shall also look at adding |
|
10 |
annotations to the plot. |
|
11 |
||
138
85ed0d5d28f8
added a newline before :: so that a colon does not appear in html
nishanth
parents:
130
diff
changeset
|
12 |
Let us start ipython with pylab loaded, by typing |
85ed0d5d28f8
added a newline before :: so that a colon does not appear in html
nishanth
parents:
130
diff
changeset
|
13 |
:: |
129 | 14 |
|
15 |
ipython -pylab |
|
16 |
||
17 |
on the terminal |
|
18 |
||
159 | 19 |
{{{ shift to terminal and type ipython -pylab }}} |
129 | 20 |
|
138
85ed0d5d28f8
added a newline before :: so that a colon does not appear in html
nishanth
parents:
130
diff
changeset
|
21 |
We shall first make a simple plot and start with decorating it. |
85ed0d5d28f8
added a newline before :: so that a colon does not appear in html
nishanth
parents:
130
diff
changeset
|
22 |
:: |
129 | 23 |
|
24 |
x = linspace(-2, 4, 20) |
|
25 |
plot(x, sin(x)) |
|
26 |
||
27 |
As you can see, the colour and thickness of line as decided by pylab. It would |
|
28 |
be nice if we could control these parameters in the plot. This is possible by |
|
29 |
passing additional arguments to the plot command. |
|
30 |
||
159 | 31 |
.. #[[Anoop: I think it will be good to rephrase the sentence]] |
32 |
||
129 | 33 |
The second argument that we shall be passing is colour. We shall first clear |
159 | 34 |
the figure and plot the same in red colour. Hence |
138
85ed0d5d28f8
added a newline before :: so that a colon does not appear in html
nishanth
parents:
130
diff
changeset
|
35 |
:: |
129 | 36 |
|
37 |
clf() |
|
38 |
plot(x, sin(x), 'r') |
|
39 |
||
40 |
Plots the same curve but now in red colour. |
|
41 |
||
42 |
To alter the thickness of the line, we use the =linewidth= argument in the plot |
|
159 | 43 |
command. Hence |
138
85ed0d5d28f8
added a newline before :: so that a colon does not appear in html
nishanth
parents:
130
diff
changeset
|
44 |
:: |
129 | 45 |
|
46 |
plot(x, cos(x), linewidth=2) |
|
47 |
||
48 |
produces a plot with a thicker line. |
|
49 |
||
159 | 50 |
.. #[[Anoop: I guess it will be good if you say that it affects the |
51 |
same plot, as you have not cleared the figure]] |
|
52 |
||
53 |
{{{ Show the plot and compare the sine and cos plots }}} |
|
129 | 54 |
|
55 |
{{{ Pause here and try out the following exercises }}} |
|
56 |
||
159 | 57 |
.. #[[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 |
|
59 |
video, then I guess the three braces can be removed.]] |
|
60 |
||
129 | 61 |
%% 1 %% Plot sin(x) in blue colour and with linewidth as 3 |
62 |
||
63 |
{{{ continue from paused state }}} |
|
64 |
||
138
85ed0d5d28f8
added a newline before :: so that a colon does not appear in html
nishanth
parents:
130
diff
changeset
|
65 |
A combination of colour and linewidth would do the job for us. Hence |
85ed0d5d28f8
added a newline before :: so that a colon does not appear in html
nishanth
parents:
130
diff
changeset
|
66 |
:: |
129 | 67 |
|
68 |
plot(x, sin(x), 'b', linewidth=3) |
|
69 |
||
159 | 70 |
.. #[[Anoop: add clf()]] |
71 |
||
129 | 72 |
produces the required plot |
73 |
||
74 |
#[Nishanth]: I could not think of a SIMPLE recipe approach for introducing |
|
75 |
linestyle. Hence the naive approach. |
|
76 |
||
159 | 77 |
.. #[[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 |
|
79 |
plot"]] |
|
80 |
||
129 | 81 |
Occasionally we would also want to alter the style of line. Sometimes all we |
82 |
want is just a bunch of points not joined. This is possible by passing the |
|
138
85ed0d5d28f8
added a newline before :: so that a colon does not appear in html
nishanth
parents:
130
diff
changeset
|
83 |
linestyle argument along with or instead of the colour argument.Hence |
85ed0d5d28f8
added a newline before :: so that a colon does not appear in html
nishanth
parents:
130
diff
changeset
|
84 |
:: |
129 | 85 |
|
86 |
clf() |
|
87 |
plot(x, sin(x), '.') |
|
88 |
||
89 |
produces a plot with only points. |
|
90 |
||
138
85ed0d5d28f8
added a newline before :: so that a colon does not appear in html
nishanth
parents:
130
diff
changeset
|
91 |
To produce the same plot but now in blue colour, we do |
85ed0d5d28f8
added a newline before :: so that a colon does not appear in html
nishanth
parents:
130
diff
changeset
|
92 |
:: |
129 | 93 |
|
94 |
clf() |
|
95 |
plot(x, sin(x), 'b.') |
|
96 |
||
138
85ed0d5d28f8
added a newline before :: so that a colon does not appear in html
nishanth
parents:
130
diff
changeset
|
97 |
Other available options can be seen in the documentation of plot. |
85ed0d5d28f8
added a newline before :: so that a colon does not appear in html
nishanth
parents:
130
diff
changeset
|
98 |
:: |
129 | 99 |
|
100 |
plot? |
|
101 |
||
102 |
{{{ Run through the documentation and show the options available }}} |
|
103 |
||
104 |
{{{ Pause here and try out the following exercises }}} |
|
105 |
||
159 | 106 |
.. #[[Anoop: same question as above, should it be read out?]] |
107 |
||
129 | 108 |
%% 2 %% Plot the sine curve with green filled circles. |
109 |
||
110 |
{{{ continue from paused state }}} |
|
111 |
||
112 |
All we have to do is use a combination of linestyle and colour to acheive this. |
|
138
85ed0d5d28f8
added a newline before :: so that a colon does not appear in html
nishanth
parents:
130
diff
changeset
|
113 |
Hence |
85ed0d5d28f8
added a newline before :: so that a colon does not appear in html
nishanth
parents:
130
diff
changeset
|
114 |
:: |
129 | 115 |
|
116 |
clf() |
|
117 |
plot(x, cos(x), 'go') |
|
118 |
||
119 |
produces the required plot. |
|
120 |
||
121 |
{{{ Pause here and try out the following exercises }}} |
|
122 |
||
123 |
%% 3 %% Produce a plot of tangent curve with red dashed line and linewidth 3 |
|
124 |
||
125 |
{{{ continue from paused state }}} |
|
126 |
||
127 |
Now that we know how to produce a bare minimum plot with colour, style and |
|
128 |
thickness of our interest, we shall look at decorating the plot. |
|
129 |
||
138
85ed0d5d28f8
added a newline before :: so that a colon does not appear in html
nishanth
parents:
130
diff
changeset
|
130 |
Let us start with a plot of the function -x^2 + 4x - 5. |
85ed0d5d28f8
added a newline before :: so that a colon does not appear in html
nishanth
parents:
130
diff
changeset
|
131 |
:: |
129 | 132 |
|
133 |
plot(x, -x*x + 4*x - 5, 'r', linewidth=2) |
|
134 |
||
135 |
{{{ Show the plot window and switch back to terminal }}} |
|
136 |
||
137 |
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. |
|
139 |
||
138
85ed0d5d28f8
added a newline before :: so that a colon does not appear in html
nishanth
parents:
130
diff
changeset
|
140 |
We will now add a title to the plot by using the =title= command. |
85ed0d5d28f8
added a newline before :: so that a colon does not appear in html
nishanth
parents:
130
diff
changeset
|
141 |
:: |
129 | 142 |
|
143 |
title("Parabolic function -x^2+4x-5") |
|
144 |
||
145 |
{{{ Show the plot window and point to the title }}} |
|
146 |
The figure now has a title which describes what the plot is. |
|
147 |
The =title= command as you can see, takes a string as argument and set the |
|
148 |
title accordingly. |
|
149 |
||
150 |
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 |
|
152 |
like log and exp. Wouldn't it be good if there was LaTex like formatting. |
|
153 |
||
154 |
That is also possible by adding a $ sign before and after the part of the |
|
155 |
string that should be LaTex style. |
|
156 |
||
138
85ed0d5d28f8
added a newline before :: so that a colon does not appear in html
nishanth
parents:
130
diff
changeset
|
157 |
for instance, we can use |
85ed0d5d28f8
added a newline before :: so that a colon does not appear in html
nishanth
parents:
130
diff
changeset
|
158 |
:: |
129 | 159 |
|
160 |
title("Parabolic function $-x^2+4x-5$") |
|
161 |
||
162 |
and we get the polynomial formatted properly. |
|
163 |
||
130 | 164 |
#[Nishanth]: Unsure if I have to give this exercise since enclosing the whole |
165 |
string in LaTex style is not good |
|
166 |
||
159 | 167 |
.. #[[Anoop: I guess you can go ahead with the LaTex thing, it's cool!]] |
168 |
||
129 | 169 |
{{{ Pause here and try out the following exercises }}} |
170 |
||
171 |
%% 4 %% Change the title of the figure such that the whole title is formatted |
|
172 |
in LaTex style |
|
173 |
||
174 |
{{{ continue from the paused state }}} |
|
175 |
||
138
85ed0d5d28f8
added a newline before :: so that a colon does not appear in html
nishanth
parents:
130
diff
changeset
|
176 |
The solution is to enclose the whole string in between $. Hence, |
85ed0d5d28f8
added a newline before :: so that a colon does not appear in html
nishanth
parents:
130
diff
changeset
|
177 |
:: |
129 | 178 |
|
179 |
title("$Parabolic function -x^2+4x-5$") |
|
180 |
||
181 |
gives a title that looks neatly formatted. |
|
182 |
||
183 |
Although we have title, the plot is not complete without labelling x and y |
|
138
85ed0d5d28f8
added a newline before :: so that a colon does not appear in html
nishanth
parents:
130
diff
changeset
|
184 |
axes. Hence we shall label x-axis to "x" and y-axis to "f(x)" |
85ed0d5d28f8
added a newline before :: so that a colon does not appear in html
nishanth
parents:
130
diff
changeset
|
185 |
:: |
129 | 186 |
|
187 |
xlabel("x") |
|
188 |
||
189 |
{{{ Switch to plot window and show the xlabel }}} |
|
190 |
||
191 |
As you can see, =xlabel= command takes a string as argument, similar to the |
|
192 |
=title= command and sets it to x-axis. |
|
193 |
||
138
85ed0d5d28f8
added a newline before :: so that a colon does not appear in html
nishanth
parents:
130
diff
changeset
|
194 |
Similarly, |
85ed0d5d28f8
added a newline before :: so that a colon does not appear in html
nishanth
parents:
130
diff
changeset
|
195 |
:: |
129 | 196 |
|
197 |
ylabel("f(x)") |
|
198 |
||
199 |
sets the name of y-axis as "f(x)" |
|
200 |
||
201 |
{{{ Show the plot window and point to ylabel and switch back to terminal }}} |
|
202 |
||
203 |
{{{ Pause here and try out the following exercises }}} |
|
204 |
||
205 |
%% 5 %% Set the x and y labels as "x" and "f(x)" in LaTex style. |
|
206 |
||
207 |
{{{ continue from paused state }}} |
|
208 |
||
209 |
Since we need LaTex style formatting, all we have to do is enclose the string |
|
138
85ed0d5d28f8
added a newline before :: so that a colon does not appear in html
nishanth
parents:
130
diff
changeset
|
210 |
in between two $. Hence, |
85ed0d5d28f8
added a newline before :: so that a colon does not appear in html
nishanth
parents:
130
diff
changeset
|
211 |
:: |
129 | 212 |
|
213 |
xlabel("$x$") |
|
214 |
yalbel("$f(x)$") |
|
215 |
||
216 |
does the job for us. |
|
217 |
||
218 |
{{{ Show the plot window with clean labels }}} |
|
219 |
||
220 |
The plot is now almost complete. Except that we have still not seen how to |
|
221 |
name the points. For example the point (2, -1) is the local maxima. We would |
|
138
85ed0d5d28f8
added a newline before :: so that a colon does not appear in html
nishanth
parents:
130
diff
changeset
|
222 |
like to name the point accordingly. We can do this by using |
85ed0d5d28f8
added a newline before :: so that a colon does not appear in html
nishanth
parents:
130
diff
changeset
|
223 |
:: |
129 | 224 |
|
225 |
annotate("local maxima", xy=(2, -1)) |
|
226 |
||
227 |
{{{ Show the annotation that has appeared on the plot }}} |
|
228 |
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 |
|
230 |
name should appear. |
|
231 |
||
159 | 232 |
.. #[[Anoop: I think we should tell explicitely that xy takes a |
233 |
sequence or a tuple]] |
|
234 |
||
129 | 235 |
{{{ Pause here and try out the following exercises }}} |
236 |
||
237 |
%% 6 %% Make an annotation called "root" at the point (-4, 0) |
|
238 |
What happens to the first annotation ? |
|
239 |
||
240 |
{{{ continue from paused state }}} |
|
241 |
||
242 |
As we can see, every annotate command makes a new annotation on the figure. |
|
243 |
||
244 |
{{{ Show summary slide }}} |
|
245 |
||
246 |
we have looked at |
|
247 |
||
248 |
* Modifying the attributes of plot by passing additional arguments |
|
249 |
* How to add title |
|
250 |
* How to incorporate LaTex style formatting |
|
251 |
* How to label x and y axes |
|
252 |
* How to add annotations |
|
253 |
||
254 |
{{{ Show the "sponsored by FOSSEE" slide }}} |
|
255 |
||
256 |
#[Nishanth]: Will add this line after all of us fix on one. |
|
257 |
This tutorial was created as a part of FOSSEE project, NME ICT, MHRD India |
|
258 |
||
259 |
Hope you have enjoyed and found it useful. |
|
260 |
Thankyou |
|
261 |
||
262 |
.. Author : Nishanth |
|
159 | 263 |
Internal Reviewer 1 : Anoop |
129 | 264 |
Internal Reviewer 2 : |
265 |
External Reviewer : |