Python plotting error bars with different values above and below the point

柔情痞子 提交于 2019-12-18 22:36:07

问题


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:


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.



来源:https://stackoverflow.com/questions/31081568/python-plotting-error-bars-with-different-values-above-and-below-the-point

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!