简体   繁体   English

如何注释 seaborn pairplots

[英]How to annotate seaborn pairplots

I have a collection of binned data from which I generate a series of seaborn pairplots.我有一组分箱数据,从中生成了一系列 seaborn 配对图。 Since all of the bins have the same labels, but not bin names, I need to annotate the pairplots with the bin name 'n' below so that I can later associate them with their bins.由于所有 bin 都具有相同的标签,但 bin 名称不同,因此我需要在下面用 bin 名称“n”注释 pairplots,以便稍后将它们与它们的 bin 相关联。

import seaborn as sns
groups = data.groupby(pd.cut(data['Lat'], bins))
for n,g in groups:
    p = sns.pairplot(data=g, hue="Label", palette="Set2", 
                 diag_kind="kde", size=4, vars=labels)

I noted in the documentation that seaborn uses, or is built upon, matplotlib. I have been unable to figure out how to annotate the legend on the left, or provide a title above or below the paired plots.我在文档中指出 seaborn 使用或构建于 matplotlib。我一直无法弄清楚如何在左侧注释图例,或在成对图的上方或下方提供标题。 Can anyone provide examples of pointers to the documentation on how to add arbitrary text to those three areas of a plot?任何人都可以提供有关如何向 plot 的这三个区域添加任意文本的文档的指针示例吗?

After following up on mwaskom's suggestion to use matplotlib.text() (thanks), I was able to get the following to work as expected: 在跟进mwaskom建议使用matplotlib.text() (谢谢)之后,我能够按预期方式使用以下内容:

p = sns.pairplot(data=g, hue="Label", palette="Set2", 
             diag_kind="kde", size=4, vars=labels)
#bottom labels
p.fig.text(0.33, -0.01, "Bin: %s"%(n), ha ='left', fontsize = 15)
p.fig.text(0.33, -0.04, "Num Points: %d"%(len(g)), ha ='left', fontsize = 15)

and other useful functionality: 和其他有用的功能:

# title on top center of subplot
p.fig.suptitle('this is the figure title', verticalalignment='top', fontsize=20)

# title above plot
p.fig.text(0.33, 1.02,'Above the plot', fontsize=20)

# left and right of plot
p.fig.text(0, 1,'Left the plot', fontsize=20, rotation=90)
p.fig.text(1.02, 1,'Right the plot', fontsize=20, rotation=270)

# an example of a multi-line footnote
p.fig.text(0.1, -0.08,
     'Some multiline\n'
     'footnote...',
      fontsize=10)

In addition, using the seaborn functionality, you can move the title up.此外,使用 seaborn 功能,您可以将标题上移。 After p=sns.pairplot , addp=sns.pairplot之后,添加

p.fig.suptitle("here is your title", y=1.05)

This title will be above the actual plot area instead of overlapping the plot.此标题将位于实际 plot 区域之上,而不是与 plot 重叠。

You can also use fontweight (bold, etc) in addition to the fontsize.除了字体大小之外,您还可以使用字体粗细(粗体等)。

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

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