首页 > 解决方案 > 如何从 2 个不同的数据集中并排显示 seaborn 图?

问题描述

sns.set(style="darkgrid")
parm = azdias_under_20.columns.to_series(index=None).sample(3)

for y in parm:
    sns.countplot(x=y, data=azdias_under_20)
    plt.show();
    sns.countplot(x=y, data=azdias_over_20)
    plt.show();

如何从 2 个不同的数据集中并排显示 seaborn 图?请回答确切的细节,而不是子情节或其他来源的链接。

谢谢你。

    ALTERSKATEGORIE_GROB    ANREDE_KZ   CJT_GESAMTTYP   FINANZ_MINIMALIST   FINANZ_SPARER   FINANZ_VORSORGER    FINANZ_ANLEGER  FINANZ_UNAUFFAELLIGER   FINANZ_HAUSBAUER    FINANZTYP   ... PLZ8_ANTG1  PLZ8_ANTG2  PLZ8_ANTG3  PLZ8_ANTG4  PLZ8_BAUMAX PLZ8_HHZ    PLZ8_GBZ    ARBEIT  ORTSGR_KLS9 RELAT_AB
1   1.0 2   5.0 1   5   2   5   4   5   1   ... 2.0 3.0 2.0 1.0 1.0 5.0 4.0 3.0 5.0 4.0
2   3.0 2   3.0 1   4   1   2   3   5   1   ... 3.0 3.0 1.0 0.0 1.0 4.0 4.0 3.0 5.0 2.0
3   4.0 2   2.0 4   2   5   2   1   2   6   ... 2.0 2.0 2.0 0.0 1.0 3.0 4.0 2.0 3.0 3.0
4   3.0 1   5.0 4   3   4   1   3   2   5   ... 2.0 4.0 2.0 1.0 2.0 3.0 3.0 4.0 6.0 5.0
5   1.0 2   2.0 3   1   5   2   2   

标签: pythonplotseaborn

解决方案


像这样的东西应该工作:

from matplotlib import pyplot as plt
import seaborn as sns

# fill in azdias_under_20, azdias_over_20, parm, ....

# draw two plots, next to each other with the same x and y axis
for y in parm:
    fig, (ax1, ax2) = plt.subplots(figsize=(12, 6), ncols=2, sharex=True, sharey=True)
    sns.countplot(x=y, data=azdias_under_20, ax=ax1)
    sns.countplot(x=y, data=azdias_over_20, ax=ax2)
    plt.show();

推荐阅读