【问题标题】:How to arrange 4 Seaborn plots in a grid (Python)?如何在网格中排列 4 个 Seaborn 图(Python)?
【发布时间】:2019-02-20 23:29:51
【问题描述】:

我想在 2 x 2 网格中排列四个 Seaborn 地块。我尝试了以下代码,但出现异常。我还想知道如何在子图中设置标题和 xlabel、ylabel 以及为整个网格图设置标题。

一些玩具数据:

df
'{"age":{"76":33,"190":30,"255":36,"296":27,"222":19,"147":39,"127":23,"98":24,"168":29,"177":39,"197":27,"131":36,"36":30,"219":28,"108":38,"198":34,"40":32,"246":24,"109":26,"117":47,"20":26,"113":24,"279":35,"120":35,"7":26,"119":28,"272":24,"66":28,"87":28,"133":28},"Less_than_College":{"76":1,"190":1,"255":0,"296":1,"222":1,"147":1,"127":0,"98":0,"168":1,"177":1,"197":0,"131":1,"36":0,"219":0,"108":0,"198":0,"40":0,"246":0,"109":1,"117":1,"20":0,"113":0,"279":0,"120":0,"7":0,"119":1,"272":0,"66":1,"87":0,"133":0},"college":{"76":0,"190":0,"255":0,"296":0,"222":0,"147":0,"127":1,"98":1,"168":0,"177":0,"197":1,"131":0,"36":1,"219":1,"108":0,"198":1,"40":1,"246":0,"109":0,"117":0,"20":1,"113":1,"279":0,"120":1,"7":1,"119":0,"272":0,"66":0,"87":1,"133":1},"Bachelor":{"76":0,"190":0,"255":1,"296":0,"222":0,"147":0,"127":0,"98":0,"168":0,"177":0,"197":0,"131":0,"36":0,"219":0,"108":1,"198":0,"40":0,"246":1,"109":0,"117":0,"20":0,"113":0,"279":1,"120":0,"7":0,"119":0,"272":1,"66":0,"87":0,"133":0},"terms":{"76":30,"190":15,"255":30,"296":30,"222":30,"147":15,"127":15,"98":15,"168":30,"177":30,"197":15,"131":30,"36":15,"219":15,"108":30,"198":7,"40":30,"246":15,"109":15,"117":15,"20":15,"113":15,"279":15,"120":15,"7":15,"119":30,"272":15,"66":30,"87":30,"133":15},"Principal":{"76":1000,"190":1000,"255":1000,"296":1000,"222":1000,"147":800,"127":800,"98":800,"168":1000,"177":1000,"197":1000,"131":1000,"36":1000,"219":800,"108":1000,"198":1000,"40":1000,"246":1000,"109":1000,"117":1000,"20":1000,"113":800,"279":800,"120":800,"7":800,"119":1000,"272":1000,"66":1000,"87":1000,"133":1000}}'

fig = plt.figure()
fig.subplots_adjust(hspace=0.4, wspace=0.4)
ax = fig.add_subplot(2, 2, 1)
ax.sns.distplot(df.Principal)
ax = fig.add_subplot(2, 2, 2)
ax.sns.distplot(df.terms)
ax = fig.add_subplot(2, 2, 3)
ax.sns.barplot(data = df[['Less_than_College', 'college', 'Bachelor', ]])
ax = fig.add_subplot(2, 2, 4)
ax.sns.boxplot(data = df['age'])
plt.show()

AttributeError: 'AxesSubplot' object has no attribute 'sns'

【问题讨论】:

    标签: python pandas matplotlib grid seaborn


    【解决方案1】:

    ax 是没有 sns 属性的 matplotlib 对象,因此您会遇到错误。 sns 是海运对象。如果您想在 seaborn 对象中使用 ax 对象和 seaborn 图作为案例传递参数 ax=ax,如下所示:

    fig = plt.figure()
    fig.subplots_adjust(hspace=0.4, wspace=0.4)
    ax = fig.add_subplot(2, 2, 1)
    sns.distplot(df.Principal,ax=ax)
    ax = fig.add_subplot(2, 2, 2)
    sns.distplot(df.terms,ax=ax)
    ax = fig.add_subplot(2, 2, 3)
    sns.barplot(data = df[['Less_than_College', 'college', 'Bachelor']],ax=ax)
    ax.set_xticklabels(ax.get_xticklabels(), rotation=90)
    ax = fig.add_subplot(2, 2, 4)
    sns.boxplot(df['age'],ax=ax)
    plt.show()
    

    剧情是这样的。

    【讨论】:

    • 在 seaborn 0.11.2 中运行产生:UserWarning: displot is a figure-level function and does not accept the ax= parameter. You may wish to try histplot. 并且这些图是在单独的图中绘制的。这个answer 提供了更多信息。
    猜你喜欢
    • 2022-01-19
    • 2020-10-06
    • 1970-01-01
    • 2019-07-02
    • 2017-10-30
    • 2021-10-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多