diff --git a/AnatomyOfMatplotlib-Part0-Intro2NumPy.ipynb b/AnatomyOfMatplotlib-Part0-Intro2NumPy.ipynb index b0797f4..6f1edd1 100644 --- a/AnatomyOfMatplotlib-Part0-Intro2NumPy.ipynb +++ b/AnatomyOfMatplotlib-Part0-Intro2NumPy.ipynb @@ -1,7 +1,6 @@ { "metadata": { - "name": "", - "signature": "sha256:291c02e0eda1008690ba35d47865fd743d899737c6c702e384430e08988052a0" + "name": "" }, "nbformat": 3, "nbformat_minor": 0, @@ -268,4 +267,4 @@ "metadata": {} } ] -} \ No newline at end of file +} diff --git a/AnatomyOfMatplotlib-Part1-Figures_Subplots_and_layouts.ipynb b/AnatomyOfMatplotlib-Part1-Figures_Subplots_and_layouts.ipynb index 90ba626..791826d 100644 --- a/AnatomyOfMatplotlib-Part1-Figures_Subplots_and_layouts.ipynb +++ b/AnatomyOfMatplotlib-Part1-Figures_Subplots_and_layouts.ipynb @@ -143,11 +143,11 @@ "\n", "\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" ] }, { @@ -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()" ], @@ -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()" ], @@ -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": {}, diff --git a/AnatomyOfMatplotlib-Part2-Plotting_Methods_Overview.ipynb b/AnatomyOfMatplotlib-Part2-Plotting_Methods_Overview.ipynb index cb44d34..725f9de 100644 --- a/AnatomyOfMatplotlib-Part2-Plotting_Methods_Overview.ipynb +++ b/AnatomyOfMatplotlib-Part2-Plotting_Methods_Overview.ipynb @@ -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." ] @@ -82,8 +82,7 @@ ], "language": "python", "metadata": {}, - "outputs": [], - "prompt_number": null + "outputs": [] }, { "cell_type": "markdown", @@ -91,14 +90,12 @@ "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", "\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." ] }, { @@ -109,12 +106,13 @@ "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", @@ -122,14 +120,13 @@ ], "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`)." ] @@ -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", @@ -150,8 +147,7 @@ ], "language": "python", "metadata": {}, - "outputs": [], - "prompt_number": null + "outputs": [] }, { "cell_type": "markdown", @@ -184,8 +180,7 @@ ], "language": "python", "metadata": {}, - "outputs": [], - "prompt_number": null + "outputs": [] }, { "cell_type": "markdown", @@ -215,8 +210,7 @@ ], "language": "python", "metadata": {}, - "outputs": [], - "prompt_number": null + "outputs": [] }, { "cell_type": "markdown", @@ -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", @@ -252,13 +278,13 @@ "\n", "\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." ] }, { @@ -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. " ] }, { @@ -284,8 +310,7 @@ ], "language": "python", "metadata": {}, - "outputs": [], - "prompt_number": null + "outputs": [] }, { "cell_type": "markdown", @@ -309,8 +334,7 @@ ], "language": "python", "metadata": {}, - "outputs": [], - "prompt_number": null + "outputs": [] }, { "cell_type": "markdown", @@ -351,8 +375,7 @@ ], "language": "python", "metadata": {}, - "outputs": [], - "prompt_number": null + "outputs": [] }, { "cell_type": "markdown", @@ -373,8 +396,7 @@ ], "language": "python", "metadata": {}, - "outputs": [], - "prompt_number": null + "outputs": [] }, { "cell_type": "markdown", @@ -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": {} } ] -} \ No newline at end of file +} diff --git a/AnatomyOfMatplotlib-Part3-HowToSpeakMPL.ipynb b/AnatomyOfMatplotlib-Part3-HowToSpeakMPL.ipynb index e756d09..02bf569 100644 --- a/AnatomyOfMatplotlib-Part3-HowToSpeakMPL.ipynb +++ b/AnatomyOfMatplotlib-Part3-HowToSpeakMPL.ipynb @@ -38,8 +38,7 @@ ], "language": "python", "metadata": {}, - "outputs": [], - "prompt_number": null + "outputs": [] }, { "cell_type": "markdown", @@ -99,8 +98,31 @@ ], "language": "python", "metadata": {}, - "outputs": [], - "prompt_number": null + "outputs": [] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "t = np.arange(0.0, 5.0, 0.2)\n", + "plt.plot(t, t, , t, t**2, , t, t**3, )\n", + "plt.show()\n" + ], + "language": "python", + "metadata": {}, + "outputs": [] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "t = np.arange(0.0, 5.0, 0.2)\n", + "plt.plot(t, t, , t, t**2, , t, t**3, )\n", + "plt.show()" + ], + "language": "python", + "metadata": {}, + "outputs": [] }, { "cell_type": "markdown", @@ -143,8 +165,7 @@ ], "language": "python", "metadata": {}, - "outputs": [], - "prompt_number": null + "outputs": [] }, { "cell_type": "markdown", @@ -162,8 +183,19 @@ ], "language": "python", "metadata": {}, - "outputs": [], - "prompt_number": null + "outputs": [] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "t = np.arange(0.0, 5.0, 0.2)\n", + "plt.plot(t, t, , t, t**2, , t, t**3, )\n", + "plt.show()\n" + ], + "language": "python", + "metadata": {}, + "outputs": [] }, { "cell_type": "markdown", @@ -182,7 +214,7 @@ "' ' | draw nothing\n", "'' | draw nothing\n", "\n", - "Also, don't mix up \".-\" (line with dot markers) and \"-.\" (dash-dot line) when using the plot or scatter functions." + "Also, don't mix up \".-\" (line with dot markers) and \"-.\" (dash-dot line) when using the ``plot`` function!" ] }, { @@ -195,14 +227,13 @@ ], "language": "python", "metadata": {}, - "outputs": [], - "prompt_number": null + "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "It is a bit confusing, but the line styles mentioned above are only valid for lines. Whenever you are dealing with the linestyles of the edges of \"Patch\" objects, you will need to use words instead of the symbols. So \"solid\" instead of \"-\", and \"dashdot\" instead of \"-.\". Hopefully, this issue will be fixed for the v1.5 release and allow these specifications to be used interchangably." + "It is a bit confusing, but the line styles mentioned above are only valid for lines. Whenever you are dealing with the linestyles of the edges of \"Patch\" objects, you will need to use words instead of the symbols. So \"solid\" instead of \"-\", and \"dashdot\" instead of \"-.\". This issue will be fixed for the v2.1 release and allow these specifications to be used interchangably." ] }, { @@ -215,8 +246,7 @@ ], "language": "python", "metadata": {}, - "outputs": [], - "prompt_number": null + "outputs": [] }, { "cell_type": "markdown", @@ -237,8 +267,7 @@ ], "language": "python", "metadata": {}, - "outputs": [], - "prompt_number": null + "outputs": [] }, { "cell_type": "markdown", @@ -282,8 +311,20 @@ ], "language": "python", "metadata": {}, - "outputs": [], - "prompt_number": null + "outputs": [] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "t = np.arange(0.0, 5.0, 0.1)\n", + "a = np.exp(-t) * np.cos(2*np.pi*t)\n", + "plt.plot(t, a, )\n", + "plt.show()" + ], + "language": "python", + "metadata": {}, + "outputs": [] }, { "cell_type": "markdown", @@ -292,7 +333,7 @@ "# Colormaps\n", "Another very important property of many figures is the colormap. The job of a colormap is to relate a scalar value to a color. In addition to the regular portion of the colormap, an \"over\", \"under\" and \"bad\" color can be optionally defined as well. NaNs will trigger the \"bad\" part of the colormap.\n", "\n", - "As we all know, we create figures in order to convey information visually to our readers. There is much care and consideration that have gone into the design of these colormaps. Your choice in which colormap to use depends on what you are displaying. In mpl, the \"jet\" colormap is used by default, but it will often not be the colormap you would want to use. Much discussion has taken place on the mailing lists with regards to what colormap should be default. Ultimately, it doesn't really matter what the default colormap will be because an arbitrary colormap will be a poor choice for many datasets.\n", + "As we all know, we create figures in order to convey information visually to our readers. There is much care and consideration that have gone into the design of these colormaps. Your choice in which colormap to use depends on what you are displaying. In mpl, the \"jet\" colormap has historically been used by default, but it will often not be the colormap you would want to use. Much discussion has taken place on the mailing lists with regards to what colormap should be default. The upcoming v2.0 release of matplotlib will take on a new default colormap along with some other stylistic changes to the defaults. The defaults will be discussed this week during SciPy 2015.\n", "\n", "I want to acknowedge Nicolas Rougier and Tony Yu for putting significant effort in educating users in proper colormap selections. Here is the full gallery of all the pre-defined colormaps, organized by the types of data they are usually used for." ] @@ -305,8 +346,7 @@ ], "language": "python", "metadata": {}, - "outputs": [], - "prompt_number": null + "outputs": [] }, { "cell_type": "code", @@ -394,8 +434,7 @@ ], "language": "python", "metadata": {}, - "outputs": [], - "prompt_number": null + "outputs": [] }, { "cell_type": "markdown", @@ -416,8 +455,7 @@ ], "language": "python", "metadata": {}, - "outputs": [], - "prompt_number": null + "outputs": [] }, { "cell_type": "markdown", @@ -437,8 +475,7 @@ ], "language": "python", "metadata": {}, - "outputs": [], - "prompt_number": null + "outputs": [] }, { "cell_type": "markdown", @@ -464,8 +501,7 @@ ], "language": "python", "metadata": {}, - "outputs": [], - "prompt_number": null + "outputs": [] }, { "cell_type": "markdown", @@ -482,10 +518,10 @@ "\n", "styles = mpatches.ArrowStyle.get_styles()\n", "\n", - "ncol=2\n", + "ncol = 2\n", "nrow = (len(styles)+1) // ncol\n", "figheight = (nrow+0.5)\n", - "fig = plt.figure(1, (4.0*ncol/0.85, figheight/0.85))\n", + "fig = plt.figure(figsize=(4.0*ncol/0.85, figheight/0.85))\n", "fontsize = 0.4 * 70\n", "\n", "ax = fig.add_axes([0, 0, 1, 1])\n", @@ -518,12 +554,11 @@ " bbox=dict(boxstyle=\"square\", fc=\"w\"))\n", "\n", "ax.set_axis_off()\n", - "plt.show()\n" + "plt.show()" ], "language": "python", "metadata": {}, - "outputs": [], - "prompt_number": null + "outputs": [] }, { "cell_type": "markdown", @@ -541,8 +576,25 @@ ], "language": "python", "metadata": {}, - "outputs": [], - "prompt_number": null + "outputs": [] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "t = np.arange(0.0, 5.0, 0.01)\n", + "s = np.cos(2*np.pi*t)\n", + "plt.plot(t, s, lw=2)\n", + "\n", + "plt.annotate('local max', xy=(2, 1), xytext=(3, 1.5),\n", + " arrowprops=dict())\n", + "\n", + "plt.ylim(-2, 2)\n", + "plt.show()" + ], + "language": "python", + "metadata": {}, + "outputs": [] }, { "cell_type": "markdown", @@ -564,7 +616,9 @@ " \n", "Letters can be combined, in which case all the specified\n", "hatchings are done. If same letter repeats, it increases the\n", - "density of hatching of that pattern." + "density of hatching of that pattern.\n", + "\n", + "## Ugly tie contest!" ] }, { @@ -573,12 +627,14 @@ "input": [ "bars = plt.bar([1, 2, 3, 4], [10, 12, 15, 17])\n", "plt.setp(bars[0], hatch='x', facecolor='w')\n", + "plt.setp(bars[1], hatch='xx-', facecolor='orange')\n", + "plt.setp(bars[2], hatch='+O.', facecolor='c')\n", + "plt.setp(bars[3], hatch='*', facecolor='y')\n", "plt.show()" ], "language": "python", "metadata": {}, - "outputs": [], - "prompt_number": null + "outputs": [] }, { "cell_type": "markdown", @@ -613,8 +669,7 @@ ], "language": "python", "metadata": {}, - "outputs": [], - "prompt_number": null + "outputs": [] }, { "cell_type": "markdown", @@ -643,8 +698,7 @@ ], "language": "python", "metadata": {}, - "outputs": [], - "prompt_number": null + "outputs": [] }, { "cell_type": "markdown", @@ -657,4 +711,4 @@ "metadata": {} } ] -} \ No newline at end of file +} diff --git a/AnatomyOfMatplotlib-Part4-Limits_Legends_and_Layouts.ipynb b/AnatomyOfMatplotlib-Part4-Limits_Legends_and_Layouts.ipynb index 26dbf76..2124b0c 100644 --- a/AnatomyOfMatplotlib-Part4-Limits_Legends_and_Layouts.ipynb +++ b/AnatomyOfMatplotlib-Part4-Limits_Legends_and_Layouts.ipynb @@ -38,8 +38,7 @@ ], "language": "python", "metadata": {}, - "outputs": [], - "prompt_number": null + "outputs": [] }, { "cell_type": "markdown", @@ -72,8 +71,7 @@ ], "language": "python", "metadata": {}, - "outputs": [], - "prompt_number": null + "outputs": [] }, { "cell_type": "markdown", @@ -102,8 +100,7 @@ ], "language": "python", "metadata": {}, - "outputs": [], - "prompt_number": null + "outputs": [] }, { "cell_type": "markdown", @@ -150,8 +147,7 @@ ], "language": "python", "metadata": {}, - "outputs": [], - "prompt_number": null + "outputs": [] }, { "cell_type": "markdown", @@ -176,8 +172,7 @@ ], "language": "python", "metadata": {}, - "outputs": [], - "prompt_number": null + "outputs": [] }, { "cell_type": "code", @@ -193,8 +188,7 @@ ], "language": "python", "metadata": {}, - "outputs": [], - "prompt_number": null + "outputs": [] }, { "cell_type": "markdown", @@ -220,8 +214,7 @@ ], "language": "python", "metadata": {}, - "outputs": [], - "prompt_number": null + "outputs": [] }, { "cell_type": "markdown", @@ -246,8 +239,7 @@ ], "language": "python", "metadata": {}, - "outputs": [], - "prompt_number": null + "outputs": [] }, { "cell_type": "markdown", @@ -262,6 +254,16 @@ "Hint: You'll need to combine `ax.axis(...)` and `ax.margins(...)`. Here's the data and some code to get you started:" ] }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "%load exercises/4.1-legends_and_scaling.py" + ], + "language": "python", + "metadata": {}, + "outputs": [] + }, { "cell_type": "code", "collapsed": true, @@ -280,8 +282,7 @@ ], "language": "python", "metadata": {}, - "outputs": [], - "prompt_number": null + "outputs": [] }, { "cell_type": "markdown", @@ -319,8 +320,7 @@ ], "language": "python", "metadata": {}, - "outputs": [], - "prompt_number": null + "outputs": [] }, { "cell_type": "markdown", @@ -348,8 +348,7 @@ ], "language": "python", "metadata": {}, - "outputs": [], - "prompt_number": null + "outputs": [] }, { "cell_type": "markdown", @@ -371,8 +370,7 @@ ], "language": "python", "metadata": {}, - "outputs": [], - "prompt_number": null + "outputs": [] }, { "cell_type": "markdown", @@ -408,8 +406,7 @@ ], "language": "python", "metadata": {}, - "outputs": [], - "prompt_number": null + "outputs": [] }, { "cell_type": "markdown", @@ -438,8 +435,7 @@ ], "language": "python", "metadata": {}, - "outputs": [], - "prompt_number": null + "outputs": [] }, { "cell_type": "markdown", @@ -457,15 +453,13 @@ "ax1.plot([1, 2, 3, 4], [1, 2, 3, 4])\n", "ax2 = ax1.twinx()\n", "ax2.scatter([1, 2, 3, 4], [60, 50, 40, 30])\n", - "ax1.set_xlabel('X')\n", - "ax1.set_ylabel('First scale')\n", - "ax2.set_ylabel('Other scale')\n", + "ax1.set(xlabel='X', ylabel='First scale')\n", + "ax2.set(ylabel='Other scale')\n", "plt.show()" ], "language": "python", "metadata": {}, - "outputs": [], - "prompt_number": null + "outputs": [] }, { "cell_type": "markdown", @@ -505,21 +499,20 @@ ], "language": "python", "metadata": {}, - "outputs": [], - "prompt_number": null + "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "# Exercise 2.2\n", + "# Exercise 4.2\n", "\n", "This one is a bit trickier. Once again, try to reproduce the figure below:\n", "\n", "\n", "\n", "\n", - "A few key hints: The two subplots have no vertical space between them (this means that the `hspace` is `0`). Note that the bottom spine is at 0 in data coordinates and the tick lines are missing from the left and top sides.\n", + "A few key hints: The two subplots have no vertical space between them (this means that the `hspace` is `0`). Note that the bottom spine is at 0 in data coordinates and the tick lines are missing from the right and top sides.\n", "\n", "Because you're going to be doing a lot of the same things to both subplots, to avoid repitive code you might consider writing a function that takes an `Axes` object and makes the spine changes, etc to it. \n", "\n" @@ -533,11 +526,28 @@ ], "language": "python", "metadata": {}, - "outputs": [], - "prompt_number": 5 + "outputs": [] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "import matplotlib.pyplot as plt\n", + "import numpy as np\n", + "\n", + "# Try to reproduce the figure shown in images/exercise_4.2.png\n", + "# This one is a bit trickier!\n", + "\n", + "# Here's the data...\n", + "data = [('dogs', 4, 4), ('frogs', -3, 1), ('cats', 1, 5), ('goldfish', -2, 2)]\n", + "animals, friendliness, popularity = zip(*data)\n" + ], + "language": "python", + "metadata": {}, + "outputs": [] } ], "metadata": {} } ] -} \ No newline at end of file +} diff --git a/AnatomyOfMatplotlib-Part5-Artists.ipynb b/AnatomyOfMatplotlib-Part5-Artists.ipynb index 307dd1f..9b25d3e 100644 --- a/AnatomyOfMatplotlib-Part5-Artists.ipynb +++ b/AnatomyOfMatplotlib-Part5-Artists.ipynb @@ -38,8 +38,7 @@ ], "language": "python", "metadata": {}, - "outputs": [], - "prompt_number": null + "outputs": [] }, { "cell_type": "markdown", @@ -154,16 +153,15 @@ ], "language": "python", "metadata": {}, - "outputs": [], - "prompt_number": null + "outputs": [] }, { "cell_type": "markdown", "metadata": {}, "source": [ - "Containers are objects like *Figure* and *Axes*. Containers are given primitives to draw. The plotting functions we discussed back in Part 1 are convenience functions that generate these primitives and places them into the appropriate containers. In fact, most of those functions will return artist objects (or a list of artist objects) as well as store them into the appropriate axes container.\n", + "Containers are objects like *Figure* and *Axes*. Containers are given primitives to draw. The plotting functions we discussed back in Parts 1 & 2 are convenience functions that generate these primitives and places them into the appropriate containers. In fact, most of those functions will return artist objects (or a list of artist objects) as well as store them into the appropriate axes container.\n", "\n", - "As discussed in Part 2, there is a wide range of properties that can be defined for your plots. These properties are processed and passed down to the primitives, for your convenience. Ultimately, you can override anything you want just by directly setting a property to the object itself." + "As discussed in Part 3, there is a wide range of properties that can be defined for your plots. These properties are processed and passed down to the primitives, for your convenience. Ultimately, you can override anything you want just by directly setting a property to the object itself." ] }, { @@ -178,8 +176,7 @@ ], "language": "python", "metadata": {}, - "outputs": [], - "prompt_number": null + "outputs": [] }, { "cell_type": "markdown", @@ -192,13 +189,12 @@ "cell_type": "code", "collapsed": false, "input": [ - "fig, ax = plt.subplots(1, 1)\n", + "fig = plt.figure()\n", "print(plt.getp(fig.patch))" ], "language": "python", "metadata": {}, - "outputs": [], - "prompt_number": null + "outputs": [] }, { "cell_type": "markdown", @@ -226,8 +222,7 @@ ], "language": "python", "metadata": {}, - "outputs": [], - "prompt_number": null + "outputs": [] }, { "cell_type": "code", @@ -247,8 +242,7 @@ ], "language": "python", "metadata": {}, - "outputs": [], - "prompt_number": null + "outputs": [] }, { "cell_type": "markdown", @@ -266,8 +260,8 @@ "fig, ax = plt.subplots(1, 1)\n", "offsets = np.random.rand(20, 2)\n", "collection = RegularPolyCollection(\n", - " numsides=5, # a pentagon\n", - " sizes=(50,),\n", + " numsides=5, # a pentagon\n", + " sizes=(150,),\n", " offsets=offsets,\n", " transOffset=ax.transData,\n", " )\n", @@ -276,8 +270,7 @@ ], "language": "python", "metadata": {}, - "outputs": [], - "prompt_number": null + "outputs": [] }, { "cell_type": "markdown", @@ -297,11 +290,28 @@ ], "language": "python", "metadata": {}, - "outputs": [], - "prompt_number": null + "outputs": [] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [ + "from matplotlib.collections import StarPolygonCollection\n", + "\n", + "fig, ax = plt.subplots(1, 1)\n", + "\n", + "collection = StarPolygonCollection(5,\n", + " offsets=[(0.5, 0.5)],\n", + " transOffset=ax.transData)\n", + "ax.add_collection(collection)\n", + "plt.show()" + ], + "language": "python", + "metadata": {}, + "outputs": [] } ], "metadata": {} } ] -} \ No newline at end of file +} diff --git a/AnatomyOfMatplotlib-Part6-mpl_toolkits.ipynb b/AnatomyOfMatplotlib-Part6-mpl_toolkits.ipynb index eef1537..8ebaede 100644 --- a/AnatomyOfMatplotlib-Part6-mpl_toolkits.ipynb +++ b/AnatomyOfMatplotlib-Part6-mpl_toolkits.ipynb @@ -1,7 +1,6 @@ { "metadata": { - "name": "", - "signature": "sha256:e9c856312773c2ed88df57c5620883f24768b7e7288be4fc8f9bd7f0d7d73ba0" + "name": "" }, "nbformat": 3, "nbformat_minor": 0, @@ -12,8 +11,8 @@ "cell_type": "code", "collapsed": false, "input": [ - "# Not using inline mode here because I want to demo interactivity\n", - "%matplotlib\n", + "import matplotlib\n", + "matplotlib.use('nbagg')\n", "import numpy as np\n", "import matplotlib.pyplot as plt" ], @@ -176,7 +175,7 @@ "cell_type": "markdown", "metadata": {}, "source": [ - "And finally, as a nice, teaser of what else axes_grid1 can do..." + "And finally, as a nice teaser of what else axes_grid1 can do..." ] }, { @@ -198,7 +197,7 @@ "import mpl_toolkits.axisartist.floating_axes as floating_axes\n", "\n", "import numpy as np\n", - "import mpl_toolkits.axisartist.angle_helper as angle_helper\n", + "import mpl_toolkits.axisartist.angle_helper as angle_helper\n", "from matplotlib.projections import PolarAxes\n", "from mpl_toolkits.axisartist.grid_finder import FixedLocator, MaxNLocator, \\\n", " DictFormatter\n", @@ -348,15 +347,22 @@ " radius = np.random.rand(10)*14000.\n", " aux_ax3.scatter(theta, radius)\n", "\n", - " plt.show()\n", - "\n" + " plt.show()" ], "language": "python", "metadata": {}, "outputs": [] + }, + { + "cell_type": "code", + "collapsed": false, + "input": [], + "language": "python", + "metadata": {}, + "outputs": [] } ], "metadata": {} } ] -} \ No newline at end of file +}