1 .. Objectives |
|
2 .. ---------- |
|
3 |
|
4 .. At the end of this tutorial, you will be able to |
|
5 |
|
6 .. 1. Create scatter plot |
|
7 .. #. Create pie charts |
|
8 .. #. Create bar charts |
|
9 .. #. Create log-log plots. |
|
10 |
|
11 .. Prerequisites |
|
12 .. ------------- |
|
13 |
|
14 .. 1. should have ``ipython`` and ``pylab`` installed. |
|
15 .. #. getting started with ``ipython``. |
|
16 .. #. loading data from files |
|
17 .. #. plotting the data |
|
18 |
|
19 |
|
20 .. Author : Anoop Jacob Thomas <anoop@fossee.in> |
|
21 Internal Reviewer : Puneeth |
|
22 External Reviewer : |
|
23 Language Reviewer : Bhanukiran |
|
24 Checklist OK? : <10-11-2010, Anand, OK> [2010-10-05] |
|
25 |
|
26 .. #[Puneeth: Quickref missing] |
|
27 |
|
28 =================== |
|
29 Other type of plots |
|
30 =================== |
|
31 |
|
32 {{{ show the first slide }}} |
|
33 |
|
34 Hello and welcome to the tutorial ``The other kinds of plots``. |
|
35 |
|
36 .. #[Puneeth: this sentence doesn't read well] |
|
37 |
|
38 {{{ show the outline slide }}} |
|
39 |
|
40 .. #[Puneeth: motivate looking at other plots. Why are we looking at |
|
41 .. them? Tell that we have only looked at one type of plot all the |
|
42 .. while, etc.] |
|
43 |
|
44 Till now we have seen only one kind of plotting, and in this tutorial we |
|
45 are going to see more kinds of plots such as the scatter plot, the pie chart, the bar chart and |
|
46 the log-log plot. This tutorial covers the making of other kinds of |
|
47 plots and also gives an introduction to matplotlib help. |
|
48 |
|
49 .. #[Puneeth: cover, see and introduce you. be consistent. does, the |
|
50 .. "We" include the viewer or not?] |
|
51 |
|
52 Let us start with scatter plot. |
|
53 |
|
54 {{{ switch to the next slide, scatter plot }}} |
|
55 |
|
56 In a scatter plot, the data is displayed as a collection of points, |
|
57 each having the value of one variable determining the position on the |
|
58 horizontal axis and the value of the other variable determining the |
|
59 position on the vertical axis. This kind of plot is also called a |
|
60 scatter chart, a scatter diagram or a scatter graph. |
|
61 |
|
62 Before we proceed further, start your IPython interpreter |
|
63 :: |
|
64 |
|
65 ipython -pylab |
|
66 |
|
67 {{{ open the ipython interpreter in the terminal using the command |
|
68 ipython -pylab }}} |
|
69 |
|
70 {{{ switch to the next slide having the problem statement of first |
|
71 exercise }}} |
|
72 |
|
73 Now, let us plot a scatter plot showing the percentage profit of |
|
74 a company A from the year 2000-2010. The data for the same is available |
|
75 in the file ``company-a-data.txt``. |
|
76 |
|
77 {{{ open the file company-a-data.txt and show the content }}} |
|
78 |
|
79 The data file has two lines with a set of values in each line, the |
|
80 first line representing years and the second line representing the |
|
81 profit percentages. |
|
82 |
|
83 {{{ close the file and switch to the terminal }}} |
|
84 |
|
85 To produce the scatter plot, we first need to load the data from the |
|
86 file using ``loadtxt``. We learned it in one of the previous sessions, |
|
87 and it can be done as :: |
|
88 |
|
89 year,profit = |
|
90 loadtxt('/home/fossee/other-plot/company-a-data.txt',dtype=type(int())) |
|
91 |
|
92 By default loadtxt converts the value to float. The |
|
93 ``dtype=type(int())`` argument in loadtxt converts the value to |
|
94 integer as we require the data as integer further in the tutorial. |
|
95 |
|
96 .. #[Puneeth: make a remark about dtype, that has not been covered in |
|
97 .. the loadtxt tutorial.] |
|
98 |
|
99 {{{ switch to next slide, ``scatter`` function }}} |
|
100 |
|
101 Now in-order to generate the scatter graph we will use the function |
|
102 ``scatter()`` |
|
103 :: |
|
104 |
|
105 scatter(year,profit) |
|
106 |
|
107 Notice that we passed two arguments to ``scatter()`` function, first |
|
108 one the values in x-coordinate, year, and the other the values in |
|
109 y-coordinate, the profit percentage. |
|
110 |
|
111 {{{ switch to the next slide which has the problem statement of |
|
112 problem to be tried out }}} |
|
113 |
|
114 Now here is a question for you to try out, plot the same data with red |
|
115 diamonds markers. |
|
116 |
|
117 .. **Clue** - *try scatter? in your ipython interpreter* |
|
118 |
|
119 Pause here and solve the question before moving on. |
|
120 |
|
121 .. scatter(year,profit,color='r',marker='d') |
|
122 |
|
123 Now let us see another kind of plot, the pie chart, for the same data. |
|
124 |
|
125 .. #[Puneeth: instead of just saying that, say that let's plot a pie |
|
126 .. chart for the same data. continuity, will be good.] |
|
127 |
|
128 {{{ switch to the slide which says about pie chart }}} |
|
129 |
|
130 A pie chart or a circle graph is a circular chart divided into |
|
131 sectors, illustrating proportion. |
|
132 |
|
133 {{{ switch to the slide showing the problem statement of second |
|
134 exercise question }}} |
|
135 |
|
136 Plot a pie chart representing the profit percentage of company A, with |
|
137 the same data from file ``company-a-data.txt``. So let us reuse the |
|
138 data we have loaded from the file previously. |
|
139 |
|
140 .. #[Puneeth, this part can be move above.] |
|
141 |
|
142 {{{ switch to next slide, ``pie()`` function }}} |
|
143 |
|
144 We can plot the pie chart using the function ``pie()``. |
|
145 :: |
|
146 |
|
147 pie(profit,labels=year) |
|
148 |
|
149 Notice that we passed two arguments to the function ``pie()``. First |
|
150 one the values and the next one the set of labels to be used in the |
|
151 pie chart. |
|
152 |
|
153 {{{ switch to the next slide which has the problem statement of |
|
154 problem to be tried out }}} |
|
155 |
|
156 Now here is a question for you to try out, plot a pie chart with the |
|
157 same data with colors for each wedges as white, red, black, magenta, |
|
158 yellow, blue, green, cyan, yellow, magenta and blue respectively. |
|
159 |
|
160 .. **Clue** - *try pie? in your ipython interpreter* |
|
161 |
|
162 Pause here and solve the question before moving on. |
|
163 |
|
164 .. pie(t,labels=s,colors=('w','r','k','m','y','b','g','c','y','m','b')) |
|
165 |
|
166 {{{ switch to the slide which says about bar chart }}} |
|
167 |
|
168 Now let us move on to the bar charts. A bar chart or bar graph is a chart |
|
169 with rectangular bars with lengths proportional to the values that |
|
170 they represent. |
|
171 |
|
172 {{{ switch to the slide showing the problem statement of fifth |
|
173 exercise question }}} |
|
174 |
|
175 Plot a bar chart representing the profit percentage of company A, with |
|
176 the same data from file ``company-a-data.txt``. |
|
177 |
|
178 So let us reuse the data we have loaded from the file previously. |
|
179 |
|
180 {{{ switch to the next slide, ``bar()`` function }}} |
|
181 |
|
182 We can plot the bar chart using the function ``bar()``. |
|
183 :: |
|
184 |
|
185 bar(year,profit) |
|
186 |
|
187 Note that the function ``bar()`` needs at least two arguments one the |
|
188 values in x-coordinate and the other values in y-coordinate which is |
|
189 used to determine the height of the bars. |
|
190 |
|
191 {{{ switch to the next slide which has the problem statement of |
|
192 problem to be tried out }}} |
|
193 |
|
194 Now here is a question for you to try, plot a bar chart which is not |
|
195 filled and which is hatched with 45\ :sup:`o` slanting lines as shown |
|
196 in the image in the slide. The data for the chart may be obtained from |
|
197 the file ``company-a-data.txt``. |
|
198 |
|
199 .. **Clue** - *try bar? in your ipython interpreter* |
|
200 |
|
201 .. bar(year,profit,fill=False,hatch='/') |
|
202 |
|
203 {{{ switch to the slide which says about log-log graph }}} |
|
204 |
|
205 Now let us move on to the log-log plot. A log-log graph or a log-log plot is |
|
206 a two-dimensional graph of numerical data that uses logarithmic scales |
|
207 on both the horizontal and vertical axes. Because of the nonlinear |
|
208 scaling of the axes, a function of the form y = ax\ :sup:`b` will |
|
209 appear as a straight line on a log-log graph |
|
210 |
|
211 {{{ switch to the slide showing the problem statement of fourth |
|
212 exercise question }}} |
|
213 |
|
214 |
|
215 Plot a `log-log` chart of y=5*x\ :sup:`3` for x from 1-20. |
|
216 |
|
217 Before we actually plot let us calculate the points needed for |
|
218 that. |
|
219 :: |
|
220 |
|
221 x = linspace(1,20,100) |
|
222 y = 5*x**3 |
|
223 |
|
224 {{{ switch to next slide, ``loglog()`` function }}} |
|
225 |
|
226 Now we can plot the log-log chart using ``loglog()`` function, |
|
227 :: |
|
228 |
|
229 loglog(x,y) |
|
230 |
|
231 To understand the difference between a normal ``plot`` and a ``log-log |
|
232 plot`` let us create another plot using the function ``plot``. |
|
233 :: |
|
234 |
|
235 figure(2) |
|
236 plot(x,y) |
|
237 |
|
238 {{{ show both the plots side by side }}} |
|
239 |
|
240 So that was ``log-log() plot``. |
|
241 |
|
242 {{{ switch to the next slide which says: "How to get help on |
|
243 matplotlib online"}}} |
|
244 |
|
245 Now we will see few more plots and also see how to access help of |
|
246 matplotlib over the internet. |
|
247 |
|
248 Help about matplotlib can be obtained from |
|
249 matplotlib.sourceforge.net/contents.html |
|
250 |
|
251 |
|
252 More plots can be seen at |
|
253 matplotlib.sourceforge.net/users/screenshots.html and also at |
|
254 matplotlib.sourceforge.net/gallery.html |
|
255 |
|
256 {{{ switch to summary slide }}} |
|
257 |
|
258 Now we have come to the end of this tutorial. We have covered scatter |
|
259 plot, pie chart, bar chart, log-log plot and also saw few other plots |
|
260 and covered how to access the matplotlib online help. |
|
261 |
|
262 {{{ switch to the thank you slide }}} |
|
263 |
|
264 Thank you! |
|