11

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!

1 Answer 1

12

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for answering! I have one more quick question. If each point has a different error bar value, what would I do? Like if the first point has values +.17 and -.3 but the second point in my data set has +.8 and -.23.
You use an array of shape (2,n) where each of the n pairs of values is the (-err, +err) for a point.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.