Python 如何在 rcParams 中使用 linestyle=None 在 matplotlib 中制作误差条图?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18498742/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 10:54:08  来源:igfitidea点击:

How do you make an errorbar plot in matplotlib using linestyle=None in rcParams?

pythonmatplotlib

提问by drs

When plotting errorbar plots, matplotlib is not following the rcParams of no linestyle. Instead, it's plotting all of the points connected with a line. Here's a minimum working example:

绘制误差条图时,matplotlib 不遵循 no linestyle 的 rcParams。相反,它绘制了所有与一条线相连的点。这是一个最低限度的工作示例:

import matplotlib.pyplot as plt

lines = {'linestyle': 'None'}
plt.rc('lines', **lines)

plt.errorbar((0, 1), (1, 0), yerr=(0.1, 0.1), marker='o')

plt.savefig('test.pdf')
plt.delaxes()

enter image description here

在此处输入图片说明

Is the only solution to explicitly set linestyle='None'when calling pyplot.errorbar()?

linestyle='None'在调用时显式设置的唯一解决方案pyplot.errorbar()吗?

采纳答案by tacaswell

This is a "bug" in older versions of matplotlib(and has been fixedfor the 1.4 series). The issue is that in Axes.errorbarthere is a default value of '-'for fmt, which is then passed to the call to plotwhich is used to draw the markers and line. Because a format string is passed into plotin never looks at the default value in rcparams.

这是旧版本中的“错误” matplotlib(并且已为 1.4 系列修复)。问题是Axes.errorbar有一个默认值'-'for fmt,然后将其传递给plot用于绘制标记和线条的调用。因为传入的格式字符串plot从不查看rcparams.

You can also pass in fmt = ''

你也可以传入 fmt = ''

eb = plt.errorbar(x, y, yerr=.1, fmt='', color='b')

which will cause the rcParam['lines.linestlye']value to be respected. I have submitted a PRto implement this.

这将导致rcParam['lines.linestlye']价值得到尊重。我已经提交了一个PR来实现这个。

Another work around for this is to make the errorbar in two steps:

另一个解决方法是分两步制作错误栏:

l0, = plt.plot(x,y, marker='o', color='b')
eb = plt.errorbar(x, y, yerr=.1, fmt=None, color='b')

This is an annoying design decision, but changing it would be a major api break. Please open an issue on github about this.

这是一个烦人的设计决定,但改变它会是一个主要的 api 中断。请在 github 上打开一个关于此的问题。

errorbar doc.

错误栏文档

As a side note, it looks like the call signature was last changed in 2007, and that was to make errorbars not default to blue.

作为旁注,看起来调用签名上次更改是在 2007 年,这是为了使错误栏不默认为蓝色。