Python Plot Error Bars
Contents |
code) (png, hires.png, pdf) (png, hires.png, pdf) #!/usr/bin/env python import asymmetric error bars python numpy as np import matplotlib.pyplot as plt # example
Matplotlib Error Bars Scatter Plot
data x = np.arange(0.1, 4, 0.5) y = np.exp(-x) # example variable
Matplotlib Errorbar No Line
error bar values yerr = 0.1 + 0.2*np.sqrt(x) xerr = 0.1 + yerr # First illustrate basic pyplot interface, using defaults
Plt.errorbar No Line
where possible. plt.figure() plt.errorbar(x, y, xerr=0.2, yerr=0.4) plt.title("Simplest errorbars, 0.2 in x, 0.4 in y") # Now switch to a more OO interface to exercise more features. fig, axs = plt.subplots(nrows=2, ncols=2, sharex=True) ax = axs[0,0] ax.errorbar(x, y, yerr=yerr, fmt='o') ax.set_title('Vert. symmetric') # matplotlib errorbar asymmetric With 4 subplots, reduce the number of axis ticks to avoid crowding. ax.locator_params(nbins=4) ax = axs[0,1] ax.errorbar(x, y, xerr=xerr, fmt='o') ax.set_title('Hor. symmetric') ax = axs[1,0] ax.errorbar(x, y, yerr=[yerr, 2*yerr], xerr=[xerr, 2*xerr], fmt='--o') ax.set_title('H, V asymmetric') ax = axs[1,1] ax.set_yscale('log') # Here we have to be careful to keep all y values positive: ylower = np.maximum(1e-2, y - yerr) yerr_lower = y - ylower ax.errorbar(x, y, yerr=[yerr_lower, 2*yerr], xerr=xerr, fmt='o', ecolor='g', capthick=2) ax.set_title('Mixed sym., log y') fig.suptitle('Variable errorbars') plt.show() Keywords: python, matplotlib, pylab, example, codex (see Search examples) Navigation index modules | home| downloads| search| examples| gallery| citation| docs » © Copyright 2013 John Hunter, Darren Dale, Eric Firing, Michael Droettboom and the matplotlib development team. Last updated on May 29, 2013. Created using Sphinx 1.2b1.
Travis-CI: Related Topics Documentation overview Matplotlib Examples statistics Examples Previous: statistics example code: errorbar_demo.py Next: seaborn error bars statistics example code: errorbar_limits.py This Page Show Source Quick search errorbar() got multiple values for keyword argument 'yerr' statistics example code: errorbar_demo_features.py¶ (Source code, png, hires.png, pdf) """ Demo of errorbar function with matplotlib errorbar marker size different ways of specifying error bars. Errors can be specified as a constant value (as shown in `errorbar_demo.py`), or as demonstrated in this example, http://matplotlib.org/1.2.1/examples/pylab_examples/errorbar_demo.html they can be specified by an N x 1 or 2 x N, where N is the number of data points. N x 1: Error varies for each point, but the error values are symmetric (i.e. the lower and upper values are equal). 2 x N: Error varies for each http://matplotlib.org/examples/statistics/errorbar_demo_features.html point, and the lower and upper limits (in that order) are different (asymmetric case) In addition, this example demonstrates how to use log scale with errorbar. """ import numpy as np import matplotlib.pyplot as plt # example data x = np.arange(0.1, 4, 0.5) y = np.exp(-x) # example error bar values that vary with x-position error = 0.1 + 0.2 * x # error bar values w/ different -/+ errors lower_error = 0.4 * error upper_error = error asymmetric_error = [lower_error, upper_error] fig, (ax0, ax1) = plt.subplots(nrows=2, sharex=True) ax0.errorbar(x, y, yerr=error, fmt='-o') ax0.set_title('variable, symmetric error') ax1.errorbar(x, y, xerr=asymmetric_error, fmt='o') ax1.set_title('variable, asymmetric error') ax1.set_yscale('log') plt.show() Keywords: python, matplotlib, pylab, example, codex (see Search examples) © Copyright 2002 - 2012 John Hunter, Darren Dale, Eric Firing, Michael Droettboom and the matplotlib development team; 2012 - 2014 The matplotlib development team. Last updated on Sep 20, 2016. Created using Sphinx 1.4.6.
here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies http://stackoverflow.com/questions/22364565/python-pylab-scatter-plot-error-bars-the-error-on-each-point-is-unique of this site About Us Learn more about Stack Overflow the company Business Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges https://tonysyu.github.io/plotting-error-bars.html Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 6.2 million programmers, just like you, helping each other. Join them; it only takes a minute: error bars Sign up Python Pylab scatter plot error bars (the error on each point is unique) up vote 4 down vote favorite I am attempting a scatter plot of 2 arrays for which I have a third array containing the absolute error (error in y direction) on each point. I want the error bars to between (point a - error on a) and errorbar no line (point a + error on a). Is there a way of achieving this with pylab and if not any ideas on how else I could do it? Thanks in advance python matplotlib share|improve this question asked Mar 12 '14 at 21:46 user3412782 31116 add a comment| 1 Answer 1 active oldest votes up vote 5 down vote accepted >>> import matplotlib.pyplot as plt >>> a = [1,3,5,7] >>> b = [11,-2,4,19] >>> plt.pyplot.scatter(a,b) >>> plt.scatter(a,b)
you would just call matplotlib's errorbar function: import numpy as np import matplotlib.pyplot as plt x = np.linspace(0, 2 * np.pi) y_sin = np.sin(x) y_cos = np.cos(x) plt.errorbar(x, y_sin, 0.2) plt.errorbar(x, y_cos, 0.2) plt.show() which produces the following plot: The numerous error bars make the plot really noisy, and, in my opinion, they're really distracting. Instead, you could use matplotlib's fillbetween to denote the error as a region in the plot. In the following, I try to imitate errorbar's interface (minus many additional key-word arguments): def errorfill(x, y, yerr, color=None, alpha_fill=0.3, ax=None): ax = ax if ax is not None else plt.gca() if color is None: color = ax._get_lines.color_cycle.next() if np.isscalar(yerr) or len(yerr) == len(y): ymin = y - yerr ymax = y + yerr elif len(yerr) == 2: ymin, ymax = yerr ax.plot(x, y, color=color) ax.fill_between(x, ymax, ymin, color=color, alpha=alpha_fill) Continuing with the data and imports from the first code block, you can use the errorfill function as follows: errorfill(x, y_sin, 0.2) errorfill(x, y_cos, 0.2) plt.show() which gives: With the fill method, you lose information about the direction of the error (especially if you have errors in both x and y), but for most use cases, this works pretty well. Comments Please enable JavaScript to view the comments powered by Disqus. comments powered by Disqus Software developer, engineer, and all-around good guy in Austin, TX, USA. Links github linkedin Tags © 2016 Tony S. Yu · Powered by pelican-bootstrap3, Pelican, Bootstrap Back to top