|
32 | 32 | "\n",
|
33 | 33 | "We've talked a lot about laying things out, etc, but we haven't talked about actually plotting data yet. Matplotlib has a number of different plotting functions -- many more than we'll cover here, in fact. There's a more complete list in the pyplot documentation, and matplotlib gallery is a great place to get examples of all of them. \n",
|
34 | 34 | "\n",
|
35 |
| - "However, a full list and/or the gallery can be a bit overwhelming at first. Instead we'll condense it down a bit, give you a look at some of the ones you're most likely to use, and then go over a subset of those in more detail.\n", |
| 35 | + "However, a full list and/or the gallery can be a bit overwhelming at first. Instead we'll condense it down and give you a look at some of the ones you're most likely to use, and then go over a subset of those in more detail.\n", |
36 | 36 | "\n",
|
37 | 37 | "Here's a simplified visual overview of matplotlib's most commonly used plot types. Let's browse through these, and then we'll go over a few in more detail. Clicking on any of these images will take you to the code that generated them. We'll skip that for now, but feel browse through it later."
|
38 | 38 | ]
|
|
82 | 82 | ],
|
83 | 83 | "language": "python",
|
84 | 84 | "metadata": {},
|
85 |
| - "outputs": [], |
86 |
| - "prompt_number": null |
| 85 | + "outputs": [] |
87 | 86 | },
|
88 | 87 | {
|
89 | 88 | "cell_type": "markdown",
|
90 | 89 | "metadata": {},
|
91 | 90 | "source": [
|
92 | 91 | "# Input Data: 1D Series\n",
|
93 | 92 | "\n",
|
94 |
| - "We've briefly mentioned `ax.plot(x, y)` and `ax.scatter(x, y)` to draw lines and points, respectively. We'll cover some of their options (markers, colors, linestyles, etc) in the next section.\n", |
95 |
| - "\n", |
96 |
| - "Rather than revisiting `plot` and `scatter`, let's move on to a couple of other common plot styles.\n", |
| 93 | + "We've briefly mentioned `ax.plot(x, y)` and `ax.scatter(x, y)` to draw lines and points, respectively. We'll cover some of their options (markers, colors, linestyles, etc) in the next section. Let's move on to a couple of other common plot types.\n", |
97 | 94 | "\n",
|
98 | 95 | "### Bar Plots: `ax.bar(...)` and `ax.barh(...)`\n",
|
99 | 96 | "<img src=\"images/bar_example.png\">\n",
|
100 | 97 | "\n",
|
101 |
| - "Bar plots are one of the most common plot types. Matplotlib's `ax.bar(...)` method can also plot general rectangles, but the default is optimized for a simple sequence of x, y values, where the rectangles have a constant width. There's also `ax.barh(...)`, which makes a constant-height assumption instead of a constant-width assumption." |
| 98 | + "Bar plots are one of the most common plot types. Matplotlib's `ax.bar(...)` method can also plot general rectangles, but the default is optimized for a simple sequence of x, y values, where the rectangles have a constant width. There's also `ax.barh(...)` (for horizontal), which makes a constant-height assumption instead of a constant-width assumption." |
102 | 99 | ]
|
103 | 100 | },
|
104 | 101 | {
|
|
109 | 106 | "x = np.arange(5)\n",
|
110 | 107 | "y = np.random.randn(5)\n",
|
111 | 108 | "\n",
|
112 |
| - "fig, axes = plt.subplots(ncols=2, figsize=plt.figaspect(0.4))\n", |
| 109 | + "fig, axes = plt.subplots(ncols=2, figsize=plt.figaspect(1./2))\n", |
113 | 110 | "\n",
|
114 | 111 | "vert_bars = axes[0].bar(x, y, color='lightblue', align='center')\n",
|
115 | 112 | "horiz_bars = axes[1].barh(x, y, color='lightblue', align='center')\n",
|
116 | 113 | "\n",
|
117 | 114 | "# I'll also introduce axhline & axvline to draw a line all the way across the axes\n",
|
| 115 | + "# This can be a quick-n-easy way to draw an axis \"spine\".\n", |
118 | 116 | "axes[0].axhline(0, color='gray', linewidth=2)\n",
|
119 | 117 | "axes[1].axvline(0, color='gray', linewidth=2)\n",
|
120 | 118 | "\n",
|
121 | 119 | "plt.show()"
|
122 | 120 | ],
|
123 | 121 | "language": "python",
|
124 | 122 | "metadata": {},
|
125 |
| - "outputs": [], |
126 |
| - "prompt_number": null |
| 123 | + "outputs": [] |
127 | 124 | },
|
128 | 125 | {
|
129 | 126 | "cell_type": "markdown",
|
130 | 127 | "metadata": {},
|
131 | 128 | "source": [
|
132 |
| - "Note that we held on to what `ax.bar(...)` returned. Matplotlib plotting methods return an `Artist` or a sequence of artists. Anything you can see in a matplotlib figure/axes/etc is an `Artist` of some sort.\n", |
| 129 | + "Note that we held on to what `ax.bar(...)` returned. Matplotlib plotting methods return an `Artist` or a sequence of artists. Anything you can see in a matplotlib figure/axes/etc is an `Artist` of some sort. Most of the time, you will not need to retain these returned objects. You will want to capture them for special customizing that may not be possible through the normal plotting mechanism.\n", |
133 | 130 | "\n",
|
134 | 131 | "Let's re-visit that last example and modify what's plotted. In the case of `bar`, a container artist is returned, so we'll modify its contents instead of the container itself (thus `for bar in vert_bars`)."
|
135 | 132 | ]
|
|
141 | 138 | "fig, ax = plt.subplots()\n",
|
142 | 139 | "vert_bars = ax.bar(x, y, color='lightblue', align='center')\n",
|
143 | 140 | "\n",
|
144 |
| - "# We could do this with two separate calls to `ax.bar` and numpy boolean indexing, as well.\n", |
| 141 | + "# We could have also done this with two separate calls to `ax.bar` and numpy boolean indexing.\n", |
145 | 142 | "for bar in vert_bars:\n",
|
146 | 143 | " if bar.xy[1] < 0:\n",
|
147 | 144 | " bar.set(edgecolor='darkred', color='salmon', linewidth=3)\n",
|
|
150 | 147 | ],
|
151 | 148 | "language": "python",
|
152 | 149 | "metadata": {},
|
153 |
| - "outputs": [], |
154 |
| - "prompt_number": null |
| 150 | + "outputs": [] |
155 | 151 | },
|
156 | 152 | {
|
157 | 153 | "cell_type": "markdown",
|
|
184 | 180 | ],
|
185 | 181 | "language": "python",
|
186 | 182 | "metadata": {},
|
187 |
| - "outputs": [], |
188 |
| - "prompt_number": null |
| 183 | + "outputs": [] |
189 | 184 | },
|
190 | 185 | {
|
191 | 186 | "cell_type": "markdown",
|
|
215 | 210 | ],
|
216 | 211 | "language": "python",
|
217 | 212 | "metadata": {},
|
218 |
| - "outputs": [], |
219 |
| - "prompt_number": null |
| 213 | + "outputs": [] |
220 | 214 | },
|
221 | 215 | {
|
222 | 216 | "cell_type": "markdown",
|
|
238 | 232 | ],
|
239 | 233 | "language": "python",
|
240 | 234 | "metadata": {},
|
241 |
| - "outputs": [], |
242 |
| - "prompt_number": null |
| 235 | + "outputs": [] |
| 236 | + }, |
| 237 | + { |
| 238 | + "cell_type": "code", |
| 239 | + "collapsed": false, |
| 240 | + "input": [ |
| 241 | + "import numpy as np\n", |
| 242 | + "import matplotlib.pyplot as plt\n", |
| 243 | + "np.random.seed(1)\n", |
| 244 | + "\n", |
| 245 | + "# Generate data...\n", |
| 246 | + "y_raw = np.random.randn(1000).cumsum() + 15\n", |
| 247 | + "x_raw = np.linspace(0, 24, y_raw.size)\n", |
| 248 | + "\n", |
| 249 | + "# Get averages of every 100 samples...\n", |
| 250 | + "x_pos = x_raw.reshape(-1, 100).min(axis=1)\n", |
| 251 | + "y_avg = y_raw.reshape(-1, 100).mean(axis=1)\n", |
| 252 | + "y_err = y_raw.reshape(-1, 100).ptp(axis=1)\n", |
| 253 | + "\n", |
| 254 | + "bar_width = x_pos[1] - x_pos[0]\n", |
| 255 | + "\n", |
| 256 | + "# Make a made up future prediction with a fake confidence\n", |
| 257 | + "x_pred = np.linspace(0, 30)\n", |
| 258 | + "y_max_pred = y_avg[0] + y_err[0] + 2.3 * x_pred\n", |
| 259 | + "y_min_pred = y_avg[0] - y_err[0] + 1.2 * x_pred\n", |
| 260 | + "\n", |
| 261 | + "# Just so you don't have to guess at the colors...\n", |
| 262 | + "barcolor, linecolor, fillcolor = 'wheat', 'salmon', 'lightblue'\n", |
| 263 | + "\n", |
| 264 | + "# Now you're on your own!\n" |
| 265 | + ], |
| 266 | + "language": "python", |
| 267 | + "metadata": {}, |
| 268 | + "outputs": [] |
243 | 269 | },
|
244 | 270 | {
|
245 | 271 | "cell_type": "markdown",
|
|
252 | 278 | "<img src=\"images/imshow_example.png\">\n",
|
253 | 279 | "<img src=\"images/pcolor_example.png\">\n",
|
254 | 280 | "\n",
|
255 |
| - "In short, `imshow` can interpolate and display large arrays very quickly, while `pcolormesh` and `pcolor` are much slower, but can handle much flexible (i.e. more than just rectangular) arrangements of cells.\n", |
| 281 | + "In short, `imshow` can interpolate and display large arrays very quickly, while `pcolormesh` and `pcolor` are much slower, but can handle flexible (i.e. more than just rectangular) arrangements of cells.\n", |
256 | 282 | "\n",
|
257 | 283 | "We won't dwell too much on the differences and overlaps here. They have overlapping capabilities, but different default behavior because their primary use-cases are a bit different (there's also `matshow`, which is `imshow` with different defaults). \n",
|
258 | 284 | "\n",
|
259 | 285 | "Instead we'll focus on what they have in common.\n",
|
260 | 286 | "\n",
|
261 |
| - "`imshow`, `pcolor`, `pcolormesh`, `scatter`, and any other matplotlib plotting methods that map a range of data values onto a colormap return artists that are instances of `ScalarMappable.` In practice, what that means is a) you can display a colorbar for them, and b) they share several keyword arguments." |
| 287 | + "`imshow`, `pcolor`, `pcolormesh`, `scatter`, and any other matplotlib plotting methods that map a range of data values onto a colormap will return artists that are instances of `ScalarMappable.` In practice, what that means is a) you can display a colorbar for them, and b) they share several keyword arguments." |
262 | 288 | ]
|
263 | 289 | },
|
264 | 290 | {
|
|
267 | 293 | "source": [
|
268 | 294 | "### Colorbars\n",
|
269 | 295 | "\n",
|
270 |
| - "Next, let's add a colorbar to the figure to display what colors correspond to values of `data` we've plotted. " |
| 296 | + "Let's add a colorbar to the figure to display what colors correspond to values of `data` we've plotted. " |
271 | 297 | ]
|
272 | 298 | },
|
273 | 299 | {
|
|
284 | 310 | ],
|
285 | 311 | "language": "python",
|
286 | 312 | "metadata": {},
|
287 |
| - "outputs": [], |
288 |
| - "prompt_number": null |
| 313 | + "outputs": [] |
289 | 314 | },
|
290 | 315 | {
|
291 | 316 | "cell_type": "markdown",
|
|
309 | 334 | ],
|
310 | 335 | "language": "python",
|
311 | 336 | "metadata": {},
|
312 |
| - "outputs": [], |
313 |
| - "prompt_number": null |
| 337 | + "outputs": [] |
314 | 338 | },
|
315 | 339 | {
|
316 | 340 | "cell_type": "markdown",
|
|
351 | 375 | ],
|
352 | 376 | "language": "python",
|
353 | 377 | "metadata": {},
|
354 |
| - "outputs": [], |
355 |
| - "prompt_number": null |
| 378 | + "outputs": [] |
356 | 379 | },
|
357 | 380 | {
|
358 | 381 | "cell_type": "markdown",
|
|
373 | 396 | ],
|
374 | 397 | "language": "python",
|
375 | 398 | "metadata": {},
|
376 |
| - "outputs": [], |
377 |
| - "prompt_number": null |
| 399 | + "outputs": [] |
378 | 400 | },
|
379 | 401 | {
|
380 | 402 | "cell_type": "markdown",
|
|
396 | 418 | ],
|
397 | 419 | "language": "python",
|
398 | 420 | "metadata": {},
|
399 |
| - "outputs": [], |
400 |
| - "prompt_number": null |
| 421 | + "outputs": [] |
| 422 | + }, |
| 423 | + { |
| 424 | + "cell_type": "code", |
| 425 | + "collapsed": false, |
| 426 | + "input": [ |
| 427 | + "import numpy as np\n", |
| 428 | + "import matplotlib.pyplot as plt\n", |
| 429 | + "np.random.seed(1)\n", |
| 430 | + "\n", |
| 431 | + "# Generate random data with different ranges...\n", |
| 432 | + "data1 = np.random.random((10, 10))\n", |
| 433 | + "data2 = 2 * np.random.random((10, 10))\n", |
| 434 | + "data3 = 3 * np.random.random((10, 10))\n", |
| 435 | + "\n", |
| 436 | + "# Set up our figure and axes...\n", |
| 437 | + "fig, axes = plt.subplots(ncols=3, figsize=plt.figaspect(0.5))\n", |
| 438 | + "fig.tight_layout() # Make the subplots fill up the figure a bit more...\n", |
| 439 | + "cax = fig.add_axes([0.25, 0.1, 0.55, 0.03]) # Add an axes for the colorbar\n", |
| 440 | + "\n", |
| 441 | + "# Now you're on your own!\n" |
| 442 | + ], |
| 443 | + "language": "python", |
| 444 | + "metadata": {}, |
| 445 | + "outputs": [] |
401 | 446 | }
|
402 | 447 | ],
|
403 | 448 | "metadata": {}
|
404 | 449 | }
|
405 | 450 | ]
|
406 |
| -} |
| 451 | +} |
0 commit comments