Newer
Older
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Notebook 4 - Basic Plotting"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"In this notebook, we will explore the basic plot interface using ``matplotlib.pyplot``. We will just scratch the surface of this vastly capable package: you can find out more about matplotlib [here](https://matplotlib.org/).\n",
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
"This notebook a matplotlib version of [this web tutorial](http://jakevdp.github.io/mpl_tutorial/tutorial_pages/tut1.html#)."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## A first plot: the matplotlib interface"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"To use matplotlib, we will need to import it. Since matplotlib is built on and designed to work with numpy, we should import this too."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import matplotlib.pyplot as plt\n",
"import numpy as np"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Let's make some simple data to plot: a sinusoid"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"scrolled": false
},
"outputs": [],
"source": [
"x = np.arange(0, 20, 0.02) # 100 evenly-spaced values from 0 to 50\n",
"y = np.sin(x)\n",
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
"plt.plot(x, y)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Customizing the plot: Axes Limits"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Let's play around with this a bit: first we can change the axis limits using ``xlim()`` and ``ylim()``"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"plt.plot(x, y)\n",
"plt.xlim(5, 15)\n",
"plt.ylim(-1.2, 1.2)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Customizing the plot: Axes Labels and Titles"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We can label the axes and add a title:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"plt.plot(x, y)\n",
"\n",
"plt.xlabel('this is x!')\n",
"plt.ylabel('this is y!')\n",
"plt.title('My First Plot')\n",
"plt.show()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Labels can also be rendered using LaTeX symbols:"
"metadata": {},
"outputs": [],
"source": [
"y = np.sin(2 * np.pi * x)\n",
"plt.plot(x, y)\n",
"plt.title(r'$\\sin(2 \\pi x)$') # the `r` before the string indicates a \"raw string\""
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
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Customizing the plot: Line Styles"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We can vary the line color or the line symbol:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"plt.plot(x, y, '-r') # solid red line ('r' comes from RGB color scheme)\n",
"plt.xlim(0, 10)\n",
"plt.ylim(-1.2, 1.2)\n",
"\n",
"plt.xlabel('this is x!')\n",
"plt.ylabel('this is y!')\n",
"plt.title('My First Plot')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Other options for the color characters are:\n",
"\n",
" 'r' = red\n",
" 'g' = green\n",
" 'b' = blue\n",
" 'c' = cyan\n",
" 'm' = magenta\n",
" 'y' = yellow\n",
" 'k' = black\n",
" 'w' = white\n",
"\n",
"Options for line styles are\n",
"\n",
" '-' = solid\n",
" '--' = dashed\n",
" ':' = dotted\n",
" '-.' = dot-dashed\n",
" '.' = points\n",
" 'o' = filled circles\n",
" '^' = filled triangles\n",
"\n",
"and many, many more.\n",
"\n",
"For more information, view the documentation of the plot function. In IPython, this can be\n",
"accomplished using the ``?`` functionality:\n",
" "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"plt.plot?"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Also see the online version of this help: http://matplotlib.org/api/pyplot_api.html#matplotlib.pyplot.plot"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Cusomizing the Plot: Legends"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Multiple lines can be shown on the same plot. In this case, you can use a legend\n",
"to label the two lines:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"x = np.arange(0, 20, 0.02)\n",
"y1 = np.sin(x)\n",
"y2 = np.cos(x)\n",
"\n",
"plt.plot(x, y1, '-b', label='sine')\n",
"plt.plot(x, y2, '-r', label='cosine')\n",
"plt.legend(loc='upper right')\n",
"plt.ylim(-1.5, 2.0)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Exercise: Linestyles & Plot Customization"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Below are two sets of arrays ``x1, y1``, and ``x2, y2``. Create a plot where\n",
"``x1`` and ``y1`` are represented by blue circles, and ``x2`` and ``y2`` are\n",
"represented by a dotted black line. Label the symbols \"sampled\" and\n",
"\"continuous\", and add a legend. Adjust the y limits to suit your taste."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"x1 = np.arange(0, 10, 0.5)\n",
"y1 = np.sin(x1)\n",
"\n",
"x2 = np.arange(0, 10, 0.01)\n",
"y2 = np.sin(x2)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",