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!
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.
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.
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.
(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()
This is described (more-or-less) in the docs.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With