data-science-ipython-notebooks/matplotlib/04.03-Errorbars.ipynb

257 lines
46 KiB
Python
Raw Permalink Normal View History

{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<!--BOOK_INFORMATION-->\n",
"<img align=\"left\" style=\"padding-right:10px;\" src=\"figures/PDSH-cover-small.png\">\n",
"*This notebook contains an excerpt from the [Python Data Science Handbook](http://shop.oreilly.com/product/0636920034919.do) by Jake VanderPlas; the content is available [on GitHub](https://github.com/jakevdp/PythonDataScienceHandbook).*\n",
"\n",
"*The text is released under the [CC-BY-NC-ND license](https://creativecommons.org/licenses/by-nc-nd/3.0/us/legalcode), and code is released under the [MIT license](https://opensource.org/licenses/MIT). If you find this content useful, please consider supporting the work by [buying the book](http://shop.oreilly.com/product/0636920034919.do)!*\n",
"\n",
"*No changes were made to the contents of this notebook from the original.*"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<!--NAVIGATION-->\n",
"< [Simple Scatter Plots](04.02-Simple-Scatter-Plots.ipynb) | [Contents](Index.ipynb) | [Density and Contour Plots](04.04-Density-and-Contour-Plots.ipynb) >"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Visualizing Errors"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"For any scientific measurement, accurate accounting for errors is nearly as important, if not more important, than accurate reporting of the number itself.\n",
"For example, imagine that I am using some astrophysical observations to estimate the Hubble Constant, the local measurement of the expansion rate of the Universe.\n",
"I know that the current literature suggests a value of around 71 (km/s)/Mpc, and I measure a value of 74 (km/s)/Mpc with my method. Are the values consistent? The only correct answer, given this information, is this: there is no way to know.\n",
"\n",
"Suppose I augment this information with reported uncertainties: the current literature suggests a value of around 71 $\\pm$ 2.5 (km/s)/Mpc, and my method has measured a value of 74 $\\pm$ 5 (km/s)/Mpc. Now are the values consistent? That is a question that can be quantitatively answered.\n",
"\n",
"In visualization of data and results, showing these errors effectively can make a plot convey much more complete information."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Basic Errorbars\n",
"\n",
"A basic errorbar can be created with a single Matplotlib function call:"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"%matplotlib inline\n",
"import matplotlib.pyplot as plt\n",
"plt.style.use('seaborn-whitegrid')\n",
"import numpy as np"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAXIAAAD/CAYAAADsfV27AAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAGjhJREFUeJzt3X1sVWcBx/HfraUMaQtIMmMbna9lYU2uWclCa4qViMIM\nUULV6jqIMVG3mJDRSeeYG8SQuiXbjAlEGDoJqGx2LINEnRDsQNqIbaQJEGBRErd7m8WheG+JBmqP\nf8y+33Nfzj1vzznfzz/reu859+nDub/73OftJCzLsgQAMFZF0AUAAJSHIAcAwxHkAGA4ghwADEeQ\nA4DhCHIAMFyl0wPHx8f12GOP6erVq6qoqNDOnTv10Y9+1M2yAQCK4LhFfvLkSSUSCf3yl7/Uli1b\n9Mwzz7hZLgBAkRy3yD/96U9r9erVkqRUKqVFixa5VigAQPEcB7kkVVRU6JFHHtGJEyf0ox/9yK0y\nAQBKkHBjif61a9f0xS9+Ub/+9a912223uVEuAECRHLfIX3nlFb311lv6xje+ofnz56uiokIVFTO7\n3IeGhsouIADEUVNTU9HPdRzkn/nMZ/Td735XnZ2dGhsb0/bt21VVVVVWYaIsnU6rrq4u6GKEAnUx\nhbqYQl1MKbUR7DjIFyxYoB/+8IdODwcAuIQFQQBgOIIcAAxHkAOA4QhyADAcQQ4AhiPIAcBwBDkA\nGI4gBwDDEeQAYLiydj8EgDjq6+tTX1/f5M9tbW2SpLa2tsmf/RTZIA9bRQOIjuk5kkgkJrMmKJEN\n8rBVNAB4JbJBbgK+NQBwA0EeIL41AHADs1YAwHAEOQAYjiAHAMMR5ABgOIIcAAxHkAOA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wHUMPSpxnn8Iqrcvq6ivFFVmLCyM0Dl\nBF4Y3iBRGSjCFLevK6bl+oO9VgwV1z0l4C0vrqsozoEPG4LcYLxB4AUTris2qJuJIAdglLDsBhkm\nBDkAo5iwna7fCHIARil08wgTuN01RJADMIrpA/1edA0ZPf2wmHnYAOYyfTM2EwZk7XixBsToICew\nvcein+hhM7ZgebEGxOgg95LpLRa3sOgnetxuEfJhXxovFkkR5DnQYkEhJn/Qu90i5MO+dG53DRHk\nOYRhHxOEl+kf9HFcNh/18TSCPIcw7GOC8IrCB73Jg4V2CoV1FALbDkGeQxxbLCgeH/ThFPWwzod5\n5DbsWixOJ/JP3CWF5cTmM30esynYT6V4tMhL4LRvNJvNasOGDSUfF1dBDyQWMwsjil0T+fjdx2z6\nOITfCPIS2PWNFrrIz58/rytXrsw5DnOF4Q0cplkYYRmk8/v1ojAO4atyb1eUj5+3enPC7jZqdq/n\n9PZqmUzGWr58uePbsvlZJ36UI9+t3oq5NZifvK77Um715oTT8jstRznv6+l14fYt5woplAVeyFf+\nUm/1Zkwfudv9ZU72O3DaN1pTU6OXX3655OPiKAobIqGwvr4+7dixQzt27Jj8pvH0009Pfvvwcxwi\nEtvilvupko9bLXIvbjRcyk1hi/19PoVuvpxPvuM8/if0pByFbr4cROvIjtd1T4t8ips3Xy5FUN8C\n85Xf6Ba5Xavbi/2HafmFV9wGEr3CrI/iRCELAhnszDUrId8glxfzdpkrjrDwYkAzDIPGprDLApP2\nkEn8v4nviaGhITU1Nc343cQFNjw8rGQyOVlxAwMDWrVqlcbGxjRv3jydOnVqxih1NptVbW2tMpmM\nqxdkIpGQXRXYPZbvGDvpdFr19fVFH1fsReSkLE45qatc0um06urqHL+Wn9z6m+0UUxdOFHo/Sd78\nbeW8Z3LVhdf1H9RrFTpnruzMx/cWud20okKt7rh93Xb7U9/L1kXQ874xF6tPzTP9Pbp+/fqSjnXU\nIrcsSzt27NDly5dVVVW
"text/plain": [
"<matplotlib.figure.Figure at 0x10bf54080>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"x = np.linspace(0, 10, 50)\n",
"dy = 0.8\n",
"y = np.sin(x) + dy * np.random.randn(50)\n",
"\n",
"plt.errorbar(x, y, yerr=dy, fmt='.k');"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Here the ``fmt`` is a format code controlling the appearance of lines and points, and has the same syntax as the shorthand used in ``plt.plot``, outlined in [Simple Line Plots](04.01-Simple-Line-Plots.ipynb) and [Simple Scatter Plots](04.02-Simple-Scatter-Plots.ipynb).\n",
"\n",
"In addition to these basic options, the ``errorbar`` function has many options to fine-tune the outputs.\n",
"Using these additional options you can easily customize the aesthetics of your errorbar plot.\n",
"I often find it helpful, especially in crowded plots, to make the errorbars lighter than the points themselves:"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAXIAAAD/CAYAAADsfV27AAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAGjxJREFUeJzt3X9slWf9//FXC2tLoYOOsMYuUguEGSWZAbYsGswgVqGf\nLLqsmurKiCxRt5DgxmznWDexIRWSjsVQIhtTCTVWrSTjj2YLZGQ6wiI2jqVLJAondOM0TUdVTkGE\n0vP9g28PPeXc58d97l/XfT8fSZNyn3Pu++rFdb/P+75+3HdJMplMCgBgrFK/CwAAKA6BHAAMRyAH\nAMMRyAHAcARyADAcgRwADDfb7gcnJyf1wgsvKBaLqbS0VDt27NCyZcucLBsAIA+2M/K3335bJSUl\n+u1vf6utW7fq5ZdfdrJcAIA82c7Iv/KVr2jdunWSpAsXLmj+/PmOFQoAkD/bgVySSktL9dxzz+nY\nsWP6+c9/7lSZAAAFKHFiif7Fixf1zW9+U/39/aqoqHCiXACAPNnOyN944w2NjIzoe9/7nsrLy1Va\nWqrS0vQu94GBgaILCABRtGrVqrzfazuQf/WrX9WPf/xjtbS0aGJiQtu3b1dZWV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P6yU9+ojNnzqisrEw7d+7U\npz/96Zyfs2pgDz74oAYHBy2z7qBVdFDYydadzC64hA+mYrsOnb4KDPpVZdBMP0fzZSuQHzt2TNeu\nXVNvb69Onz6tzs5O7du3L+fnrBrYnj17JMmIro4g8fsyj0v4wnnR31rsVazT5fK7nUaBrUA+MDCg\nNWvWSJLuu+8+DQ4O5vW
"text/plain": [
"<matplotlib.figure.Figure at 0x10bec4d30>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"plt.errorbar(x, y, yerr=dy, fmt='o', color='black',\n",
" ecolor='lightgray', elinewidth=3, capsize=0);"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"In addition to these options, you can also specify horizontal errorbars (``xerr``), one-sided errorbars, and many other variants.\n",
"For more information on the options available, refer to the docstring of ``plt.errorbar``."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Continuous Errors\n",
"\n",
"In some situations it is desirable to show errorbars on continuous quantities.\n",
"Though Matplotlib does not have a built-in convenience routine for this type of application, it's relatively easy to combine primitives like ``plt.plot`` and ``plt.fill_between`` for a useful result.\n",
"\n",
"Here we'll perform a simple *Gaussian process regression*, using the Scikit-Learn API (see [Introducing Scikit-Learn](05.02-Introducing-Scikit-Learn.ipynb) for details).\n",
"This is a method of fitting a very flexible non-parametric function to data with a continuous measure of the uncertainty.\n",
"We won't delve into the details of Gaussian process regression at this point, but will focus instead on how you might visualize such a continuous error measurement:"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"collapsed": false
},
"outputs": [],
"source": [
"from sklearn.gaussian_process import GaussianProcess\n",
"\n",
"# define the model and draw some data\n",
"model = lambda x: x * np.sin(x)\n",
"xdata = np.array([1, 3, 5, 6, 8])\n",
"ydata = model(xdata)\n",
"\n",
"# Compute the Gaussian process fit\n",
"gp = GaussianProcess(corr='cubic', theta0=1e-2, thetaL=1e-4, thetaU=1E-1,\n",
" random_start=100)\n",
"gp.fit(xdata[:, np.newaxis], ydata)\n",
"\n",
"xfit = np.linspace(0, 10, 1000)\n",
"yfit, MSE = gp.predict(xfit[:, np.newaxis], eval_MSE=True)\n",
"dyfit = 2 * np.sqrt(MSE) # 2*sigma ~ 95% confidence region"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We now have ``xfit``, ``yfit``, and ``dyfit``, which sample the continuous fit to our data.\n",
"We could pass these to the ``plt.errorbar`` function as above, but we don't really want to plot 1,000 points with 1,000 errorbars.\n",
"Instead, we can use the ``plt.fill_between`` function with a light color to visualize this continuous error:"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"collapsed": false
},
"outputs": [
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAXcAAAD/CAYAAAAKVJb/AAAABHNCSVQICAgIfAhkiAAAAAlwSFlz\nAAALEgAACxIB0t1+/AAAIABJREFUeJzt3Xt0lPWdP/D33G/JJJPJdRIIEAg3A0iAeoPqKShIL4LU\nn1q0HtlqbXVddS0qYOSibN2erruKla67awuuHI+2K56228oCQS4aCBAFCShGApnJZGYyl2fuz+33\nBz5PEsl1Mslc8nmdkyMJzzzz5evkPd/5XhWiKIoghBCSVZSpLgAhhJDko3AnhJAsROFOCCFZiMKd\nEEKyEIU7IYRkIQp3QgjJQupEHygIAtavX4+WlhYolUps3LgRLMviwQcfxIQJEwAAd911F5YtW5as\nshJCCBkkRaLz3Pfs2YN9+/bh+eefR0NDA9544w3cdNNNCIVCuO+++5JcTEIIIUORcLgDl1vvSqUS\nf/zjH9HQ0ACdToeWlhbwPI/KykqsW7cORqMxmeUlhBAyCMMKdwB46qmnsGfPHvzbv/0bnE4npk6d\nihkzZuC1116D3+/H2rVrk1VWQgghgzTscAcAj8eDH/7wh9i1axeKi4sBAOfPn8eWLVvwX//1X8Mu\nJCGEkKFJeED1vffeg9PpxAMPPACdTgeFQoFHHnkE69atw6xZs3DkyBHMnDnzisc1NjYOq8CEEDJW\n1dbWDvrahFvukUgETz/9NNxuNziOwwMPPICysjJs2rQJGo0G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pUPux5NoiiiubkZx44dw9SpU7Fo0aKk9vUn3HKvrKzE\ntm3b5O9Pnz6N/fv3Y/X
"text/plain": [
"<matplotlib.figure.Figure at 0x10e55bba8>"
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"# Visualize the result\n",
"plt.plot(xdata, ydata, 'or')\n",
"plt.plot(xfit, yfit, '-', color='gray')\n",
"\n",
"plt.fill_between(xfit, yfit - dyfit, yfit + dyfit,\n",
" color='gray', alpha=0.2)\n",
"plt.xlim(0, 10);"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Note what we've done here with the ``fill_between`` function: we pass an x value, then the lower y-bound, then the upper y-bound, and the result is that the area between these regions is filled.\n",
"\n",
"The resulting figure gives a very intuitive view into what the Gaussian process regression algorithm is doing: in regions near a measured data point, the model is strongly constrained and this is reflected in the small model errors.\n",
"In regions far from a measured data point, the model is not strongly constrained, and the model errors increase.\n",
"\n",
"For more information on the options available in ``plt.fill_between()`` (and the closely related ``plt.fill()`` function), see the function docstring or the Matplotlib documentation.\n",
"\n",
"Finally, if this seems a bit too low level for your taste, refer to [Visualization With Seaborn](04.14-Visualization-With-Seaborn.ipynb), where we discuss the Seaborn package, which has a more streamlined API for visualizing this type of continuous errorbar."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<!--NAVIGATION-->\n",
"< [Simple Scatter Plots](04.02-Simple-Scatter-Plots.ipynb) | [Contents](Index.ipynb) | [Density and Contour Plots](04.04-Density-and-Contour-Plots.ipynb) >"
]
}
],
"metadata": {
"anaconda-cloud": {},
"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",
"version": "3.4.3"
}
},
"nbformat": 4,
"nbformat_minor": 0
}