我正在尝试使用 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. 在每个类别的平均数字后面绘制垂直线。
这是一个独立的例子:
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
之后 绘图,否则它们将被覆盖。
其次,如果您查看该方法的文档字符串,它不仅仅采用任意标题列表。它提供了一种使用列变量名称和值更改标题呈现方式的方法:
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).
因此,获得“有意义的文本”的最简单方法是在数据框中使用有意义的数据。以随机数据为例:
df = pd.DataFrame({'val': np.random.randn(100),
'group': np.repeat([0, 1], 50)})
如果您希望“组”为零
和一
,您应该只更改该列,或创建一个新列:
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)
然后你只需要重新绘制:
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}')
关于python - 使用 Seaborn 的 FacetGrid 自定义注释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31632372/