Yale Newman
Yale Newman

Reputation: 1231

Seaborn FacetGrid plot two different y axis next to eachother

This works, but I'm sure there's got to be a way to do this via FacetGrid. Im imagining some kind of parameter where you can access where the chart is located on the grid. Or maybe this was considered redundant and using plt.subplots is the way to access the axis of the grid.

fig, (ax1, ax2) = plt.subplots(nrows=1, ncols=2)
sns.catplot(x='not.fully.paid', y='int.rate', kind='box', data=loans, ax=ax1)
sns.catplot(x='not.fully.paid', y='fico', kind='box', data=loans, ax=ax2)

Even when I call this, this prints out in the display...

<seaborn.axisgrid.FacetGrid at 0x1a314b6c50>

Granted, I get my plot of the two catplots next to eachother, but an empty facetgrid below.

Upvotes: 2

Views: 3072

Answers (1)

Diziet Asahi
Diziet Asahi

Reputation: 40667

It's actually surpising that this code works. sns.catplot() is designed to plot onto a FacetGrid and is not meant to be used the way you are using it.

I think you should use sns.boxplot() directly:

fig, (ax1, ax2) = plt.subplots(nrows=1, ncols=2)
sns.boxplot(x='not.fully.paid', y='int.rate', kind='box', data=loans, ax=ax1)
sns.boxplot(x='not.fully.paid', y='fico', kind='box', data=loans, ax=ax2)

If you really wanted to use catplot you would have to transform your dataframe so that all y-values would be in a single column, with an additional category column indicating the type of value (int.rate/fico) and then call:

sns.catplot(x='not.fully.paid', y='<values column>', col='<category column>', kind='box', data=loans)

and seaborn would automatically the correct subplots to accommodate the number of unique categories in your category column.

Upvotes: 4

Related Questions