如何在 rcParams 中使用 linestyle=None 在 matplotlib 中制作误差条图? [英] How do you make an errorbar plot in matplotlib using linestyle=None in rcParams?
问题描述
在绘制误差条图时,matplotlib 没有遵循 no linestyle 的 rcParams.而是绘制与一条线连接的所有点.这是一个最低限度的工作示例:
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:
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()
是否是在调用 pyplot.errorbar()
时显式设置 linestyle='None'
的唯一解决方案?
Is the only solution to explicitly set linestyle='None'
when calling pyplot.errorbar()
?
推荐答案
This is a "bug" in older versions of matplotlib
(and has been fixed for the 1.4 series). The issue is that in Axes.errorbar
there is a default value of '-'
for fmt
, which is then passed to the call to plot
which is used to draw the markers and line. Because a format string is passed into plot
in never looks at the default value in rcparams
.
你也可以传入fmt = ''
eb = plt.errorbar(x, y, yerr=.1, fmt='', color='b')
这将导致遵守 rcParam ['lines.linestlye']
值.我已经提交了一个PR来实现这个.
which will cause the rcParam['lines.linestlye']
value to be respected. I have submitted a PRto implement this.
另一种解决方法是分两步制作误差条:
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')
这是一个烦人的设计决定,但改变它会是一个重大的 API 中断.请在 github 上打开一个关于此的问题.
This is an annoying design decision, but changing it would be a major api break. Please open an issue on github about this.
errorbar 文档.
errorbar doc.
作为旁注,呼叫签名似乎在2007年进行了最后一次更改,这是为了使错误栏不默认为蓝色.
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.
这篇关于如何在 rcParams 中使用 linestyle=None 在 matplotlib 中制作误差条图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!