Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixing up some examples, typos, and prose. #14

Merged
merged 1 commit into from
Jul 7, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Fixing up some examples, typos, and prose.
  • Loading branch information
WeatherGod committed Jul 6, 2015
commit 368663e90ca7aeeb239342070bd4a7dcb0d359fc
5 changes: 2 additions & 3 deletions AnatomyOfMatplotlib-Part0-Intro2NumPy.ipynb
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
{
"metadata": {
"name": "",
"signature": "sha256:291c02e0eda1008690ba35d47865fd743d899737c6c702e384430e08988052a0"
"name": ""
},
"nbformat": 3,
"nbformat_minor": 0,
Expand Down Expand Up @@ -268,4 +267,4 @@
"metadata": {}
}
]
}
}
26 changes: 19 additions & 7 deletions AnatomyOfMatplotlib-Part1-Figures_Subplots_and_layouts.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -143,11 +143,11 @@
"\n",
"<img src=\"images/figure_axes_axis_labeled.png\">\n",
"\n",
"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",
"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",
"\n",
"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",
"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",
"\n",
"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"
"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"
]
},
{
Expand Down Expand Up @@ -316,7 +316,7 @@
"fig = plt.figure()\n",
"ax = fig.add_subplot(111)\n",
"ax.plot([1, 2, 3, 4], [10, 20, 25, 30], color='lightblue', linewidth=3)\n",
"ax.scatter([0.3, 3.8, 1.2, 2.5], [11, 25, 9, 26], marker='^', color='darkgreen')\n",
"ax.scatter([0.3, 3.8, 1.2, 2.5], [11, 25, 9, 26], color='darkgreen', marker='^')\n",
"ax.set_xlim(0.5, 4.5)\n",
"plt.show()"
],
Expand All @@ -338,7 +338,7 @@
"collapsed": false,
"input": [
"plt.plot([1, 2, 3, 4], [10, 20, 25, 30], color='lightblue', linewidth=3)\n",
"plt.scatter([0.3, 3.8, 1.2, 2.5], [11, 25, 9, 26], marker='^', color='darkgreen')\n",
"plt.scatter([0.3, 3.8, 1.2, 2.5], [11, 25, 9, 26], color='darkgreen', marker='^')\n",
"plt.xlim(0.5, 4.5)\n",
"plt.show()"
],
Expand Down Expand Up @@ -453,17 +453,29 @@
},
{
"cell_type": "code",
"collapsed": true,
"collapsed": false,
"input": [
"%load exercises/1.1-subplots_and_basic_plotting.py"
],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"import numpy as np\n",
"import matplotlib.pyplot as plt\n",
"\n",
"# Try to reproduce the figure shown in images/exercise_1-1.png\n",
"\n",
"# Our data...\n",
"x = np.linspace(0, 10, 100)\n",
"y1, y2, y3 = np.cos(x), np.cos(x + 1), np.cos(x + 2)\n",
"names = ['Signal 1', 'Signal 2', 'Signal 3']\n",
"\n",
"# Can you figure out what to do next to plot x vs y1, y2, and y3?"
"# Can you figure out what to do next to plot x vs y1, y2, and y3 on one figure?\n"
],
"language": "python",
"metadata": {},
Expand Down
113 changes: 79 additions & 34 deletions AnatomyOfMatplotlib-Part2-Plotting_Methods_Overview.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
"\n",
"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",
"\n",
"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",
"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",
"\n",
"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."
]
Expand Down Expand Up @@ -82,23 +82,20 @@
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": null
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Input Data: 1D Series\n",
"\n",
"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",
"\n",
"Rather than revisiting `plot` and `scatter`, let's move on to a couple of other common plot styles.\n",
"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",
"\n",
"### Bar Plots: `ax.bar(...)` and `ax.barh(...)`\n",
"<img src=\"images/bar_example.png\">\n",
"\n",
"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."
"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."
]
},
{
Expand All @@ -109,27 +106,27 @@
"x = np.arange(5)\n",
"y = np.random.randn(5)\n",
"\n",
"fig, axes = plt.subplots(ncols=2, figsize=plt.figaspect(0.4))\n",
"fig, axes = plt.subplots(ncols=2, figsize=plt.figaspect(1./2))\n",
"\n",
"vert_bars = axes[0].bar(x, y, color='lightblue', align='center')\n",
"horiz_bars = axes[1].barh(x, y, color='lightblue', align='center')\n",
"\n",
"# I'll also introduce axhline & axvline to draw a line all the way across the axes\n",
"# This can be a quick-n-easy way to draw an axis \"spine\".\n",
"axes[0].axhline(0, color='gray', linewidth=2)\n",
"axes[1].axvline(0, color='gray', linewidth=2)\n",
"\n",
"plt.show()"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": null
"outputs": []
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"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",
"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",
"\n",
"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`)."
]
Expand All @@ -141,7 +138,7 @@
"fig, ax = plt.subplots()\n",
"vert_bars = ax.bar(x, y, color='lightblue', align='center')\n",
"\n",
"# We could do this with two separate calls to `ax.bar` and numpy boolean indexing, as well.\n",
"# We could have also done this with two separate calls to `ax.bar` and numpy boolean indexing.\n",
"for bar in vert_bars:\n",
" if bar.xy[1] < 0:\n",
" bar.set(edgecolor='darkred', color='salmon', linewidth=3)\n",
Expand All @@ -150,8 +147,7 @@
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": null
"outputs": []
},
{
"cell_type": "markdown",
Expand Down Expand Up @@ -184,8 +180,7 @@
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": null
"outputs": []
},
{
"cell_type": "markdown",
Expand Down Expand Up @@ -215,8 +210,7 @@
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": null
"outputs": []
},
{
"cell_type": "markdown",
Expand All @@ -238,8 +232,40 @@
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": null
"outputs": []
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"import numpy as np\n",
"import matplotlib.pyplot as plt\n",
"np.random.seed(1)\n",
"\n",
"# Generate data...\n",
"y_raw = np.random.randn(1000).cumsum() + 15\n",
"x_raw = np.linspace(0, 24, y_raw.size)\n",
"\n",
"# Get averages of every 100 samples...\n",
"x_pos = x_raw.reshape(-1, 100).min(axis=1)\n",
"y_avg = y_raw.reshape(-1, 100).mean(axis=1)\n",
"y_err = y_raw.reshape(-1, 100).ptp(axis=1)\n",
"\n",
"bar_width = x_pos[1] - x_pos[0]\n",
"\n",
"# Make a made up future prediction with a fake confidence\n",
"x_pred = np.linspace(0, 30)\n",
"y_max_pred = y_avg[0] + y_err[0] + 2.3 * x_pred\n",
"y_min_pred = y_avg[0] - y_err[0] + 1.2 * x_pred\n",
"\n",
"# Just so you don't have to guess at the colors...\n",
"barcolor, linecolor, fillcolor = 'wheat', 'salmon', 'lightblue'\n",
"\n",
"# Now you're on your own!\n"
],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "markdown",
Expand All @@ -252,13 +278,13 @@
"<img src=\"images/imshow_example.png\">\n",
"<img src=\"images/pcolor_example.png\">\n",
"\n",
"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",
"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",
"\n",
"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",
"\n",
"Instead we'll focus on what they have in common.\n",
"\n",
"`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."
"`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."
]
},
{
Expand All @@ -267,7 +293,7 @@
"source": [
"### Colorbars\n",
"\n",
"Next, let's add a colorbar to the figure to display what colors correspond to values of `data` we've plotted. "
"Let's add a colorbar to the figure to display what colors correspond to values of `data` we've plotted. "
]
},
{
Expand All @@ -284,8 +310,7 @@
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": null
"outputs": []
},
{
"cell_type": "markdown",
Expand All @@ -309,8 +334,7 @@
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": null
"outputs": []
},
{
"cell_type": "markdown",
Expand Down Expand Up @@ -351,8 +375,7 @@
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": null
"outputs": []
},
{
"cell_type": "markdown",
Expand All @@ -373,8 +396,7 @@
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": null
"outputs": []
},
{
"cell_type": "markdown",
Expand All @@ -396,11 +418,34 @@
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": null
"outputs": []
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"import numpy as np\n",
"import matplotlib.pyplot as plt\n",
"np.random.seed(1)\n",
"\n",
"# Generate random data with different ranges...\n",
"data1 = np.random.random((10, 10))\n",
"data2 = 2 * np.random.random((10, 10))\n",
"data3 = 3 * np.random.random((10, 10))\n",
"\n",
"# Set up our figure and axes...\n",
"fig, axes = plt.subplots(ncols=3, figsize=plt.figaspect(0.5))\n",
"fig.tight_layout() # Make the subplots fill up the figure a bit more...\n",
"cax = fig.add_axes([0.25, 0.1, 0.55, 0.03]) # Add an axes for the colorbar\n",
"\n",
"# Now you're on your own!\n"
],
"language": "python",
"metadata": {},
"outputs": []
}
],
"metadata": {}
}
]
}
}
Loading