author | nishanth |
Wed, 15 Sep 2010 19:12:12 +0530 | |
changeset 138 | 85ed0d5d28f8 |
parent 130 | 5b04f1c63b16 |
child 159 | 8efa612b17e1 |
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 |
||
19 |
{{{ shit to terminal and type ipython -pylab }}} |
|
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 |
||
31 |
The second argument that we shall be passing is colour. We shall first clear |
|
138
85ed0d5d28f8
added a newline before :: so that a colon does not appear in html
nishanth
parents:
130
diff
changeset
|
32 |
the figure and plot the same in red colour.Hence |
85ed0d5d28f8
added a newline before :: so that a colon does not appear in html
nishanth
parents:
130
diff
changeset
|
33 |
:: |
129 | 34 |
|
35 |
clf() |
|
36 |
plot(x, sin(x), 'r') |
|
37 |
||
38 |
Plots the same curve but now in red colour. |
|
39 |
||
40 |
To alter the thickness of the line, we use the =linewidth= argument in the plot |
|
138
85ed0d5d28f8
added a newline before :: so that a colon does not appear in html
nishanth
parents:
130
diff
changeset
|
41 |
command.Hence |
85ed0d5d28f8
added a newline before :: so that a colon does not appear in html
nishanth
parents:
130
diff
changeset
|
42 |
:: |
129 | 43 |
|
44 |
plot(x, cos(x), linewidth=2) |
|
45 |
||
46 |
produces a plot with a thicker line. |
|
47 |
||
48 |
{{{ Show the plot and compare the sin and cos plots }}} |
|
49 |
||
50 |
{{{ Pause here and try out the following exercises }}} |
|
51 |
||
52 |
%% 1 %% Plot sin(x) in blue colour and with linewidth as 3 |
|
53 |
||
54 |
{{{ continue from paused state }}} |
|
55 |
||
138
85ed0d5d28f8
added a newline before :: so that a colon does not appear in html
nishanth
parents:
130
diff
changeset
|
56 |
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
|
57 |
:: |
129 | 58 |
|
59 |
plot(x, sin(x), 'b', linewidth=3) |
|
60 |
||
61 |
produces the required plot |
|
62 |
||
63 |
#[Nishanth]: I could not think of a SIMPLE recipe approach for introducing |
|
64 |
linestyle. Hence the naive approach. |
|
65 |
||
66 |
Occasionally we would also want to alter the style of line. Sometimes all we |
|
67 |
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
|
68 |
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
|
69 |
:: |
129 | 70 |
|
71 |
clf() |
|
72 |
plot(x, sin(x), '.') |
|
73 |
||
74 |
produces a plot with only points. |
|
75 |
||
138
85ed0d5d28f8
added a newline before :: so that a colon does not appear in html
nishanth
parents:
130
diff
changeset
|
76 |
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
|
77 |
:: |
129 | 78 |
|
79 |
clf() |
|
80 |
plot(x, sin(x), 'b.') |
|
81 |
||
138
85ed0d5d28f8
added a newline before :: so that a colon does not appear in html
nishanth
parents:
130
diff
changeset
|
82 |
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
|
83 |
:: |
129 | 84 |
|
85 |
plot? |
|
86 |
||
87 |
{{{ Run through the documentation and show the options available }}} |
|
88 |
||
89 |
{{{ Pause here and try out the following exercises }}} |
|
90 |
||
91 |
%% 2 %% Plot the sine curve with green filled circles. |
|
92 |
||
93 |
{{{ continue from paused state }}} |
|
94 |
||
95 |
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
|
96 |
Hence |
85ed0d5d28f8
added a newline before :: so that a colon does not appear in html
nishanth
parents:
130
diff
changeset
|
97 |
:: |
129 | 98 |
|
99 |
clf() |
|
100 |
plot(x, cos(x), 'go') |
|
101 |
||
102 |
produces the required plot. |
|
103 |
||
104 |
{{{ Pause here and try out the following exercises }}} |
|
105 |
||
106 |
%% 3 %% Produce a plot of tangent curve with red dashed line and linewidth 3 |
|
107 |
||
108 |
{{{ continue from paused state }}} |
|
109 |
||
110 |
Now that we know how to produce a bare minimum plot with colour, style and |
|
111 |
thickness of our interest, we shall look at decorating the plot. |
|
112 |
||
138
85ed0d5d28f8
added a newline before :: so that a colon does not appear in html
nishanth
parents:
130
diff
changeset
|
113 |
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
|
114 |
:: |
129 | 115 |
|
116 |
plot(x, -x*x + 4*x - 5, 'r', linewidth=2) |
|
117 |
||
118 |
{{{ Show the plot window and switch back to terminal }}} |
|
119 |
||
120 |
We now have the plot in a colour and linewidth of our interest. As you can see, |
|
121 |
the figure does have any description describing the plot. |
|
122 |
||
138
85ed0d5d28f8
added a newline before :: so that a colon does not appear in html
nishanth
parents:
130
diff
changeset
|
123 |
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
|
124 |
:: |
129 | 125 |
|
126 |
title("Parabolic function -x^2+4x-5") |
|
127 |
||
128 |
{{{ Show the plot window and point to the title }}} |
|
129 |
The figure now has a title which describes what the plot is. |
|
130 |
The =title= command as you can see, takes a string as argument and set the |
|
131 |
title accordingly. |
|
132 |
||
133 |
The formatting in title is messed and it does not look clean. You can imagine |
|
134 |
what would be the situation if there were fractions and more complex functions |
|
135 |
like log and exp. Wouldn't it be good if there was LaTex like formatting. |
|
136 |
||
137 |
That is also possible by adding a $ sign before and after the part of the |
|
138 |
string that should be LaTex style. |
|
139 |
||
138
85ed0d5d28f8
added a newline before :: so that a colon does not appear in html
nishanth
parents:
130
diff
changeset
|
140 |
for instance, we can use |
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 |
and we get the polynomial formatted properly. |
|
146 |
||
130 | 147 |
#[Nishanth]: Unsure if I have to give this exercise since enclosing the whole |
148 |
string in LaTex style is not good |
|
149 |
||
129 | 150 |
{{{ Pause here and try out the following exercises }}} |
151 |
||
152 |
%% 4 %% Change the title of the figure such that the whole title is formatted |
|
153 |
in LaTex style |
|
154 |
||
155 |
{{{ continue from the paused state }}} |
|
156 |
||
138
85ed0d5d28f8
added a newline before :: so that a colon does not appear in html
nishanth
parents:
130
diff
changeset
|
157 |
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
|
158 |
:: |
129 | 159 |
|
160 |
title("$Parabolic function -x^2+4x-5$") |
|
161 |
||
162 |
gives a title that looks neatly formatted. |
|
163 |
||
164 |
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
|
165 |
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
|
166 |
:: |
129 | 167 |
|
168 |
xlabel("x") |
|
169 |
||
170 |
{{{ Switch to plot window and show the xlabel }}} |
|
171 |
||
172 |
As you can see, =xlabel= command takes a string as argument, similar to the |
|
173 |
=title= command and sets it to x-axis. |
|
174 |
||
138
85ed0d5d28f8
added a newline before :: so that a colon does not appear in html
nishanth
parents:
130
diff
changeset
|
175 |
Similarly, |
85ed0d5d28f8
added a newline before :: so that a colon does not appear in html
nishanth
parents:
130
diff
changeset
|
176 |
:: |
129 | 177 |
|
178 |
ylabel("f(x)") |
|
179 |
||
180 |
sets the name of y-axis as "f(x)" |
|
181 |
||
182 |
{{{ Show the plot window and point to ylabel and switch back to terminal }}} |
|
183 |
||
184 |
{{{ Pause here and try out the following exercises }}} |
|
185 |
||
186 |
%% 5 %% Set the x and y labels as "x" and "f(x)" in LaTex style. |
|
187 |
||
188 |
{{{ continue from paused state }}} |
|
189 |
||
190 |
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
|
191 |
in between two $. Hence, |
85ed0d5d28f8
added a newline before :: so that a colon does not appear in html
nishanth
parents:
130
diff
changeset
|
192 |
:: |
129 | 193 |
|
194 |
xlabel("$x$") |
|
195 |
yalbel("$f(x)$") |
|
196 |
||
197 |
does the job for us. |
|
198 |
||
199 |
{{{ Show the plot window with clean labels }}} |
|
200 |
||
201 |
The plot is now almost complete. Except that we have still not seen how to |
|
202 |
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
|
203 |
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
|
204 |
:: |
129 | 205 |
|
206 |
annotate("local maxima", xy=(2, -1)) |
|
207 |
||
208 |
{{{ Show the annotation that has appeared on the plot }}} |
|
209 |
As you can see, the first argument to =annotate= command is the name we would |
|
210 |
like to mark the point as and the argument after xy= is the point at which the |
|
211 |
name should appear. |
|
212 |
||
213 |
{{{ Pause here and try out the following exercises }}} |
|
214 |
||
215 |
%% 6 %% Make an annotation called "root" at the point (-4, 0) |
|
216 |
What happens to the first annotation ? |
|
217 |
||
218 |
{{{ continue from paused state }}} |
|
219 |
||
220 |
As we can see, every annotate command makes a new annotation on the figure. |
|
221 |
||
222 |
{{{ Show summary slide }}} |
|
223 |
||
224 |
we have looked at |
|
225 |
||
226 |
* Modifying the attributes of plot by passing additional arguments |
|
227 |
* How to add title |
|
228 |
* How to incorporate LaTex style formatting |
|
229 |
* How to label x and y axes |
|
230 |
* How to add annotations |
|
231 |
||
232 |
{{{ Show the "sponsored by FOSSEE" slide }}} |
|
233 |
||
234 |
#[Nishanth]: Will add this line after all of us fix on one. |
|
235 |
This tutorial was created as a part of FOSSEE project, NME ICT, MHRD India |
|
236 |
||
237 |
Hope you have enjoyed and found it useful. |
|
238 |
Thankyou |
|
239 |
||
240 |
.. Author : Nishanth |
|
241 |
Internal Reviewer 1 : |
|
242 |
Internal Reviewer 2 : |
|
243 |
External Reviewer : |