微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Seaborn regplot 带有 sharey=True 的水平子图并显示 y 刻度标签

如何解决Seaborn regplot 带有 sharey=True 的水平子图并显示 y 刻度标签

我并排放置了这三个 regplots,但是,我想做这些: - 增加图表的大小 - 将它们分开一点,以便更清楚地看到 y 轴 - 查看右侧两个图的 y 轴上的值。

有谁知道如何有效地做到这一点?谢谢

fig,(ax1,ax2,ax3) = plt.subplots(ncols=3,sharey=True)
sns.regplot(x=Dem['Price'],y=Dem['A'],color="g",ax=ax1)
sns.regplot(x=Dem['Price'],y=Dem['B'],color="b",ax=ax2)
sns.regplot(x=Dem['Price'],y=Dem['C'],color="purple",ax=ax3)

enter image description here

解决方法

  1. 您可以使用:fig.set_figwidth(25) 来加宽图形并创建传递您想要的任何数值的空间,例如25

  2. 要标记所有子图的 y 轴刻度,请使用:

    for ax in fig.axes:
        ax.tick_params(axis='y',labelleft=True)
    

具有 flights seaborn 数据集的完全可重现的代码示例:

import seaborn as sns
df = sns.load_dataset('flights')
df1 = df[df['year']==1949]
df2 = df[df['year']==1950]
df3 = df[df['year']==1951]
fig,(ax1,ax2,ax3) = plt.subplots(ncols=3,sharey=True)

#1
fig.set_figwidth(25)

sns.regplot(x=df1['year'],y=df1['passengers'],color="g",ax=ax1)
sns.regplot(x=df2['year'],y=df2['passengers'],color="b",ax=ax2)
sns.regplot(x=df3['year'],y=df3['passengers'],color="purple",ax=ax3)

#2
for ax in fig.axes:
    ax.tick_params(axis='y',labelleft=True)

enter image description here

您的代码:

fig,sharey=True)
fig.set_figwidth(25)
sns.regplot(x=Dem['Price'],y=Dem['A'],ax=ax1)
sns.regplot(x=Dem['Price'],y=Dem['B'],ax=ax2)
sns.regplot(x=Dem['Price'],y=Dem['C'],ax=ax3)
for ax in fig.axes:
    ax.tick_params(axis='y',labelleft=True)

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。