Seaborn FacetGrid彼此相邻绘制两个不同的y轴

时间:2018-10-05 06:45:34

标签: python seaborn

这有效,但是我敢肯定必须有一种通过FacetGrid做到这一点的方法。我想像一种参数,您可以在其中访问图表在网格上的位置。也许这被认为是多余的,使用plt.subplots是访问网格轴的方法。

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)

即使我这样称呼,它也会打印在显示屏上...

<seaborn.axisgrid.FacetGrid at 0x1a314b6c50>

当然,我把两个猫形图彼此相邻,但下面是一个空的网格。

1 个答案:

答案 0 :(得分:1)

实际上令人惊讶的是该代码有效。 sns.catplot()旨在绘制到FacetGrid上,并不意味着以您使用它的方式使用。

我认为您应该直接使用sns.boxplot()

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)

如果您真的想使用catplot,则必须转换数据框,以使所有y值都在同一列中,并附加一个类别列,以指示值的类型(int.rate / fico ),然后致电:

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

和seaborn会自动将正确的子图放入您的类别列中的唯一类别中。