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