Matplotlib错误栏帽丢失

pxyaymoc  于 12个月前  发布在  其他
关注(0)|答案(4)|浏览(98)

我试图在matplotlib中创建一个带有错误条的散点图。下面是我的代码的示例:

import matplotlib.pyplot as plt
import numpy as np
import random

x = np.linspace(1,2,10)
y = np.linspace(2,3,10)
err = [random.uniform(0,1) for i in range(10)]

plt.errorbar(x, y,
       yerr=err,
       marker='o',
       color='k',
       ecolor='k',
       markerfacecolor='g',
       label="series 2",
       capsize=5,
       linestyle='None')
plt.show()

问题是输出的图根本不包含上限!x1c 0d1x
不管怎样,我用的是Ubuntu 13.04,Python 2.7.5| Anaconda 1.6.1(64-bit)|和Matplotlib 1.2.1。
这可能是一个需要覆盖的隐藏rcparam吗?

yrdbyhpb

yrdbyhpb1#

对我有用的是添加这个(按照:How to set the line width of error bar caps, in matplotlib):

(_, caps, _) = plt.errorbar(x,y, yerr=err, capsize=20, elinewidth=3)

for cap in caps:
    cap.set_color('red')
    cap.set_markeredgewidth(10)
x4shl7ld

x4shl7ld2#

对astromax的回答稍作简化:

plt.errorbar(x,y, yerr=err, capsize=20, elinewidth=3, markeredgewidth=10)

似乎markedgewidth有时默认为0。

vwoqyblh

vwoqyblh3#

它与matplotlib中的rcParams有关。要解决它,请在脚本的开头添加以下行:

import matplotlib
matplotlib.rcParams.update({'errorbar.capsize': 2})

它也适用于plt.bar()

gz5pxeao

gz5pxeao4#

关于markeredgewidth的答案是有效的。现在有一个新的参数,它对capthick的 * 帽厚度 * 有更好的描述。来自matplotlib3.7.2版本的文档。

capthick : float, default: None
    An alias to the keyword argument *markeredgewidth* (a.k.a. *mew*).
    This setting is a more sensible name for the property that
    controls the thickness of the error bar cap in points. For
    backwards compatibility, if *mew* or *markeredgewidth* are given,
    then they will over-ride *capthick*. This may change in future
    releases.

相关问题