python - 将箱线图排列为带有 seaborn `FacetGrid` 的网格

标签 python python-3.x seaborn boxplot

我有一个包含三列的数据框 Features, CV-fold, Accuracy, Network .我想为每个网络绘制一个箱线图,按轴的特征和 CV 折叠进行分组(参见示例图像)。

Output right now

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 (%)")

因为我有 8 个不同的网络,所以我不想将它们全部放在一列或一行中,而是在网格中格式化(例如 2x4)。此外,即使 sharex未启用,x 轴仅标记在最底部的图形。

我怎样才能做到这一点?

最佳答案

您将使用 col_wrap关键字参数以在多列的多行上绘制您的图。

要重复 x 轴标签,请使用 ax.tick_params() .

例子:

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()

结果:
enter image description here

关于python - 将箱线图排列为带有 seaborn `FacetGrid` 的网格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60265159/

相关文章:

python - python中的RSA加密和JS中的解密

python - 如何使用 python3 在 "horizontal union"中加入两个 pandas DataFrame

python - 每个色调带有堆叠条的计数图

python - 如何使用元组打印此图案?

python - 如何在 Python 中发出 PATCH 请求?

python - 删除 too 元素之间的多个 XML 元素

python - Jupiter Python seaborn 热图未显示所有相关性

python 3.6 gmail api——发送带附件的电子邮件

python - 修改冷却装饰器以用于方法而不是函数

python - 为什么 seaborn Heatmap 显示白色的行和列?