使用 Seaborn 的 FacetGrid 自定义注释

Customizing annotation with Seaborn's FacetGrid

我正在尝试使用 Python 中的 Seaborn 模块自定义一些图形,但我没有成功创建自定义标签或注释。我有一些生成下图的代码:

plot = sns.FacetGrid(data = data, col = 'bot', margin_titles = True).set_titles('Human', 'Bot')
bins = np.linspace(0, 2000, 15)
plot = plot.map(plt.hist, 'friends_count', color = 'black', lw = 0, bins = bins)
plot.set_axis_labels('Number Following', 'Count')
sns.despine(left = True, bottom = True)

我想做两件事:1. 替换默认的因子标签,例如'bot = 0.0',带有有意义的文本,以及 2. 在每个类别的平均数字后面画垂直线。

这是一个 self-contained 示例:

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

fake = pd.DataFrame({'val': [1, 2, 2, 3, 3, 2, 1, 1, 2, 3], 'group': [0, 0, 0, 0, 0, 1, 1, 1, 1, 1]})
plot = sns.FacetGrid(data = fake, col = 'group', margin_titles = True).set_titles('zero', 'one')
plot = plot.map(plt.hist, 'val', color = 'black', lw = 0)
sns.despine(left = True, bottom = True)

有人知道如何自定义 FacetGrids 吗?

关于 set_titles 的一些事情。

首先,默认的标题是在FacetGrid.map方法中绘制的,所以如果你想改变标题,你必须调用set_titles after绘图,否则它们将被覆盖。

其次,如果您查看该方法的文档字符串,它不只是采用任意标题列表。它提供了一种使用列变量名称和值更改标题呈现方式的方法:

template : string
    Template for all titles with the formatting keys {col_var} and
    {col_name} (if using a `col` faceting variable) and/or {row_var}
    and {row_name} (if using a `row` faceting variable).

因此,获得 "meaningful text" 的最简单方法是在数据框中使用有意义的数据。以随机数据为例:

df = pd.DataFrame({'val': np.random.randn(100),
                   'group': np.repeat([0, 1], 50)})

如果您希望 "group" 成为 zeroone,您应该只更改该列,或创建一个新列:

df["group"] = df["group"].map({0: "zero", 1; "one"})

然后说你不想在标题中有变量名,正确的使用方法FacetGrid.set_titles应该是

g = sns.FacetGrid(data=df, col='group')
g.map(plt.hist, 'val', color='black', lw=0)
g.set_titles('{col_name}')

如果您不想更改正在绘制的数据,则必须直接在 matplotlib 轴上设置属性,例如:

for ax, title in zip(g.axes.flat, ['zero', 'one']):
    ax.set_title(title)

请注意,这不如上述方法可取,因为您必须非常小心地确保列表的顺序正确并且不会更改,同时从数据框本身获取信息会更健壮。

要绘制平均值,您需要创建一个可以传递给 FacetGrid.map 的小函数。教程中有 multiple examples 说明如何执行此操作。在这种情况下,这很容易:

def vertical_mean_line(x, **kwargs):
    plt.axvline(x.mean(), **kwargs)

那么你只需要 re-plot:

g = sns.FacetGrid(data=df, col='group')
g.map(plt.hist, 'val', color='black', lw=0)
g.map(vertical_mean_line, 'val')
g.set_titles('{col_name}')