繁体   English   中英

如何将传说放在seaborn.FacetGrid的第一个子图上?

[英]How to put the legend on first subplot of seaborn.FacetGrid?

我有一个pandas DataFrame df ,我用seaborn.barplot子图可视化。 我的问题是我想把我的传说移到其中一个子图中。

要根据条件(在我的案例中为Area )创建子图,我使用seaborn.FacetGrid 这是我使用的代码:

import matplotlib.pyplot as plt
import matplotlib
import seaborn as sns
# .. load data
grid = sns.FacetGrid(df, col="Area", col_order=['F1','F2','F3'])
bp = grid.map(sns.barplot,'Param','Time','Method')
bp.add_legend()
bp.set_titles("{col_name}")
bp.set_ylabels("Time (s)")
bp.set_xlabels("Number")
sns.plt.show()

这产生了这个情节:

barplot,所有

你看到这里的图例完全在右边,但是我想把它放在一个图中(例如左图),因为我的原始数据标签很长而且图例占用的空间太大。 这是仅有1个绘图的示例,其中图例位于绘图内:

barplot1

和代码:

mask = df['Area']=='F3'
ax=sns.barplot(x='Param',y='Time',hue='Method',data=df[mask])
sns.plt.show()

测试1 :我尝试了一个答案的例子,他们在其中一个子图中有传说:

grid = sns.FacetGrid(df, col="Area", col_order=['F1','F2','F3'])
bp = grid.map(sns.barplot,'Param','Time','Method')
Ax = bp.axes[0]
Boxes = [item for item in Ax.get_children()
     if isinstance(item, matplotlib.patches.Rectangle)][:-1]
legend_labels  = ['So1', 'So2', 'So3', 'So4', 'So5']
# Create the legend patches
legend_patches = [matplotlib.patches.Patch(color=C, label=L) for
              C, L in zip([item.get_facecolor() for item in Boxes],
                          legend_labels)]

# Plot the legend
plt.legend(legend_patches)    
sns.plt.show()

请注意,我更改了plt.legend(handles=legend_patches)对我不起作用,因此我在本回答中使用plt.legend(legend_patches)作为评论。 结果却是:

第三届传奇测试

如您所见,图例位于第三个子图中,颜色和标签都不匹配。


测试2

最后,我尝试创建一个列包裹为2( col_wrap=2 )的子图,其中包含右下方正方形的图例:

grid = sns.FacetGrid(df, col="MapPubName", col_order=['F1','F2','F3'],col_wrap=2)

但这也导致传说在右边:

barplot 2cols


问题 :如何在第一个子图中获取图例? 或者我如何将图例移动到网格中的任何位置?

您可以使用grid.axes[i][j].legend()在所需的特定轴上设置图例。

对于1 row, 3 column网格的情况,您需要将grid.axes[0][0].legend()为在左侧绘图。

这是从您的代码派生的简单示例,但更改为考虑样本数据集。

import matplotlib.pyplot as plt
import matplotlib
import seaborn as sns

df = sns.load_dataset("tips")

grid = sns.FacetGrid(df, col="day")
bp = grid.map(sns.barplot,"time",'total_bill','sex')

grid.axes[0][0].legend()

bp.set_titles("{col_name}")
bp.set_ylabels("Time (s)")
bp.set_xlabels("Number")
sns.plt.show()

在此输入图像描述

  1. 使用legend_out=False选项。

  2. 如果要制作多面条形图,则应使用factorplot with kind=bar 否则,如果您没有明确指定每个方面的顺序,则您的绘图可能最终会出错。

     import seaborn as sns tips = sns.load_dataset("tips") sns.factorplot(x="sex", y="total_bill", hue="smoker", col="day", data=tips, kind="bar", aspect=.7, legend_out=False) 

在此输入图像描述

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM