我想绘制一些数据x和y,其中我需要标记大小依赖于第三个数组z。我可以分别绘制它们(例如,使用size = z的散布x和y,以及不带标记的errorbar,fmc = 'none'),这样就解决了问题。问题是我需要图例一起显示错误条和点:

而不是

下面的代码包含一些虚构的数据:
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(1,10,100)
y = 2*x
yerr = np.random(0.5,1.0,100)
z = np.random(1,10,100)
fig, ax = plt.subplots()
plt.scatter(x, y, s=z, facecolors='', edgecolors='red', label='Scatter')
ax.errorbar(x, y, yerr=yerr, xerr=0, fmt='none', mfc='o', color='red', capthick=1, label='Error bar')
plt.legend()
plt.show()这会产生我想要避免的图例:

在errorbar the argumentmarkersizedoes not accept arrays asscatter`中是这样。
发布于 2019-09-25 23:09:03
通常的想法是使用代理将其放入图例中。因此,虽然绘图中的错误栏可能没有标记,但图例中的错误栏有一个标记集。
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(1,10,11)
y = 2*x
yerr = np.random.rand(11)*5
z = np.random.rand(11)*2+5
fig, ax = plt.subplots()
sc = ax.scatter(x, y, s=z**2, facecolors='', edgecolors='red')
errb = ax.errorbar(x, y, yerr=yerr, xerr=0, fmt='none',
color='red', capthick=1, label="errorbar")
proxy = ax.errorbar([], [], yerr=[], xerr=[], marker='o', mfc="none", mec="red",
color='red', capthick=1, label="errorbar")
ax.legend(handles=[proxy], labels=["errorbar"])
plt.show()https://stackoverflow.com/questions/58100735
复制相似问题