我想绘制一些数据x
和y
,其中我需要标记大小取决于第三数组z
。我可以分别绘制它们(即,将分散的x
和y
与size = z
一起绘制,而错误栏没有标记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 argument
markersize does not accept arrays as
scatter`中起作用。
答案 0 :(得分:1)
通常的想法是使用代理服务器来添加图例。因此,尽管图中的错误栏可能没有标记,但图例中的错误栏却设置了标记。
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()