How to change the number or rows and columns in my catplot

5.6k Views Asked by At

I have a dataframe that looks like this: dataframe:

with several different model_names's. I am trying to graph the data in a seaborn catplot using the following code:

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)

The follow is the graph that I result in:graph

How do I change the format so the graphs are displayed on a 2x2 grid? Having them all in one line is too cramped.

2

There are 2 best solutions below

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

enter image description here

0
On

Use col_wrap:

'Wrap' the column variable at this width, so that the column facets span multiple rows. Incompatible with a row facet.

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
)