gpt4 book ai didi

python-3.x - 在 Seaborn 绘图上绘图并添加图例

转载 作者:行者123 更新时间:2023-12-04 09:49:36 30 4
gpt4 key购买 nike

请参阅下面的代码。我用 seaborn 创建了一个情节然后我用 plt.plot 在同一图上绘制了一些其他值,工作正常,但 plt.legend方法显然不是可加的,也就是说,我不能用它来标记图表中的两条线,即 'from seaborn''from plt' ,两者都与正确的线条颜色相关联。

import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
mean, cov = [0, 2], [(1, .5), (.5, 1)]
data_sample, y = np.random.multivariate_normal(mean, cov, size=100).T
data_min = min(data_sample)
data_max = max(data_sample)
plot_points = [(data_min + (i/100) * (data_max-data_min)) for i in range(0, 100)]
sns.set(color_codes=True)
plt.figure()
ax = sns.kdeplot(data_sample, legend='from seaborn')
ax.plot(plot_points, y)
plt.legend(['from plt'])
plt.show()

我该如何解决这个问题?

最佳答案

更改 legendlabel并添加 labelplot :

ax = sns.kdeplot(data_sample, label='from seaborn')
ax.plot(plot_points, y, label='from plt')
ax.legend()

输出:

enter image description here

关于python-3.x - 在 Seaborn 绘图上绘图并添加图例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62028117/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com