将盒形图布置为带有Seabor`FacetGrid`的网格

问题描述 投票:1回答:1

我有一个包含三列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轴也仅标记在最底部的图表上。

我该怎么做?

python-3.x seaborn boxplot
1个回答
1
投票

您将使用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

© www.soinside.com 2019 - 2024. All rights reserved.