仅为seaborn中的一个边缘地块设置图例

时间:2017-11-16 09:24:22

标签: python matplotlib seaborn

我正在使用seaborn创建一个JointGrid图。

import seaborn as sns
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
mydataset=pd.DataFrame(data=np.random.rand(50,2),columns=['a','b'])
g = sns.JointGrid(x=mydataset['a'], y=mydataset['b'])
g=g.plot_marginals(sns.distplot,color='black',kde=True,hist=False,rug=True,bins=20,label='X')
g=g.plot_joint(plt.scatter,label='X')        


legend_properties = {'weight':'bold','size':8}
legendMain=g.ax_joint.legend(prop=legend_properties,loc='upper right')


legendSide=g.ax_marg_x.legend(prop=legend_properties,loc='upper right')

我明白了:

enter image description here

我想摆脱垂直边缘图(右边的那个)内的传说,但保留一个用于水平边距。 怎么实现呢?

编辑:来自@ImportanceOfBeingErnest的解决方案适用于一个情节。但是,如果我在for循环中重复它会发生意外情况。 我仍然在上面的情节中得到一个传奇,这是出乎意料的。 如何摆脱它?

以下代码:

import seaborn as sns
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
mydataset=pd.DataFrame(data=np.random.rand(50,2),columns=['a','b'])
g = sns.JointGrid(x=mydataset['a'], y=mydataset['b'])
LABEL_LIST=['x','Y','Z']
for n in range(0,3):

    g=g.plot_marginals(sns.distplot,color='black',kde=True,hist=False,rug=True,bins=20,label=LABEL_LIST[n])
    g=g.plot_joint(plt.scatter,label=LABEL_LIST[n])        


    legend_properties = {'weight':'bold','size':8}
    legendMain=g.ax_joint.legend(prop=legend_properties,loc='upper right')


    legendSide=g.ax_marg_y.legend(labels=[LABEL_LIST[n]],prop=legend_properties,loc='upper right')

给出:

enter image description here

这几乎是完美的,我需要摆脱右边plo中的最后一个传奇条目。

1 个答案:

答案 0 :(得分:1)

您可以决定不向边缘提供任何标签,而是在顶部边缘轴内创建图例时添加标签。

import seaborn as sns
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

mydataset=pd.DataFrame(data=np.random.rand(50,2),columns=['a','b'])
g = sns.JointGrid(x=mydataset['a'], y=mydataset['b'])
g=g.plot_marginals(sns.distplot,color='black',
                   kde=True,hist=False,rug=True,bins=20)
g=g.plot_joint(plt.scatter,label='X')        


legend_properties = {'weight':'bold','size':8}
legendMain=g.ax_joint.legend(prop=legend_properties,loc='upper right')


legendSide=g.ax_marg_x.legend(labels=["x"], 
                              prop=legend_properties,loc='upper right')

plt.show()

enter image description here

对于循环中的绘图,解决方案是相同的。

import seaborn as sns
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt

mydataset=pd.DataFrame(data=np.random.rand(50,2),columns=['a','b'])
g = sns.JointGrid(x=mydataset['a'], y=mydataset['b'])
LABEL_LIST=['x','Y','Z']
for n in range(0,3):
    g=g.plot_marginals(sns.distplot,color='black',kde=True,hist=False,rug=True,bins=20)
    g=g.plot_joint(plt.scatter,label=LABEL_LIST[n])        

legend_properties = {'weight':'bold','size':8}
legendMain=g.ax_joint.legend(prop=legend_properties,loc='upper right')
legendSide=g.ax_marg_x.legend(labels=LABEL_LIST,prop=legend_properties,loc='upper right')

plt.show()

enter image description here