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 |
|
|
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 |
|
130
|
135 |
#[Nishanth]: Unsure if I have to give this exercise since enclosing the whole
|
|
136 |
string in LaTex style is not good
|
|
137 |
|
129
|
138 |
{{{ Pause here and try out the following exercises }}}
|
|
139 |
|
|
140 |
%% 4 %% Change the title of the figure such that the whole title is formatted
|
|
141 |
in LaTex style
|
|
142 |
|
|
143 |
{{{ continue from the paused state }}}
|
|
144 |
|
|
145 |
The solution is to enclose the whole string in between $. Hence,::
|
|
146 |
|
|
147 |
title("$Parabolic function -x^2+4x-5$")
|
|
148 |
|
|
149 |
gives a title that looks neatly formatted.
|
|
150 |
|
|
151 |
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)"::
|
|
153 |
|
|
154 |
xlabel("x")
|
|
155 |
|
|
156 |
{{{ Switch to plot window and show the xlabel }}}
|
|
157 |
|
|
158 |
As you can see, =xlabel= command takes a string as argument, similar to the
|
|
159 |
=title= command and sets it to x-axis.
|
|
160 |
|
|
161 |
Similarly,::
|
|
162 |
|
|
163 |
ylabel("f(x)")
|
|
164 |
|
|
165 |
sets the name of y-axis as "f(x)"
|
|
166 |
|
|
167 |
{{{ Show the plot window and point to ylabel and switch back to terminal }}}
|
|
168 |
|
|
169 |
{{{ Pause here and try out the following exercises }}}
|
|
170 |
|
|
171 |
%% 5 %% Set the x and y labels as "x" and "f(x)" in LaTex style.
|
|
172 |
|
|
173 |
{{{ continue from paused state }}}
|
|
174 |
|
|
175 |
Since we need LaTex style formatting, all we have to do is enclose the string
|
|
176 |
in between two $. Hence,::
|
|
177 |
|
|
178 |
xlabel("$x$")
|
|
179 |
yalbel("$f(x)$")
|
|
180 |
|
|
181 |
does the job for us.
|
|
182 |
|
|
183 |
{{{ Show the plot window with clean labels }}}
|
|
184 |
|
|
185 |
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
|
|
187 |
like to name the point accordingly. We can do this by using::
|
|
188 |
|
|
189 |
annotate("local maxima", xy=(2, -1))
|
|
190 |
|
|
191 |
{{{ 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
|
|
193 |
like to mark the point as and the argument after xy= is the point at which the
|
|
194 |
name should appear.
|
|
195 |
|
|
196 |
{{{ Pause here and try out the following exercises }}}
|
|
197 |
|
|
198 |
%% 6 %% Make an annotation called "root" at the point (-4, 0)
|
|
199 |
What happens to the first annotation ?
|
|
200 |
|
|
201 |
{{{ continue from paused state }}}
|
|
202 |
|
|
203 |
As we can see, every annotate command makes a new annotation on the figure.
|
|
204 |
|
|
205 |
{{{ Show summary slide }}}
|
|
206 |
|
|
207 |
we have looked at
|
|
208 |
|
|
209 |
* Modifying the attributes of plot by passing additional arguments
|
|
210 |
* How to add title
|
|
211 |
* How to incorporate LaTex style formatting
|
|
212 |
* How to label x and y axes
|
|
213 |
* How to add annotations
|
|
214 |
|
|
215 |
{{{ Show the "sponsored by FOSSEE" slide }}}
|
|
216 |
|
|
217 |
#[Nishanth]: Will add this line after all of us fix on one.
|
|
218 |
This tutorial was created as a part of FOSSEE project, NME ICT, MHRD India
|
|
219 |
|
|
220 |
Hope you have enjoyed and found it useful.
|
|
221 |
Thankyou
|
|
222 |
|
|
223 |
.. Author : Nishanth
|
|
224 |
Internal Reviewer 1 :
|
|
225 |
Internal Reviewer 2 :
|
|
226 |
External Reviewer :
|