python - Matplotlib 在 python 中的非对称误差线绘图
问题描述
尝试绘制范围从负值到正值的不对称误差线时遇到错误。我修改了从文档中获取的示例:
import numpy as np
import matplotlib.pyplot as plt
# example data
x = np.arange(0, 4, 1)
y = -0.2* x
# example error bar values that vary with x-position
error = 0.1 + 0.2 * x
# error bar values w/ different -/+ errors that
# also vary with the x-position
lower_error = -1 * error
upper_error = 4* error
asymmetric_error = [lower_error, upper_error]
plt.errorbar(x, y, yerr=asymmetric_error, fmt='.', ecolor = 'red')
plt.show()
这给出了以下情节:
但具有以下不对称误差值:
array([-0.1, -0.3, -0.5, -0.7]), array([0.4, 1.2, 2. , 2.8])]
这似乎遵循文档,所以我不确定是什么原因造成的。
解决方案
您不需要在较低错误之前使用负数。
import numpy as np
import matplotlib.pyplot as plt
# example data
x = np.arange(0, 4, 1)
y = -0.2* x
# example error bar values that vary with x-position
error = 0.1 + 0.2 * x
# error bar values w/ different -/+ errors that
# also vary with the x-position
lower_error = error
upper_error = 4*error
asymmetric_error = np.array(list(zip(lower_error, upper_error))).T
plt.errorbar(x, y, yerr=asymmetric_error, fmt='.', ecolor = 'red')
plt.show()
输出:
推荐阅读
- node.js - 节点选项:在抛出错误时让它变得平静
- jquery - 如何在 jquery 对话框中创建表?
- python - 如何通过 os.system() 调用 anaconda 环境以从其他 python(3.7) 脚本运行特定的 python(2.7) 包?
- django - 如何将对象从选择控件传递到django中的另一个视图
- javascript - PHP区分javascript和用户点击?
- python - 当使用 spyder 3.3.3 和 ipython 7.4 时,来自 PyQt5 的 app.exec 阻止了代码
- docker - 尽管存在 sudo,但 Packer 使我的 docker 构建失败并出现错误“sudo:未找到”
- swift - 禁用相机视图增长动画
- elasticsearch - ElasticSearch 可以接受预标记的内容并返回相应的 tf-idf 分数吗?
- java - 并行流看起来不像完全并行工作