简体   繁体   English

使用 seaborn `FacetGrid` 将箱线图排列为网格

[英]Arrange boxplots as a grid with seaborn `FacetGrid`

I have a data frame with three columns Features, CV-fold, Accuracy, Network .我有一个包含三列Features, CV-fold, Accuracy, Network的数据框。 I want to have a boxplot for each Network, grouped by the Features and the CV-fold for the axis (see example image).我想为每个网络绘制一个箱线图,按轴的特征和 CV 折叠进行分组(参见示例图像)。

立即输出

df = pd.read_csv(path)
df['Features'] = df["Features"].astype('category')
ordered_features = sorted(df.Network.value_counts().index)

df = df.loc[df['Accuracy'] > 0.1]
df.Accuracy = df.Accuracy*100

#sns.color_palette("husl", len(df['CV-fold'].value_counts().index))
#sns.set_palette('husl', len(df['CV-fold'].value_counts().index))

g = sns.FacetGrid(df, row="Network", row_order=ordered_features,
                  height=3, aspect=3, legend_out=True, despine=False)
g.map(sns.boxplot, x="CV-fold", y="Accuracy", hue="Features", data=df, palette='muted').add_legend()

g.set_axis_labels("", "Accuracy (%)")

Because I have 8 different networks, I would like to not have them all in a column or a row, but formatted in a grid (eg 2x4).因为我有 8 个不同的网络,所以我不想将它们全部放在一列或一行中,而是在网格中格式化(例如 2x4)。 Additionally, even though sharex is not enabled, the x-axis is only labeled at the very bottom graph.此外,即使未启用sharex ,x 轴也仅标记在最底部的图形中。

How can I do that?我怎样才能做到这一点?

You would use the col_wrap keyword argument to get your plots on multiple rows with multiple columns.您可以使用col_wrap关键字参数在多行多列上绘制图。

For repeating the x-axis labels use ax.tick_params() .要重复 x 轴标签,请使用ax.tick_params()

Example:例子:

import seaborn as sns, matplotlib.pyplot as plt

tips = sns.load_dataset('tips')
ordered_days = sorted(tips['day'].unique())
g = sns.FacetGrid(tips,col='day',col_order=ordered_days,col_wrap=2)
#                                               change this to 4 ^
g.map(sns.boxplot,'sex','total_bill',palette='muted')
for ax in g.axes.flatten(): 
    ax.tick_params(labelbottom=True)
plt.tight_layout()
plt.show()

Result:结果: 在此处输入图片说明

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

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