Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit e06634f

Browse filesBrowse files
committed
Merge pull request #14 from WeatherGod/polishing
Fixing up some examples, typos, and prose.
2 parents e93c5c4 + 368663e commit e06634f
Copy full SHA for e06634f
Expand file treeCollapse file tree

7 files changed

+295
-159
lines changed

‎AnatomyOfMatplotlib-Part0-Intro2NumPy.ipynb

Copy file name to clipboardExpand all lines: AnatomyOfMatplotlib-Part0-Intro2NumPy.ipynb
+2-3Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
{
22
"metadata": {
3-
"name": "",
4-
"signature": "sha256:291c02e0eda1008690ba35d47865fd743d899737c6c702e384430e08988052a0"
3+
"name": ""
54
},
65
"nbformat": 3,
76
"nbformat_minor": 0,
@@ -268,4 +267,4 @@
268267
"metadata": {}
269268
}
270269
]
271-
}
270+
}

‎AnatomyOfMatplotlib-Part1-Figures_Subplots_and_layouts.ipynb

Copy file name to clipboardExpand all lines: AnatomyOfMatplotlib-Part1-Figures_Subplots_and_layouts.ipynb
+19-7Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -143,11 +143,11 @@
143143
"\n",
144144
"<img src=\"images/figure_axes_axis_labeled.png\">\n",
145145
"\n",
146-
"The ``Figure`` is the top-level container in this hierarchy. It is the overall window/page that everything is drawn on. You can have multiple independent figures. However, ``Figure``s can contain multiple ``Axes``. \n",
146+
"The ``Figure`` is the top-level container in this hierarchy. It is the overall window/page that everything is drawn on. You can have multiple independent figures and ``Figure``s can contain multiple ``Axes``. \n",
147147
"\n",
148-
"Most plotting ocurs on an ``Axes``. The axes is effectively the area that we plot data on and any ticks/labels/etc associated with it. Usually we'll set up an Axes with a call to ``subplot`` (which places Axes on a regular grid), so in most cases we'll deal with here, ``Axes`` and ``Subplot`` are synonymous. We'll be heavily using ``Axes`` instances for plotting, etc, so you'll be seeing a lot of these.\n",
148+
"Most plotting ocurs on an ``Axes``. The axes is effectively the area that we plot data on and any ticks/labels/etc associated with it. Usually we'll set up an Axes with a call to ``subplot`` (which places Axes on a regular grid), so in most cases, ``Axes`` and ``Subplot`` are synonymous.\n",
149149
"\n",
150-
"Each ``Axes`` has an ``XAxis`` and a ``YAxis``. These contain the ticks, tick locations, labels, etc. In this tutorial, we'll mostly control ticks, tick labels, and data limits through other mechanisms, so we won't touch the individual ``Axis`` part of things much at all. However, it's worth mentioning here to explain where the term ``Axes`` comes from.\n"
150+
"Each ``Axes`` has an ``XAxis`` and a ``YAxis``. These contain the ticks, tick locations, labels, etc. In this tutorial, we'll mostly control ticks, tick labels, and data limits through other mechanisms, so we won't touch the individual ``Axis`` part of things all that much. However, it's worth mentioning here to explain where the term ``Axes`` comes from.\n"
151151
]
152152
},
153153
{
@@ -316,7 +316,7 @@
316316
"fig = plt.figure()\n",
317317
"ax = fig.add_subplot(111)\n",
318318
"ax.plot([1, 2, 3, 4], [10, 20, 25, 30], color='lightblue', linewidth=3)\n",
319-
"ax.scatter([0.3, 3.8, 1.2, 2.5], [11, 25, 9, 26], marker='^', color='darkgreen')\n",
319+
"ax.scatter([0.3, 3.8, 1.2, 2.5], [11, 25, 9, 26], color='darkgreen', marker='^')\n",
320320
"ax.set_xlim(0.5, 4.5)\n",
321321
"plt.show()"
322322
],
@@ -338,7 +338,7 @@
338338
"collapsed": false,
339339
"input": [
340340
"plt.plot([1, 2, 3, 4], [10, 20, 25, 30], color='lightblue', linewidth=3)\n",
341-
"plt.scatter([0.3, 3.8, 1.2, 2.5], [11, 25, 9, 26], marker='^', color='darkgreen')\n",
341+
"plt.scatter([0.3, 3.8, 1.2, 2.5], [11, 25, 9, 26], color='darkgreen', marker='^')\n",
342342
"plt.xlim(0.5, 4.5)\n",
343343
"plt.show()"
344344
],
@@ -453,17 +453,29 @@
453453
},
454454
{
455455
"cell_type": "code",
456-
"collapsed": true,
456+
"collapsed": false,
457+
"input": [
458+
"%load exercises/1.1-subplots_and_basic_plotting.py"
459+
],
460+
"language": "python",
461+
"metadata": {},
462+
"outputs": []
463+
},
464+
{
465+
"cell_type": "code",
466+
"collapsed": false,
457467
"input": [
458468
"import numpy as np\n",
459469
"import matplotlib.pyplot as plt\n",
460470
"\n",
471+
"# Try to reproduce the figure shown in images/exercise_1-1.png\n",
472+
"\n",
461473
"# Our data...\n",
462474
"x = np.linspace(0, 10, 100)\n",
463475
"y1, y2, y3 = np.cos(x), np.cos(x + 1), np.cos(x + 2)\n",
464476
"names = ['Signal 1', 'Signal 2', 'Signal 3']\n",
465477
"\n",
466-
"# Can you figure out what to do next to plot x vs y1, y2, and y3?"
478+
"# Can you figure out what to do next to plot x vs y1, y2, and y3 on one figure?\n"
467479
],
468480
"language": "python",
469481
"metadata": {},

‎AnatomyOfMatplotlib-Part2-Plotting_Methods_Overview.ipynb

Copy file name to clipboardExpand all lines: AnatomyOfMatplotlib-Part2-Plotting_Methods_Overview.ipynb
+79-34Lines changed: 79 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
"\n",
3333
"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",
3434
"\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",
3636
"\n",
3737
"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."
3838
]
@@ -82,23 +82,20 @@
8282
],
8383
"language": "python",
8484
"metadata": {},
85-
"outputs": [],
86-
"prompt_number": null
85+
"outputs": []
8786
},
8887
{
8988
"cell_type": "markdown",
9089
"metadata": {},
9190
"source": [
9291
"# Input Data: 1D Series\n",
9392
"\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",
9794
"\n",
9895
"### Bar Plots: `ax.bar(...)` and `ax.barh(...)`\n",
9996
"<img src=\"images/bar_example.png\">\n",
10097
"\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."
10299
]
103100
},
104101
{
@@ -109,27 +106,27 @@
109106
"x = np.arange(5)\n",
110107
"y = np.random.randn(5)\n",
111108
"\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",
113110
"\n",
114111
"vert_bars = axes[0].bar(x, y, color='lightblue', align='center')\n",
115112
"horiz_bars = axes[1].barh(x, y, color='lightblue', align='center')\n",
116113
"\n",
117114
"# 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",
118116
"axes[0].axhline(0, color='gray', linewidth=2)\n",
119117
"axes[1].axvline(0, color='gray', linewidth=2)\n",
120118
"\n",
121119
"plt.show()"
122120
],
123121
"language": "python",
124122
"metadata": {},
125-
"outputs": [],
126-
"prompt_number": null
123+
"outputs": []
127124
},
128125
{
129126
"cell_type": "markdown",
130127
"metadata": {},
131128
"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",
133130
"\n",
134131
"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`)."
135132
]
@@ -141,7 +138,7 @@
141138
"fig, ax = plt.subplots()\n",
142139
"vert_bars = ax.bar(x, y, color='lightblue', align='center')\n",
143140
"\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",
145142
"for bar in vert_bars:\n",
146143
" if bar.xy[1] < 0:\n",
147144
" bar.set(edgecolor='darkred', color='salmon', linewidth=3)\n",
@@ -150,8 +147,7 @@
150147
],
151148
"language": "python",
152149
"metadata": {},
153-
"outputs": [],
154-
"prompt_number": null
150+
"outputs": []
155151
},
156152
{
157153
"cell_type": "markdown",
@@ -184,8 +180,7 @@
184180
],
185181
"language": "python",
186182
"metadata": {},
187-
"outputs": [],
188-
"prompt_number": null
183+
"outputs": []
189184
},
190185
{
191186
"cell_type": "markdown",
@@ -215,8 +210,7 @@
215210
],
216211
"language": "python",
217212
"metadata": {},
218-
"outputs": [],
219-
"prompt_number": null
213+
"outputs": []
220214
},
221215
{
222216
"cell_type": "markdown",
@@ -238,8 +232,40 @@
238232
],
239233
"language": "python",
240234
"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": []
243269
},
244270
{
245271
"cell_type": "markdown",
@@ -252,13 +278,13 @@
252278
"<img src=\"images/imshow_example.png\">\n",
253279
"<img src=\"images/pcolor_example.png\">\n",
254280
"\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",
256282
"\n",
257283
"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",
258284
"\n",
259285
"Instead we'll focus on what they have in common.\n",
260286
"\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."
262288
]
263289
},
264290
{
@@ -267,7 +293,7 @@
267293
"source": [
268294
"### Colorbars\n",
269295
"\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. "
271297
]
272298
},
273299
{
@@ -284,8 +310,7 @@
284310
],
285311
"language": "python",
286312
"metadata": {},
287-
"outputs": [],
288-
"prompt_number": null
313+
"outputs": []
289314
},
290315
{
291316
"cell_type": "markdown",
@@ -309,8 +334,7 @@
309334
],
310335
"language": "python",
311336
"metadata": {},
312-
"outputs": [],
313-
"prompt_number": null
337+
"outputs": []
314338
},
315339
{
316340
"cell_type": "markdown",
@@ -351,8 +375,7 @@
351375
],
352376
"language": "python",
353377
"metadata": {},
354-
"outputs": [],
355-
"prompt_number": null
378+
"outputs": []
356379
},
357380
{
358381
"cell_type": "markdown",
@@ -373,8 +396,7 @@
373396
],
374397
"language": "python",
375398
"metadata": {},
376-
"outputs": [],
377-
"prompt_number": null
399+
"outputs": []
378400
},
379401
{
380402
"cell_type": "markdown",
@@ -396,11 +418,34 @@
396418
],
397419
"language": "python",
398420
"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": []
401446
}
402447
],
403448
"metadata": {}
404449
}
405450
]
406-
}
451+
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.