正确地将图例添加到 seaborn 联合图 [英] Correctly add a legend to a seaborn jointplot
本文介绍了正确地将图例添加到 seaborn 联合图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在 seaborn 联合图中绘制了两个分布,如下所示:
I have two distributions plotting in a seaborn jointplot like so:
graph = sns.jointplot(setosa.sepal_width, setosa.sepal_length,
cmap="Reds", kind="kde", marginal_kws={"color":"r", "alpha":.2}, shade=True, shade_lowest=False, alpha=.5)
graph.x = virginica.sepal_width
graph.y = virginica.sepal_length
graph.plot_joint(sns.kdeplot, cmap="Blues", shade=True, shade_lowest=False, alpha=.5)
ax = plt.gca()
ax.legend(["setosa", "virginica"])
graph.plot_marginals(sns.kdeplot, color='b', shade=True, alpha=.2, legend=False)
注意图例,setosa
和 virginica
左边没有颜色.有没有办法在seaborn的jointplot中正确添加图例?
Notice on the legend, that the setosa
and virginica
have no color to their left. Is there any way to correctly add a legend to a jointplot in seaborn?
推荐答案
您可以将 label
添加到您的两个绘图调用中,并从 ax.legend()
中删除名称代码>.所以完整的例子(见 #CHANGE HERE
注释):
You can add label
to both of your plotting calls, and remove the names from ax.legend()
. So the full example (see the #CHANGE HERE
comments):
import seaborn as sns
import matplotlib.pyplot as plt
#I'm taking a stab at defining what you had
#the resulting plot is different
#but method still applies
iris = sns.load_dataset("iris")
setosa = iris[iris['species'] == 'setosa']
virginica = iris[iris['species'] == 'virginica']
graph = sns.jointplot(setosa.sepal_width, setosa.sepal_length,
cmap="Reds", kind="kde", marginal_kws={"color":"r", "alpha":.2}, shade=True, shade_lowest=False, alpha=.5,
label='setosa') ## CHANGE HERE
graph.x = virginica.sepal_width
graph.y = virginica.sepal_length
graph.plot_joint(sns.kdeplot, cmap="Blues", shade=True, shade_lowest=False, alpha=.5, label='virginica') ## CHANGE HERE
graph.set_axis_labels()
ax = plt.gca()
ax.legend() #CHANGE HERE
graph.plot_marginals(sns.kdeplot, color='b', shade=True, alpha=.2, legend=False)
这篇关于正确地将图例添加到 seaborn 联合图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文