How to plot multiple seaborn catplots on a 2x2 gri

2020-04-18 07:46发布

问题:

I tried to use the FacetGrid to plot a 2x2-grid with each subplot being a catplot of the same data, but just with different 'jitter' values. It didn't worked.

Here is the code I am using now:

sns.catplot(x="Sex", y="SidestepDist", jitter=False, data=daten_csv)
sns.catplot(x="Sex", y="SidestepDist", jitter=0.2, data=daten_csv)
sns.catplot(x="Sex", y="SidestepDist", jitter=0.5, data=daten_csv)
sns.catplot(x="Sex", y="SidestepDist", jitter=1, data=daten_csv)

But of course I get the plots below each other like this:

How could I have a main plot, with all 4 subplots placed as a 2x2 matrix (grid)?

回答1:

So.. I figure it out, with the great help of @ImportanceOfBeingErnest

Here is the way to do it:

Plot the Sidestep distance against Sex

fig, ax = plt.subplots(2,2, figsize=(12,10))
jitter = [[False, 1], [0.5, 0.2]]

for j in range(len(ax)):
    for i in range(len(ax[j])):
        ax[j][i].tick_params(labelsize=15)
        ax[j][i].set_xlabel('label', fontsize=17, position=(.5,20))
        ax[j][i].set_ylabel('label', fontsize=17)
        # x as Hindernisabstand hinzufügen 
        ax[j][i] = sns.stripplot(x="Sex", y="SidestepDist", jitter=jitter[j][i], data=daten_csv, ax=ax[j][i])
fig.suptitle('Categorical Features Overview', position=(.5,1.1), fontsize=20)
fig.tight_layout()

fig.show()

And here is what it looks like: