7

我有一个看起来像这样的数据框: 数据框:

有几个不同的model_names。我正在尝试使用以下代码绘制 seaborn catplot 中的数据:

sns.set(style="whitegrid")
sns.catplot(x='model_name', y='score', hue='train_val_test', col='score_name',
            data=classification_scores, kind='bar', height=4, aspect=.8)

以下是我得到的图表:图形

如何更改格式以使图表显示在 2x2 网格上?将它们全部放在一条线上太拥挤了。

4

2 回答 2

9

使用参数col_wraprow_wrap设置所需的列/行数。IE

sns.catplot(
    x='model_name',
    y='score',
    hue='train_val_test',
    col='score_name',
    col_wrap=3, #Set the number of columns you want.
    data=classification_scores,
    kind='bar',
    height=4,
    aspect=.8
)

对于 3 列。同样,如果row用于分类,则调用相应的变量row_wrap

于 2020-11-16T16:37:45.187 回答
0
import seaborn as sns
import matplotlib.pyplot as plt
plt.figure(figsize=(16,10))
sns.set(style="whitegrid")

for ax_num, score in zip(range(1,5), ['f1', 'recall', 'accuracy', 'precision']):
    plt.subplot(2,2,ax_num)
    sns.barplot(x='model_name', y='score', hue='train_val_test',
                data=classification_scores[classification_scores['score_name'] == score])
    plt.xticks(rotation=15, fontsize=14)
    
plt.tight_layout()

在此处输入图像描述

于 2020-07-23T19:30:15.443 回答