Python在该点的上方和下方绘制具有不同值的误差线 [英] Python plotting error bars with different values above and below the point

查看:598
本文介绍了Python在该点的上方和下方绘制具有不同值的误差线的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

警告:我对使用python很陌生.

Warning: I'm very new to using python.

我正在尝试使用误差线来绘制数据图形,但是我的数据对于误差线上方和下方的误差具有不同的值,即2 + .75,2-.32.

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()

但是我希望该点上方的误差条为.17之类的特定值,而该点下方的误差条为.3之类的特定点 有人知道该怎么做吗?

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?

谢谢!

推荐答案

像这样:

plt.errorbar(x, y, np.array([[0.3]*len(x), [0.17]*len(x)]), fmt='r^')

传递形状为(2,n)的数组,第一行带有-errors,第二行带有+ errors.

Pass an array of shape (2,n) with the -errors on the first row and the +errors on the second.

(另请注意,您需要将格式字符串r^显式传递给fmt参数).

(Note also that you need to explicitly pass your format string r^ to the fmt argument).

如果每个点都需要不同的误差线,则可以在此(2,n)数组中传递它们.通常,每个数据点都有一个-err和+ err值对的列表,在这种情况下,您可以构建必要的数组作为此列表的转置:

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()

(或多或少)在文档.

这篇关于Python在该点的上方和下方绘制具有不同值的误差线的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆