-
Notifications
You must be signed in to change notification settings - Fork 0
/
lec16.tex
executable file
·281 lines (230 loc) · 6.33 KB
/
lec16.tex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
\documentclass{beamer}
\usepackage{listings}
\usepackage{color}
\usepackage{hyperref}
\usepackage[T1]{fontenc}
\usepackage{textcomp}
\usepackage{upquote}
% Default fixed font does not support bold face
\DeclareFixedFont{\ttb}{T1}{txtt}{bx}{n}{10} % for bold
\DeclareFixedFont{\ttm}{T1}{txtt}{m}{n}{10} % for normal
% Custom colors
\usepackage{color}
\definecolor{deepblue}{rgb}{0,0,0.5}
\definecolor{deepred}{rgb}{0.6,0,0}
\definecolor{deepgreen}{rgb}{0,0.5,0}
\definecolor{shadecolor}{rgb}{1, 0.9, 0.3}
\usepackage{listings}
% Python style for highlighting
\newcommand\pythonstyle{\lstset{
language=Python,
basicstyle=\ttm,
otherkeywords={self, as}, % Add keywords here
keywordstyle=\ttb\color{deepblue},
emph={MyClass,__init__}, % Custom highlighting
emphstyle=\ttb\color{deepred}, % Custom highlighting style
stringstyle=\color{deepgreen},
frame=tb, % Any extra options here
showstringspaces=false, %
upquote=True,
columns=fullflexible,
basicstyle=\ttfamily
}}
% Python environment
\lstnewenvironment{code}[1][]
{
%\begin{small}
\pythonstyle
\lstset{#1}
%\end{small}
}
{}
\begin{document}
% Hands on with plotting graphs using data from files.
\begin{frame}
\frametitle{CS24420 \& MA25220 \& MT25220 \& MX35220 \& CSM0120}
\begin{center}
\begin{huge}
Lecture 16: Plotting data
\end{huge}
\bigskip
Amanda Clare ([email protected])
\end{center}
\end{frame}
\begin{frame}[fragile]
\frametitle{The next two weeks}
\begin{itemize}
\item \textbf{Tues 6th:} Plotting, and prac with worksheet 8. Sign off worksheet 6 deadline
\item \textbf{Fri 9th:} What would you like? Mathematical algorithms
hack? 50-min data
analysis challenge? Fractal plotting? Extra material on a previous topic? Python show and tell?
\item \textbf{Tues 13th:} Revision class. Prac, and sign off worksheet 7 deadline.
\item \textbf{Fri 16th:} Optional extra practical. Sign off worksheet 8 deadline.
\end{itemize}
January: Extra material on Databases and Python (using Sqlite).
\end{frame}
\begin{frame}[fragile]
\frametitle{matplotlib}
\begin{itemize}
\item matplotlib provides MATLAB-like 2D plotting.
\item A free and open source library for Python. \url{http://matplotlib.org}
\item Produces a range of image formats: png, pdf, ps, eps, svg etc
\begin{figure}
\centering
\begin{minipage}{.5\textwidth}
\centering
\includegraphics[width=5cm]{histogram_demo_multihist.png}
\end{minipage}%
\begin{minipage}{.5\textwidth}
\centering
\includegraphics[width=5cm]{finance_work2.png}
\end{minipage}
\end{figure}
\end{itemize}
\end{frame}
\begin{frame}[fragile]
\frametitle{The most basic plot}
\begin{code}
import matplotlib.pyplot as plt
plt.plot([1, 3, 2, 4])
plt.ylabel("the y axis name")
plt.show()
\end{code}
\end{frame}
\begin{frame}[fragile]
\frametitle{Adding x values for the y values}
\begin{code}
import matplotlib.pyplot as plt
plt.plot([10, 20, 30, 40], [1, 3, 2, 4])
plt.ylabel("the y axis name")
plt.xlabel("some x axis")
plt.show()
\end{code}
\end{frame}
\begin{frame}[fragile]
\frametitle{Plotting triangles etc}
\begin{code}
import matplotlib.pyplot as plt
plt.plot([10, 20, 30, 40], [1, 3, 2, 4], "bv")
plt.ylabel("the y axis name")
plt.xlabel("some x axis")
plt.show()
\end{code}
\bigskip
Try \texttt{"ro" "g." "c-" "k:"}
\end{frame}
\begin{frame}[fragile]
\frametitle{Plotting points and lines}
Create variables x and y to hold the data. Plot with points and plot
again with lines on the same graph.
\begin{code}
import matplotlib.pyplot as plt
x = [10, 20, 30, 40]
y = [1, 3, 2, 4]
plt.plot(x, y, "bv")
plt.plot(x, y, "b-")
plt.ylabel("the y axis name")
plt.xlabel("some x axis")
plt.show()
\end{code}
\end{frame}
\begin{frame}[fragile]
\frametitle{Plotting multiple data series on one graph}
\begin{code}
import matplotlib.pyplot as plt
x = [10, 20, 30, 40]
y1 = [1, 3, 2, 4]
y2 = [1.5, 2.5, 3.5, 4.5]
plt.plot(x, y1, "b-")
plt.plot(x, y2, "r-")
plt.axis([0, 50, 0, 5])
plt.ylabel("the y axis name")
plt.xlabel("some x axis")
plt.show()
\end{code}
\end{frame}
\begin{frame}[fragile]
\frametitle{Saving the image}
\begin{code}
import matplotlib.pyplot as plt
x = [1, 2, 3, 4]
mouse = [10, 12, 20, 15]
plt.bar(x, mouse)
plt.xticks(x, ['A', 'T', 'C', 'G'])
plt.savefig("fig.png", dpi=200)
\end{code}
\bigskip
Could also have \pythonstyle{\lstinline{plt.savefig("fig.png")}}
or
\pythonstyle{\lstinline{plt.savefig("fig.pdf")}}
or
\pythonstyle{\lstinline{plt.savefig("fig.tiff")}}
\end{frame}
\begin{frame}[fragile]
\frametitle{Saving the image}
\includegraphics[width=10cm]{barfig.png}
\end{frame}
\begin{frame}[fragile]
\frametitle{Midpoints of the bars}
We'd prefer the bar ticks to be at the midpoints of the bars. So we
need to calculate where the midpoints of the bars are.
If the x locations are [1, 2, 3, 4] and the bars are of width 0.8 then
the midpoints of the bars are [1.4, 2.4, 3.4, 4.4].
\begin{code}
import matplotlib.pyplot as plt
x = [1, 2, 3, 4]
mouse = [10, 12, 20, 15]
barwidth = 0.8
midpoints = []
for b in x:
midpoints.append(b + barwidth/2)
plt.bar(x, mouse, barwidth)
plt.xticks(midpoints, ['A', 'T', 'C', 'G'])
plt.show()
\end{code}
\end{frame}
\begin{frame}[fragile]
\frametitle{Multiple bars}
\begin{code}
import matplotlib.pyplot as plt
x = [1, 2, 3, 4]
mouse = [10, 12, 20, 15]
human = [11, 14, 5, 5]
barwidth = 0.4
midpoints = []
for b in x:
midpoints.append(b + barwidth)
plt.bar(x, mouse, barwidth, color="r", label="mouse")
plt.bar(midpoints,human,barwidth,color="b",label="human")
plt.xticks(midpoints, ['A', 'T', 'C', 'G'])
plt.legend()
plt.grid(True, axis='y')
plt.show()
\end{code}
\end{frame}
\begin{frame}[fragile]
\frametitle{Multiple bars}
\includegraphics[width=10cm]{twobars.png}
\end{frame}
\begin{frame}[fragile]
\frametitle{Scatter plot exercise}
Read the documentation for scatter plots:
\url{http://matplotlib.org/api/pyplot_api.html?highlight=scatter#matplotlib.pyplot.scatter}
and produce a scatter plot with green diamond
markers for the following data:
\begin{code}
import random
x = range(0,100)
y = random.sample(range(1000),100)
\end{code}
\end{frame}
\begin{frame}[fragile]
\frametitle{Summary}
Begun to experiment with:
\begin{itemize}
\item Examples of plots: plot, bar, scatter
\item Customising: labels, axes, colors, markers, positions
\item Many more matplotlib plot styles on offer
\end{itemize}
\end{frame}
\end{document}