我必须绘制两条线,然后测量它们之间的绝对距离。该距离随后成为图例的标签。 伪代码看起来像这样:
for line in file:
# extract data here
sample = sns.kdeplot(data)
perfect_template = sns.kdeplot(perfect_template_data)
sample_line = sample.get_lines()[iterate].get_data()
perfect_line = perfect_template.get_lines()[iterate]get_data()
absolute_diff = sum(abs(np.diff(zip(sample_line, perfect_line))))
label = absolute_diff
sample = sns.kdeplot(data, linewidth=1, label=label)
plt(legend)
pp.savefig()
这需要我绘制 sns.kdeplot
两次。
有没有办法将标签添加到初始 sns.kdeplot
而不重新绘制?
最佳答案
您可以像这样使用set_label()
:
sample.set_label(label)
即举一个最小的例子:
x = np.linspace(0, 2*np.pi)
y = np.sin(x)
y2 = np.sin(2*x)
first_line, = plt.plot(x, y)
plt.plot(x, y2)
first_line.set_label('First line')
plt.legend()
关于python - 为图例后绘图添加标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32526324/