Matplotlib误差线缺少端标记

30

我正在尝试使用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()
问题在于生成的图表中没有任何标题!对我来说,我使用的是Ubuntu 13.04、Python 2.7.5 |Anaconda 1.6.1 (64-bit)|以及Matplotlib 1.2.1。这可能是需要覆盖的隐藏rcparam参数吗?enter image description here

1
你有改变任何东西使你的背景变成灰色吗?我在想你是否遇到了zorder问题(相关链接:https://dev59.com/i2Yr5IYBdhLWcg3wB16h#14007175)。该问题的补丁已经在1.3版本中修复,但在1.2.1版本中没有。 - tacaswell
3
你的代码在我的系统上可以正确运行,我怀疑在调整图形外观(是的,我知道这是主观的)时,你可能破坏了某些内容。我们需要查看你的rcparams以进行调试。 - tacaswell
是的,实际上我正在使用一个matplotlibrc文件,它与默认设置有很大不同。你需要查看文件的哪个部分? - astromax
1
如果您使用默认的 matplotlibrc 文件(在启动 Python 之前重命名您自己的文件),那么这是否按预期工作? - tacaswell
4个回答

25

不是特别清楚。我尝试理解它,但是语法似乎让我感到困惑。我知道(,caps,)会产生一个元组的元组,然而,set_color()和set_markeredgewidth()我不熟悉。 - astromax
它们的功能就像它们的名字一样,设置标记的颜色和边缘宽度。http://matplotlib.org/api/artist_api.html#matplotlib.lines.Line2D.set_color http://matplotlib.org/api/artist_api.html#matplotlib.lines.Line2D.set_markeredgewidth - tacaswell
我更多地是在问你是否找出了你在matplotlibrc文件中改变了什么导致它破裂的问题。 - tacaswell
对我来说,主要的问题是由于某种无法解释的原因capsize出了问题。你应该能够在errorbar调用中使用capthickecolor参数,而不是set_markeredgewidthset_color - mxmlnkn

16

这与matplotlib中的rcParams有关。要解决这个问题,在您的脚本开头添加以下行:

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

它也可以与plt.bar()一起使用。


16

将astromax的回答稍作简化:

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

似乎有时 markeredgewidth 默认为 0。


0
关于markeredgewidth的答案是有效的。现在有一个更具描述性的新参数capthick,用于表示帽子厚度。来自matplotlib版本3.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.

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接