{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "\n# Different ways of specifying error bars\n\nErrors can be specified as a constant value (as shown in\n:doc:`/gallery/statistics/errorbar`). However, this example demonstrates\nhow they vary by specifying arrays of error values.\n\nIf the raw ``x`` and ``y`` data have length N, there are two options:\n\nArray of shape (N,):\n Error varies for each point, but the error values are\n symmetric (i.e. the lower and upper values are equal).\n\nArray of shape (2, N):\n Error varies for each point, and the lower and upper limits\n (in that order) are different (asymmetric case)\n\nIn addition, this example demonstrates how to use log\nscale with error bars.\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "collapsed": false }, "outputs": [], "source": [ "import matplotlib.pyplot as plt\nimport numpy as np\n\n# example data\nx = np.arange(0.1, 4, 0.5)\ny = np.exp(-x)\n\n# example error bar values that vary with x-position\nerror = 0.1 + 0.2 * x\n\nfig, (ax0, ax1) = plt.subplots(nrows=2, sharex=True)\nax0.errorbar(x, y, yerr=error, fmt='-o')\nax0.set_title('variable, symmetric error')\n\n# error bar values w/ different -/+ errors that\n# also vary with the x-position\nlower_error = 0.4 * error\nupper_error = error\nasymmetric_error = [lower_error, upper_error]\n\nax1.errorbar(x, y, xerr=asymmetric_error, fmt='o')\nax1.set_title('variable, asymmetric error')\nax1.set_yscale('log')\nplt.show()" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ ".. tags:: plot-type: errorbar, domain: statistics\n\n.. admonition:: References\n\n The use of the following functions, methods, classes and modules is shown\n in this example:\n\n - `matplotlib.axes.Axes.errorbar` / `matplotlib.pyplot.errorbar`\n\n" ] } ], "metadata": { "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.13.2" } }, "nbformat": 4, "nbformat_minor": 0 }