author | Puneeth Chaganti <punchagan@fossee.in> |
Thu, 07 Oct 2010 12:28:12 +0530 | |
changeset 242 | a33e942379d7 |
parent 214 | 8b08fc88d5a0 |
permissions | -rw-r--r-- |
214 | 1 |
.. Author : Nishanth |
2 |
Internal Reviewer 1 : Anoop |
|
3 |
Internal Reviewer 2 : Madhu |
|
4 |
External Reviewer : |
|
5 |
||
6 |
.. Prerequisites: using ``plot`` command |
|
7 |
||
172
438e7bae3cf3
Second Review for embellishing plot.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
159
diff
changeset
|
8 |
Hello friends and welcome to the tutorial on Embellishing Plots. |
129 | 9 |
|
10 |
{{{ Show the slide containing title }}} |
|
11 |
||
172
438e7bae3cf3
Second Review for embellishing plot.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
159
diff
changeset
|
12 |
{{{ Show the slide containing the outline }}} |
129 | 13 |
|
14 |
In this tutorial, we shall look at how to modify the colour, thickness and |
|
15 |
linestyle of the plot. We shall then learn how to add title to the plot and |
|
16 |
then look at adding labels to x and y axes. we shall also look at adding |
|
214 | 17 |
annotations to the plot and setting the limits of axes. |
129 | 18 |
|
173
24289d3a4fdb
added the changes suggested by anoop and madhu
Nishanth <nishanth@fossee.in>
parents:
172
diff
changeset
|
19 |
Let us start ipython with pylab loaded, by typing on the terminal |
24289d3a4fdb
added the changes suggested by anoop and madhu
Nishanth <nishanth@fossee.in>
parents:
172
diff
changeset
|
20 |
|
24289d3a4fdb
added the changes suggested by anoop and madhu
Nishanth <nishanth@fossee.in>
parents:
172
diff
changeset
|
21 |
{{{ shift to terminal and type ipython -pylab }}} |
24289d3a4fdb
added the changes suggested by anoop and madhu
Nishanth <nishanth@fossee.in>
parents:
172
diff
changeset
|
22 |
|
138
85ed0d5d28f8
added a newline before :: so that a colon does not appear in html
nishanth
parents:
130
diff
changeset
|
23 |
:: |
129 | 24 |
|
25 |
ipython -pylab |
|
26 |
||
190
6dcc77582e26
made the comments in my script as rst comments
Nishanth <nishanth@fossee.in>
parents:
173
diff
changeset
|
27 |
.. #[madhu: I feel the instructions should precede the actual action, |
191 | 28 |
|
172
438e7bae3cf3
Second Review for embellishing plot.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
159
diff
changeset
|
29 |
since while recording we need to know before hand what we need to do] |
438e7bae3cf3
Second Review for embellishing plot.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
159
diff
changeset
|
30 |
|
173
24289d3a4fdb
added the changes suggested by anoop and madhu
Nishanth <nishanth@fossee.in>
parents:
172
diff
changeset
|
31 |
We shall first make a simple plot and start decorating it. |
172
438e7bae3cf3
Second Review for embellishing plot.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
159
diff
changeset
|
32 |
|
438e7bae3cf3
Second Review for embellishing plot.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
159
diff
changeset
|
33 |
.. #[madhu: start decorating it should be fine, with is not necessary] |
438e7bae3cf3
Second Review for embellishing plot.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
159
diff
changeset
|
34 |
|
138
85ed0d5d28f8
added a newline before :: so that a colon does not appear in html
nishanth
parents:
130
diff
changeset
|
35 |
:: |
129 | 36 |
|
37 |
x = linspace(-2, 4, 20) |
|
38 |
plot(x, sin(x)) |
|
39 |
||
172
438e7bae3cf3
Second Review for embellishing plot.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
159
diff
changeset
|
40 |
.. #[madhu: Standard is to choose between -50 to 50 or 0 to 50 with 100 |
438e7bae3cf3
Second Review for embellishing plot.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
159
diff
changeset
|
41 |
points right?] |
438e7bae3cf3
Second Review for embellishing plot.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
159
diff
changeset
|
42 |
|
173
24289d3a4fdb
added the changes suggested by anoop and madhu
Nishanth <nishanth@fossee.in>
parents:
172
diff
changeset
|
43 |
As we can see, the default colour and the default thickness of the |
172
438e7bae3cf3
Second Review for embellishing plot.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
159
diff
changeset
|
44 |
line is as decided by pylab. Wouldn't be nice if we could control |
173
24289d3a4fdb
added the changes suggested by anoop and madhu
Nishanth <nishanth@fossee.in>
parents:
172
diff
changeset
|
45 |
these parameters in the plot? This is possible by passing additional |
172
438e7bae3cf3
Second Review for embellishing plot.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
159
diff
changeset
|
46 |
arguments to the plot command. |
129 | 47 |
|
159 | 48 |
.. #[[Anoop: I think it will be good to rephrase the sentence]] |
172
438e7bae3cf3
Second Review for embellishing plot.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
159
diff
changeset
|
49 |
.. #[madhu: Why "you" here? Shouldn't this be "we" as decided? Also I |
438e7bae3cf3
Second Review for embellishing plot.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
159
diff
changeset
|
50 |
added "the default" check the diff] |
159 | 51 |
|
172
438e7bae3cf3
Second Review for embellishing plot.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
159
diff
changeset
|
52 |
The additional argument that we shall be passing in here now is the |
438e7bae3cf3
Second Review for embellishing plot.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
159
diff
changeset
|
53 |
colour argument. We shall first clear the figure and plot the same in |
438e7bae3cf3
Second Review for embellishing plot.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
159
diff
changeset
|
54 |
red colour. Hence |
438e7bae3cf3
Second Review for embellishing plot.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
159
diff
changeset
|
55 |
|
438e7bae3cf3
Second Review for embellishing plot.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
159
diff
changeset
|
56 |
.. #[Madhu: Note the diff for changes] |
438e7bae3cf3
Second Review for embellishing plot.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
159
diff
changeset
|
57 |
:: |
129 | 58 |
|
59 |
clf() |
|
60 |
plot(x, sin(x), 'r') |
|
61 |
||
172
438e7bae3cf3
Second Review for embellishing plot.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
159
diff
changeset
|
62 |
As we can see we have the same plot but now in red colour. |
438e7bae3cf3
Second Review for embellishing plot.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
159
diff
changeset
|
63 |
|
438e7bae3cf3
Second Review for embellishing plot.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
159
diff
changeset
|
64 |
.. #[Madhu: diff again] |
129 | 65 |
|
193
0d175627e828
changed = to `` so that it shows properly
Nishanth <nishanth@fossee.in>
parents:
191
diff
changeset
|
66 |
To alter the thickness of the line, we use the ``linewidth`` argument in the plot |
159 | 67 |
command. Hence |
138
85ed0d5d28f8
added a newline before :: so that a colon does not appear in html
nishanth
parents:
130
diff
changeset
|
68 |
:: |
129 | 69 |
|
70 |
plot(x, cos(x), linewidth=2) |
|
71 |
||
172
438e7bae3cf3
Second Review for embellishing plot.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
159
diff
changeset
|
72 |
produces a plot with a thicker line, to be more precise plot with line |
438e7bae3cf3
Second Review for embellishing plot.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
159
diff
changeset
|
73 |
thickness 2. |
129 | 74 |
|
159 | 75 |
.. #[[Anoop: I guess it will be good if you say that it affects the |
76 |
same plot, as you have not cleared the figure]] |
|
172
438e7bae3cf3
Second Review for embellishing plot.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
159
diff
changeset
|
77 |
.. #[Madhu: To Anoop, not necessary I feel since they can see it?] |
159 | 78 |
|
79 |
{{{ Show the plot and compare the sine and cos plots }}} |
|
129 | 80 |
|
81 |
{{{ Pause here and try out the following exercises }}} |
|
82 |
||
159 | 83 |
.. #[[Anoop: is the above a context switch for the person who does the |
172
438e7bae3cf3
Second Review for embellishing plot.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
159
diff
changeset
|
84 |
recording, other wise if it an instruction to the person viewing |
438e7bae3cf3
Second Review for embellishing plot.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
159
diff
changeset
|
85 |
the video, then I guess the three braces can be removed.]] |
159 | 86 |
|
129 | 87 |
%% 1 %% Plot sin(x) in blue colour and with linewidth as 3 |
88 |
||
89 |
{{{ continue from paused state }}} |
|
90 |
||
138
85ed0d5d28f8
added a newline before :: so that a colon does not appear in html
nishanth
parents:
130
diff
changeset
|
91 |
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
|
92 |
:: |
129 | 93 |
|
173
24289d3a4fdb
added the changes suggested by anoop and madhu
Nishanth <nishanth@fossee.in>
parents:
172
diff
changeset
|
94 |
clf() |
129 | 95 |
plot(x, sin(x), 'b', linewidth=3) |
96 |
||
159 | 97 |
.. #[[Anoop: add clf()]] |
98 |
||
129 | 99 |
produces the required plot |
100 |
||
190
6dcc77582e26
made the comments in my script as rst comments
Nishanth <nishanth@fossee.in>
parents:
173
diff
changeset
|
101 |
.. #[Nishanth]: I could not think of a SIMPLE recipe approach for |
172
438e7bae3cf3
Second Review for embellishing plot.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
159
diff
changeset
|
102 |
introducing linestyle. Hence the naive approach. |
129 | 103 |
|
159 | 104 |
.. #[[Anoop: I guess the recipe is fine, but would be better if you |
105 |
add the problem statement rather than just saying "let's do a simple |
|
106 |
plot"]] |
|
107 |
||
172
438e7bae3cf3
Second Review for embellishing plot.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
159
diff
changeset
|
108 |
.. #[Madhu: It is good enough.] |
438e7bae3cf3
Second Review for embellishing plot.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
159
diff
changeset
|
109 |
|
438e7bae3cf3
Second Review for embellishing plot.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
159
diff
changeset
|
110 |
Occasionally we would also want to alter the style of line. Sometimes |
438e7bae3cf3
Second Review for embellishing plot.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
159
diff
changeset
|
111 |
all we want is just a bunch of points not joined. This is possible by |
438e7bae3cf3
Second Review for embellishing plot.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
159
diff
changeset
|
112 |
passing the linestyle argument along with or instead of the colour |
438e7bae3cf3
Second Review for embellishing plot.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
159
diff
changeset
|
113 |
argument. Hence :: |
129 | 114 |
|
115 |
clf() |
|
116 |
plot(x, sin(x), '.') |
|
117 |
||
118 |
produces a plot with only points. |
|
119 |
||
138
85ed0d5d28f8
added a newline before :: so that a colon does not appear in html
nishanth
parents:
130
diff
changeset
|
120 |
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
|
121 |
:: |
129 | 122 |
|
123 |
clf() |
|
124 |
plot(x, sin(x), 'b.') |
|
125 |
||
138
85ed0d5d28f8
added a newline before :: so that a colon does not appear in html
nishanth
parents:
130
diff
changeset
|
126 |
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
|
127 |
:: |
129 | 128 |
|
129 |
plot? |
|
130 |
||
131 |
{{{ Run through the documentation and show the options available }}} |
|
132 |
||
173
24289d3a4fdb
added the changes suggested by anoop and madhu
Nishanth <nishanth@fossee.in>
parents:
172
diff
changeset
|
133 |
{{{ Show the options available for line style and colors }}} |
24289d3a4fdb
added the changes suggested by anoop and madhu
Nishanth <nishanth@fossee.in>
parents:
172
diff
changeset
|
134 |
|
172
438e7bae3cf3
Second Review for embellishing plot.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
159
diff
changeset
|
135 |
.. #[Madhu: The script needs to tell what needs to be shown or |
438e7bae3cf3
Second Review for embellishing plot.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
159
diff
changeset
|
136 |
explained.] |
438e7bae3cf3
Second Review for embellishing plot.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
159
diff
changeset
|
137 |
|
129 | 138 |
{{{ Pause here and try out the following exercises }}} |
139 |
||
159 | 140 |
.. #[[Anoop: same question as above, should it be read out?]] |
141 |
||
129 | 142 |
%% 2 %% Plot the sine curve with green filled circles. |
143 |
||
144 |
{{{ continue from paused state }}} |
|
145 |
||
146 |
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
|
147 |
Hence |
85ed0d5d28f8
added a newline before :: so that a colon does not appear in html
nishanth
parents:
130
diff
changeset
|
148 |
:: |
129 | 149 |
|
150 |
clf() |
|
151 |
plot(x, cos(x), 'go') |
|
152 |
||
153 |
produces the required plot. |
|
154 |
||
155 |
{{{ Pause here and try out the following exercises }}} |
|
156 |
||
173
24289d3a4fdb
added the changes suggested by anoop and madhu
Nishanth <nishanth@fossee.in>
parents:
172
diff
changeset
|
157 |
%% 3 %% Plot the curve of x vs tan(x) in red dashed line and linewidth 3 |
129 | 158 |
|
159 |
{{{ continue from paused state }}} |
|
160 |
||
172
438e7bae3cf3
Second Review for embellishing plot.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
159
diff
changeset
|
161 |
.. #[Madhu: I did not understand the question] |
438e7bae3cf3
Second Review for embellishing plot.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
159
diff
changeset
|
162 |
|
438e7bae3cf3
Second Review for embellishing plot.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
159
diff
changeset
|
163 |
Now that we know how to produce a bare minimum plot with colour, style |
438e7bae3cf3
Second Review for embellishing plot.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
159
diff
changeset
|
164 |
and thickness of our interest, we shall look at decorating the plot. |
129 | 165 |
|
138
85ed0d5d28f8
added a newline before :: so that a colon does not appear in html
nishanth
parents:
130
diff
changeset
|
166 |
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
|
167 |
:: |
129 | 168 |
|
169 |
plot(x, -x*x + 4*x - 5, 'r', linewidth=2) |
|
170 |
||
171 |
{{{ Show the plot window and switch back to terminal }}} |
|
172 |
||
173 |
We now have the plot in a colour and linewidth of our interest. As you can see, |
|
172
438e7bae3cf3
Second Review for embellishing plot.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
159
diff
changeset
|
174 |
the figure does not have any description describing the plot. |
438e7bae3cf3
Second Review for embellishing plot.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
159
diff
changeset
|
175 |
|
438e7bae3cf3
Second Review for embellishing plot.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
159
diff
changeset
|
176 |
.. #[Madhu: Added "not". See the diff] |
129 | 177 |
|
193
0d175627e828
changed = to `` so that it shows properly
Nishanth <nishanth@fossee.in>
parents:
191
diff
changeset
|
178 |
We will now add a title to the plot by using the ``title`` command. |
138
85ed0d5d28f8
added a newline before :: so that a colon does not appear in html
nishanth
parents:
130
diff
changeset
|
179 |
:: |
129 | 180 |
|
181 |
title("Parabolic function -x^2+4x-5") |
|
182 |
||
183 |
{{{ Show the plot window and point to the title }}} |
|
172
438e7bae3cf3
Second Review for embellishing plot.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
159
diff
changeset
|
184 |
|
438e7bae3cf3
Second Review for embellishing plot.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
159
diff
changeset
|
185 |
The figure now has a title which describes what the plot is. The |
193
0d175627e828
changed = to `` so that it shows properly
Nishanth <nishanth@fossee.in>
parents:
191
diff
changeset
|
186 |
``title`` command as you can see, takes a string as an argument and sets |
172
438e7bae3cf3
Second Review for embellishing plot.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
159
diff
changeset
|
187 |
the title accordingly. |
438e7bae3cf3
Second Review for embellishing plot.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
159
diff
changeset
|
188 |
|
438e7bae3cf3
Second Review for embellishing plot.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
159
diff
changeset
|
189 |
.. #[Madhu: See the diff] |
129 | 190 |
|
191 |
The formatting in title is messed and it does not look clean. You can imagine |
|
192 |
what would be the situation if there were fractions and more complex functions |
|
172
438e7bae3cf3
Second Review for embellishing plot.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
159
diff
changeset
|
193 |
like log and exp. Wouldn't it be good if there was LaTex like formatting? |
129 | 194 |
|
195 |
That is also possible by adding a $ sign before and after the part of the |
|
172
438e7bae3cf3
Second Review for embellishing plot.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
159
diff
changeset
|
196 |
string that should be in LaTex style. |
129 | 197 |
|
138
85ed0d5d28f8
added a newline before :: so that a colon does not appear in html
nishanth
parents:
130
diff
changeset
|
198 |
for instance, we can use |
85ed0d5d28f8
added a newline before :: so that a colon does not appear in html
nishanth
parents:
130
diff
changeset
|
199 |
:: |
129 | 200 |
|
201 |
title("Parabolic function $-x^2+4x-5$") |
|
202 |
||
203 |
and we get the polynomial formatted properly. |
|
204 |
||
190
6dcc77582e26
made the comments in my script as rst comments
Nishanth <nishanth@fossee.in>
parents:
173
diff
changeset
|
205 |
.. #[Nishanth]: Unsure if I have to give this exercise since enclosing the whole |
130 | 206 |
string in LaTex style is not good |
207 |
||
172
438e7bae3cf3
Second Review for embellishing plot.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
159
diff
changeset
|
208 |
.. #[[Anoop: I guess you can go ahead with the LaTex thing, it's |
438e7bae3cf3
Second Review for embellishing plot.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
159
diff
changeset
|
209 |
cool!]] |
438e7bae3cf3
Second Review for embellishing plot.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
159
diff
changeset
|
210 |
.. #[Madhu: Instead of saying LaTeX style you can say Typeset math |
438e7bae3cf3
Second Review for embellishing plot.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
159
diff
changeset
|
211 |
since that is how it is called as. I am not sure as well. It |
438e7bae3cf3
Second Review for embellishing plot.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
159
diff
changeset
|
212 |
doesn't really solve the purpose] |
159 | 213 |
|
129 | 214 |
{{{ Pause here and try out the following exercises }}} |
215 |
||
216 |
%% 4 %% Change the title of the figure such that the whole title is formatted |
|
217 |
in LaTex style |
|
218 |
||
219 |
{{{ continue from the paused state }}} |
|
220 |
||
138
85ed0d5d28f8
added a newline before :: so that a colon does not appear in html
nishanth
parents:
130
diff
changeset
|
221 |
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
|
222 |
:: |
129 | 223 |
|
224 |
title("$Parabolic function -x^2+4x-5$") |
|
225 |
||
226 |
gives a title that looks neatly formatted. |
|
227 |
||
172
438e7bae3cf3
Second Review for embellishing plot.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
159
diff
changeset
|
228 |
Although we have title, the plot is not complete without labelling x |
438e7bae3cf3
Second Review for embellishing plot.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
159
diff
changeset
|
229 |
and y axes. Hence we shall label x-axis to "x" and y-axis to "f(x)" :: |
129 | 230 |
|
231 |
xlabel("x") |
|
232 |
||
233 |
{{{ Switch to plot window and show the xlabel }}} |
|
234 |
||
193
0d175627e828
changed = to `` so that it shows properly
Nishanth <nishanth@fossee.in>
parents:
191
diff
changeset
|
235 |
As you can see, ``xlabel`` command takes a string as an argument, |
0d175627e828
changed = to `` so that it shows properly
Nishanth <nishanth@fossee.in>
parents:
191
diff
changeset
|
236 |
similar to the ``title`` command and sets it as the label to x-axis. |
172
438e7bae3cf3
Second Review for embellishing plot.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
159
diff
changeset
|
237 |
|
438e7bae3cf3
Second Review for embellishing plot.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
159
diff
changeset
|
238 |
.. #[See the diff] |
129 | 239 |
|
138
85ed0d5d28f8
added a newline before :: so that a colon does not appear in html
nishanth
parents:
130
diff
changeset
|
240 |
Similarly, |
85ed0d5d28f8
added a newline before :: so that a colon does not appear in html
nishanth
parents:
130
diff
changeset
|
241 |
:: |
129 | 242 |
|
243 |
ylabel("f(x)") |
|
244 |
||
172
438e7bae3cf3
Second Review for embellishing plot.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
159
diff
changeset
|
245 |
sets the name of the y-axis as "f(x)" |
129 | 246 |
|
172
438e7bae3cf3
Second Review for embellishing plot.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
159
diff
changeset
|
247 |
{{{ Show the plot window and point to ylabel and switch back to the terminal }}} |
129 | 248 |
|
249 |
{{{ Pause here and try out the following exercises }}} |
|
250 |
||
251 |
%% 5 %% Set the x and y labels as "x" and "f(x)" in LaTex style. |
|
252 |
||
253 |
{{{ continue from paused state }}} |
|
254 |
||
255 |
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
|
256 |
in between two $. Hence, |
85ed0d5d28f8
added a newline before :: so that a colon does not appear in html
nishanth
parents:
130
diff
changeset
|
257 |
:: |
129 | 258 |
|
259 |
xlabel("$x$") |
|
260 |
yalbel("$f(x)$") |
|
261 |
||
262 |
does the job for us. |
|
263 |
||
264 |
{{{ Show the plot window with clean labels }}} |
|
265 |
||
266 |
The plot is now almost complete. Except that we have still not seen how to |
|
267 |
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
|
268 |
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
|
269 |
:: |
129 | 270 |
|
271 |
annotate("local maxima", xy=(2, -1)) |
|
272 |
||
273 |
{{{ Show the annotation that has appeared on the plot }}} |
|
172
438e7bae3cf3
Second Review for embellishing plot.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
159
diff
changeset
|
274 |
|
193
0d175627e828
changed = to `` so that it shows properly
Nishanth <nishanth@fossee.in>
parents:
191
diff
changeset
|
275 |
As you can see, the first argument to ``annotate`` command is the name we would |
173
24289d3a4fdb
added the changes suggested by anoop and madhu
Nishanth <nishanth@fossee.in>
parents:
172
diff
changeset
|
276 |
like to mark the point as and the second argument is the co-ordinates of the |
24289d3a4fdb
added the changes suggested by anoop and madhu
Nishanth <nishanth@fossee.in>
parents:
172
diff
changeset
|
277 |
point at which the name should appear. It is a sequence containing two numbers. |
24289d3a4fdb
added the changes suggested by anoop and madhu
Nishanth <nishanth@fossee.in>
parents:
172
diff
changeset
|
278 |
The first is x co-ordinate and second is y co-ordinate. |
129 | 279 |
|
159 | 280 |
.. #[[Anoop: I think we should tell explicitely that xy takes a |
281 |
sequence or a tuple]] |
|
172
438e7bae3cf3
Second Review for embellishing plot.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
159
diff
changeset
|
282 |
.. #[Madhu: Agreed to what anoop says and also that xy= is the point |
438e7bae3cf3
Second Review for embellishing plot.
Madhusudan.C.S <madhusudancs@gmail.com>
parents:
159
diff
changeset
|
283 |
part should be rephrased I think.] |
159 | 284 |
|
129 | 285 |
{{{ Pause here and try out the following exercises }}} |
286 |
||
287 |
%% 6 %% Make an annotation called "root" at the point (-4, 0) |
|
288 |
What happens to the first annotation ? |
|
289 |
||
290 |
{{{ continue from paused state }}} |
|
291 |
||
292 |
As we can see, every annotate command makes a new annotation on the figure. |
|
293 |
||
214 | 294 |
Now we have everything we need to decorate a plot. but the plot would be |
295 |
incomplete if we can not set the limits of axes. This is possible using the |
|
296 |
button on the plot window. |
|
297 |
||
298 |
we shall look at how to get and set them from the script. |
|
299 |
:: |
|
300 |
||
301 |
xlim() |
|
302 |
ylim() |
|
303 |
||
304 |
We see that ``xlim`` function returns the current x axis limits and ylim |
|
305 |
function returns the current y-axis limits. |
|
306 |
||
307 |
Let us look at how to set the limits. |
|
308 |
:: |
|
309 |
||
310 |
xlim(-4, 5) |
|
311 |
||
312 |
We see the limits of x-axis are now set to -4 and 5. |
|
313 |
Similarly |
|
314 |
:: |
|
315 |
||
316 |
ylim(-15, 2) |
|
317 |
||
318 |
sets the limits of y-axis appropriately. |
|
319 |
||
320 |
{{{ Pause here and try out the following exercises }}} |
|
321 |
||
322 |
%% 7 %% Set the limits of axes such that the area of interest is the rectangle |
|
323 |
(-1, -15) and (3, 0) |
|
324 |
||
325 |
{{{ continue from paused state }}} |
|
326 |
||
327 |
As we can see, the lower upper limits of x-axis in the question are -1 and 3. |
|
328 |
The limits of y-axis are -15 and 0. |
|
329 |
||
330 |
:: |
|
331 |
||
332 |
xlim(-1, 3) |
|
333 |
ylim(-15, 0) |
|
334 |
||
335 |
Gives us the required rectangle. |
|
336 |
||
129 | 337 |
{{{ Show summary slide }}} |
338 |
||
339 |
we have looked at |
|
340 |
||
341 |
* Modifying the attributes of plot by passing additional arguments |
|
342 |
* How to add title |
|
343 |
* How to incorporate LaTex style formatting |
|
344 |
* How to label x and y axes |
|
345 |
* How to add annotations |
|
214 | 346 |
* How to set the limits of axes |
129 | 347 |
|
348 |
{{{ Show the "sponsored by FOSSEE" slide }}} |
|
349 |
||
190
6dcc77582e26
made the comments in my script as rst comments
Nishanth <nishanth@fossee.in>
parents:
173
diff
changeset
|
350 |
.. #[Nishanth]: Will add this line after all of us fix on one. |
191 | 351 |
|
129 | 352 |
This tutorial was created as a part of FOSSEE project, NME ICT, MHRD India |
353 |
||
354 |
Hope you have enjoyed and found it useful. |
|
355 |
Thankyou |
|
214 | 356 |
|
357 |
Questions |
|
358 |
========= |
|
359 |
||
360 |
1. Draw a plot of cosine graph between -2pi to 2pi with line thickness 4 |
|
361 |
||
362 |
Answer:: |
|
363 |
||
364 |
x = linspace(-2*pi, 2*pi) |
|
365 |
plot(x, cos(x), linewidth=4) |
|
366 |
||
367 |
2. Draw a plot of the polynomial x^2-5x+6 in the range 0 to 5 in blue dotted |
|
368 |
line |
|
369 |
||
370 |
Answer:: |
|
371 |
||
372 |
x = linspace(-2*pi, 2*pi) |
|
373 |
plot(x, x**2 - 5*x + 6, 'r.') |
|
374 |
||
375 |
3. Which marker is used to get circles |
|
376 |
||
377 |
a. '.' |
|
378 |
#. '^' |
|
379 |
#. 'o' |
|
380 |
#. '--' |
|
381 |
||
382 |
4. What does the '^' marker produce |
|
383 |
||
384 |
Answer: Triangle up marker |
|
385 |
||
386 |
5. How do you set the title as x^2-5x+6 in LaTex style formatting |
|
387 |
||
388 |
Answer: title("$x^2-5x+6$") |
|
389 |
||
390 |
6. What happens when the following code is executed:: |
|
391 |
||
392 |
xlabel("First label") |
|
393 |
xlabel("Second label") |
|
394 |
||
395 |
Answer: The label of x-axis is set to "Second label" |
|
396 |
||
397 |
7. Read thorugh the documentation and find out is there a way to modify the |
|
398 |
alignment of text in the command ``ylabel`` |
|
399 |
||
400 |
a. Yes |
|
401 |
#. No |
|
402 |
||
403 |
Answer: No |
|
404 |
||
405 |
8. How to add the annotation "Maxima" at the point (1, 2) |
|
406 |
||
407 |
Answer: annotate("Maxima", xy=(1, 2)) |
|
408 |
||
409 |
9. Is the command ``annotate("max", (1, 2))`` same as ``annotate("max", |
|
410 |
xy=(1, 2)`` |
|
411 |
||
412 |
a. True |
|
413 |
b. False |
|
414 |
||
415 |
Answer: True |
|
416 |
||
417 |
10. When a new annotation is made at a point, what happens to the old one |
|
418 |
||
419 |
a. It is replaced |
|
420 |
b. It is overwritten |
|
421 |
c. The new annotation is combined with old one |
|
422 |
||
423 |
Answer: It is overwritten |
|
424 |
||
425 |
11. What happens when xlim is used without arguments |
|
426 |
||
427 |
Answer: It gives the current limits of x-axis |
|
428 |
||
429 |
12. What happens when ``ylim(0, 5)`` is used |
|
430 |
||
431 |
Answer: It sets the lower and upper limits of y-axis to 0 and 5 |
|
432 |
||
433 |
13. Draw a cosine plot from 0 to 2*pi with green dots. annotate the origin as |
|
434 |
"origin" and set x and y labels to "x" and cos(x) and x limits to 0 and |
|
435 |
2pi and y limits to -1.2 and 1.2 |
|
436 |
||
437 |
Answer:: |
|
438 |
||
439 |
x = linspace(0, 2*pi) |
|
440 |
plot(x, cos(x), 'g.') |
|
441 |
annotate("origin", (0, 0)) |
|
442 |
xlabel("$x$") |
|
443 |
ylabel("$cos(x)$") |
|
444 |
xlim(0, 2*pi) |
|
445 |
ylim(-1.2, 1.2) |
|
446 |