0

Say I have measurements with x and y values

x = [1, 2, 3, 4]
y = [10, 9, 8, 7]

and the measurements have upper and lower limits on the y values (i.e., where the errorbars should end)

y_lower = [6, 5, 3, 6.5]
y_upper = [12, 10.5, 9, 7]

Can I use plt.errorbar() to plot errorbars from y_lower to y_upper for each of these four points? In other words, I would like to use plt.errorbar() with endpoints for the errorbars, not their sizes.

Nikko Cleri
  • 185
  • 1
  • 1
  • 11

1 Answers1

1

Gig 'em!

IIUC, do you want this?

import matplotlib.pyplot as plt

x = [1, 2, 3, 4]
y = [10, 9, 8, 7]

y_lower = [6, 5, 3, 6.5]
y_upper = [12, 10.5, 9, 7]

errors = [y_lower, y_upper]

plt.errorbar(x, y, yerr=errors, fmt='o', ecolor = 'red')
plt.show()

Output:

enter image description here


Update per comment below:

import matplotlib.pyplot as plt
import numpy as np

x = [1, 2, 3, 4]
y = [10, 9, 8, 7]

y_lower = [6, 5, 3, 6.5]
y_upper = [12, 10.5, 9, 7]
y_l = np.array(y) - np.array(y_lower)
y_u =np.array(y_upper) - np.array(y)

errors = [y_l, y_u]

plt.errorbar(x, y, yerr=errors, fmt='o', ecolor = 'red')
ax = plt.gca()
ax.set_ylim(0,15)
plt.show()

Output:

enter image description here

Scott Boston
  • 147,308
  • 15
  • 139
  • 187
  • Not quite: I am looking for errorbars which have endpoints at `y_lower` and `y_upper`, not errorbars with upper and lower lengths of those two lists. – Nikko Cleri Jun 29 '22 at 20:38
  • Oh.. then I think we just have to do a little math on the error array. – Scott Boston Jun 29 '22 at 20:39
  • @NikkoCleri See update.... – Scott Boston Jun 29 '22 at 20:42
  • 1
    I know in a simple case like this I can just subtract to get the length, but in my real data I have to do propagations and move between linear and log space, so it gets much more clunky. – Nikko Cleri Jun 29 '22 at 20:44
  • I was hoping there was some cleaner built-in way of doing this, like some kwarg within plt.errorbar – Nikko Cleri Jun 29 '22 at 20:45
  • Hrm... per [docs](https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.errorbar.html) yerr and xerr are for sizes.... I guess you could attempt to create a function yourself to manually draw vertical lines using [vline](https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.vlines.html) where you can supply X, ymin and xmin in matplotlib. – Scott Boston Jun 29 '22 at 20:51