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