Discussion:
[Tutor] Plotting asymmetric error bars for a single point in matplotlib
Colin Ross
2015-08-04 14:43:30 UTC
Permalink
Hi all,

Goal: To plot asymmetric x error bars for a single point using errorbar. I
am interested in displaying the inter quartile range (IQR) for a data set.

Code:

import numpy as np
import matplotlib.pyplot as plt

y = 1.0
data = np.random.rand(100)

median = np.median(data)
upper_quartile = np.percentile(data, 75)
lower_quartile = np.percentile(data, 25)
IQR = upper_quartile - lower_quartile

plt.errorbar(median, y, xerr=[lower_quartile ,upper_quartile], fmt='k--')

plt.savefig('IQR.eps')
plt.show()

Error:

Traceback (most recent call last):
File "IQR.py", line 15, in <module>
plt.errorbar(median, y, xerr=[0.5,0.75], fmt='k--')
File "/usr/lib/pymodules/python2.7/matplotlib/pyplot.py", line 2251, in
errorbar
ret = ax.errorbar(x, y, yerr, xerr, fmt, ecolor, elinewidth, capsize,
barsabove, lolims, uplims, xlolims, xuplims, **kwargs)
File "/usr/lib/pymodules/python2.7/matplotlib/axes.py", line 5327, in
errorbar
in cbook.safezip(x,xerr)]
File "/usr/lib/pymodules/python2.7/matplotlib/cbook.py", line 1294, in
safezip
raise ValueError(_safezip_msg % (Nx, i+1, len(arg)))
ValueError: In safezip, len(args[0])=1 but len(args[1])=2

My understanding is that safezip zips together the x array with the
specified upper and lower x limits?

Thanks.

Colin
_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Peter Otten
2015-08-05 12:35:57 UTC
Permalink
Post by Colin Ross
Hi all,
Goal: To plot asymmetric x error bars for a single point using errorbar. I
am interested in displaying the inter quartile range (IQR) for a data set.
import numpy as np
import matplotlib.pyplot as plt
y = 1.0
data = np.random.rand(100)
median = np.median(data)
upper_quartile = np.percentile(data, 75)
lower_quartile = np.percentile(data, 25)
IQR = upper_quartile - lower_quartile
plt.errorbar(median, y, xerr=[lower_quartile ,upper_quartile], fmt='k--')
plt.savefig('IQR.eps')
plt.show()
File "IQR.py", line 15, in <module>
plt.errorbar(median, y, xerr=[0.5,0.75], fmt='k--')
File "/usr/lib/pymodules/python2.7/matplotlib/pyplot.py", line 2251, in
errorbar
ret = ax.errorbar(x, y, yerr, xerr, fmt, ecolor, elinewidth, capsize,
barsabove, lolims, uplims, xlolims, xuplims, **kwargs)
File "/usr/lib/pymodules/python2.7/matplotlib/axes.py", line 5327, in
errorbar
in cbook.safezip(x,xerr)]
File "/usr/lib/pymodules/python2.7/matplotlib/cbook.py", line 1294, in
safezip
raise ValueError(_safezip_msg % (Nx, i+1, len(arg)))
ValueError: In safezip, len(args[0])=1 but len(args[1])=2
My understanding is that safezip zips together the x array with the
specified upper and lower x limits?
If you have not solved this yet: my guess is that

xerr=[lower_quartile, upper_quartile]

is interpreted as two error values that would correspond to x and y vectors
of length 2 with symmetrical error bars. Try passing a list of two length-
one lists two avoid the ambiguity:

plt.errorbar(
median, y,
xerr=[[lower_quartile], [upper_quartile]], fmt='k--')


_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor

Loading...