Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python plotting error bars with different values above and below the point

Warning: I'm very new to using python.

I'm trying to graph data using error bars but my data has different values for the error above and below the bar, i.e. 2+.75,2-.32.

import numpy as np
import matplotlib.pyplot as plt

# example data
x = (1,2,3,4)
y = (1,2,3,4)

# example variable error bar values
yerr = 0.2

plt.figure()
plt.errorbar(x, y, yerr,"r^")
plt.show()

But I want the error bar above the point to be a specific value like .17 and below the point to be a specific point like .3 Does anyone know how to do this?

Thanks!

like image 316
6 revs, 3 users 89% Avatar asked Jun 26 '15 20:06

6 revs, 3 users 89%


People also ask

How do you plot multiple error bars in Python?

errorbar() method we plot the error bars and pass the argument yerr to plot error on the y values in the scatter plot. After this defines the data point on the x-axis and y-axis. Then we define the error value and use the plt. scatter() method to plot a scatter plot and use plt.

Do error bars have to be symmetrical?

Why do error bars on bar graphs sometimes appear to be asymmetrical? One reason for asymmetrical error bars is because they actually are asymmetrical. If you enter separate up and down error values, they may not be the same. If you plot median with quartiles, the error bars are likely to be asymmetrical.


1 Answers

Like this:

plt.errorbar(x, y, np.array([[0.3]*len(x), [0.17]*len(x)]), fmt='r^')

Pass an array of shape (2,n) with the -errors on the first row and the +errors on the second.

enter image description here

(Note also that you need to explicitly pass your format string r^ to the fmt argument).

If you want different error bars on each point, you can pass them in this (2,n) array. You typically have a list of -err and +err value pairs for each data point in order, in which case you can build the necessary array as the transpose of this list:

yerr = np.array([(0.5,0.5), (0.3,0.17), (0.1, 0.3), (0.1,0.3)]).T
plt.errorbar(x, y, yerr, fmt='r^')
plt.show()

enter image description here

This is described (more-or-less) in the docs.

like image 154
xnx Avatar answered Sep 17 '22 00:09

xnx