本文介绍了在matplotlib中使用yscale('log')时缺少错误栏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在某些情况下,当使用对数刻度时,matplotlib 会错误地显示带有误差条的绘图.假设这些数据(例如在pylab中):

In some cases matplotlib shows plot with errorbars errorneously when using logarithmic scale. Suppose these data (within pylab for example):

s=[19.0, 20.0, 21.0, 22.0, 24.0]
v=[36.5, 66.814250000000001, 130.17750000000001, 498.57466666666664, 19.41]
verr=[0.28999999999999998, 80.075044597909169, 71.322124839818571, 650.11015891565125, 0.02]
errorbar(s,v,yerr=verr)

我得到了一个正常的结果,但是当我切换到对数刻度时:

and I get a normal result but when I switch to logarithmic scale:

yscale('log')

我得到一个图,其中一些误差条不可见,但您仍然可以看到一些误差条的上限.(见下文.)为什么会发生这种情况,我该如何解决?

I get a plot in which some errorbars are not visible, although you can still see some of the error bar caps. (See below.) Why is this happening, and how can I fix it?

推荐答案

问题是对于某些点, v-verr 变为负数,对数轴上无法显示< = 0的值(log(x), x 未定义)要解决此问题,您可以使用不对称错误并强制违规点的结果值高于零.

The problem is that for some points v-verr is becoming negative, values <=0 cannot be shown on a logarithmic axis (log(x), x<=0 is undefined) To get around this you can use asymmetric errors and force the resulting values to be above zero for the offending points.

在误差大于值 verr>=v 的任何点,我们分配 verr=.999v 在这种情况下,误差条将接近于零.

At any point for which errors are bigger than value verr>=v we assign verr=.999v in this case the error bar will go close to zero.

这是脚本

import matplotlib.pyplot as plt
import numpy as np

s=[19.0, 20.0, 21.0, 22.0, 24.0]
v=np.array([36.5, 66.814250000000001, 130.17750000000001, 498.57466666666664, 19.41])
verr=np.array([0.28999999999999998, 80.075044597909169, 71.322124839818571,     650.11015891565125, 0.02])
verr2 = np.array(verr)
verr2[verr>=v] = v[verr>=v]*.999999
plt.errorbar(s,v,yerr=[verr2,verr])
plt.ylim(1E1,1E4)
plt.yscale('log')
plt.show()

这是结果

这篇关于在matplotlib中使用yscale('log')时缺少错误栏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-25 07:05