首页 > 解决方案 > Seaborn relplot 子图轴限制

问题描述

这是我在 Stackoverflow 上的第一个问题!

我一直在用 seaborn 绘制一些散点图relplot()。对于我的前两张图,子图的大小和它们的 x 轴看起来很正常。但是在我的第三个情节中,我得到了这样的东西

在此处输入图像描述

你可以看到 x 轴只有 2 个从 0 到 1 的刻度,我想让它们与 y 轴相同。这是我的代码:

#Setting style
sns.set_style('ticks')
sns.set_palette('RdBu',6)
plt.figure(figsize=(10,8))

#Plotting
rel = sns.relplot(data=df_scenarios, x="feb_pred", 
                  y="mar_pred", col="scenarios", col_wrap=2, 
                  kind="scatter", hue="pred_diff")

#Customizing
rel.set(xlabel="202002 pred", ylabel = "202003 pred")

a0 = rel.fig.axes[0]
a0.set_title("scen1")
a0.plot([0,1],[0,1],'black',linewidth = 2 )
a1 = rel.fig.axes[1]
a1.set_title("scen2")
a1.plot([0,1],[0,1],'black',linewidth = 2 )
a2 = rel.fig.axes[2]
a2.set_title("scen3")
a2.plot([0,1],[0,1],'black',linewidth = 2 )
a3 = rel.fig.axes[3]
a3.set_title("scen4")
a3.plot([0,1],[0,1],'black',linewidth = 2 )
a4 = rel.fig.axes[4]
a4.set_title("scen5")
a4.plot([0,1],[0,1],'black',linewidth = 2 )
a5 = rel.fig.axes[5]
a5.set_title("scen6")
a5.plot([0,1],[0,1],'black',linewidth = 2 )

plt.show()

有谁知道我可以改变什么以使 x 和 y 轴具有相同的刻度?

谢谢!

标签: pythonseaborn

解决方案


欢迎来到 Stackoverflow!您可以将此添加到您的绘图自定义中,以强制 x 限制等于 y 限制并使每个子图二次:

for ax in rel.fig.axes:
    ax.set_xlim(ax.get_ylim())
    ax.set_aspect('equal')

您可能还想更改图形大小以适应 2x3 格式。


推荐阅读