Seaborn FacetGrid plot two different y axis next to eachother

天涯浪子 提交于 2020-05-31 00:00:10

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/52659771/seaborn-facetgrid-plot-two-different-y-axis-next-to-eachother

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!