Can we set a different lower and upper value for the y error bars?

Question

In the context of this exercise, can we set a different lower and upper value for the y error bars?

Answer

No, currently there is no clear or easy way to set different lower and upper values for y error bars on a bar chart in Matplotlib.

Error bars having different lower and upper values are also referred to as “asymmetric error bars”. At this current point in time, Matplotlib only supports “symmetric error bar” values, so applying asymmetric error bars will either not be possible or difficult to apply using some clever trick.

3 Likes

This may be out of date information. Looking at the current documentation for the bar method, it seems an array of shape (2, N) is also supported in the current version.

xerr, yerr scalar or array-like of shape(N,) or shape(2, N), optional

If not None , add horizontal / vertical errorbars to the bar tips. The values are +/- sizes relative to the data:

  • scalar: symmetric +/- values for all bars
  • shape(N,): symmetric +/- values for each bar
  • shape(2, N): Separate - and + values for each bar. First row contains the lower errors, the second row contains the upper errors.
  • None : No errorbar. (Default)
7 Likes

Interesting, thank you. good to know.
The following would achieve this thanks for your hint:
ax= plt.subplot()
error = ([0.1i for i in ounces_of_milk], [0.2i for i in ounces_of_milk])
ax.bar(range(len(drinks)), ounces_of_milk, yerr = error, capsize = 5)
ax.set_xticks(range(len(drinks)))
ax.set_xticklabels(drinks)

7 Likes