首页 > 解决方案 > 具有相同 Y 轴刻度的 Seaborn Reg 图(并排)

问题描述

我在 seaborn 有两个 reg 地块

sb.regplot(x="V", y="Matrix Time", data = df_1, scatter_kws={"color": "b"}, line_kws={"color": "red"})
sb.regplot(x="V", y="List Time", data = df_1, scatter_kws={"color": "g"}, line_kws={"color": "red"})

我想在 Y 轴上以相同的比例并排显示这些图。如果有人可以帮助我,我将不胜感激。

编辑:我尝试使用

fig, ax = plt.subplots(1,2, figsize=(16,8))
sb.regplot(x="V", y="Matrix Time", data=df_1, ax=ax[0], scatter_kws={"color": "b"}, line_kws={"color": "red"})
sb.regplot(x="V", y="List Time", data=df_1, ax=ax[1], scatter_kws={"color": "g"}, line_kws={"color": "red"})

虽然它确实给了我两张相同大小的图表。每个图表的 Y 轴比例不同。

https://i.stack.imgur.com/xkIUO.png

我希望将两个图形并排放置,以使每个图形的 Y 轴上的值处于相同的水平水平。像这样:https ://i.stack.imgur.com/28iko.png

(我通过在 powerpoint 中截取每个图形的屏幕截图并调整大小以使 y 轴值处于同一水平来创建上面的图像)

标签: pythonpython-3.xmatplotlibseaborn

解决方案


在您的代码中,您需要编写:

sb.regplot(x="V", y="Matrix Time", data = df_1, scatter_kws={"color": "b"}, line_kws={"color": "red"}, ax=ax[0]) 
sb.regplot(x="V", y="List Time", data = df_1, scatter_kws={"color": "g"}, line_kws={"color": "red"}, ax=ax[1])

对于您编辑的问题,您需要plt.subplots(..., sharey=True). 我写的例子:

import seaborn as sns
import pandas as pd
import matplotlib.pyplot as plt

tips = sns.load_dataset("tips")

fig, ax =plt.subplots(1,2, sharey=True)
sns.regplot(x="total_bill", y="tip", data=tips, ax=ax[0])
sns.regplot(x="total_bill", y="tip", data=tips.loc[:10], ax=ax[1])
fig.show()

输出:

在此处输入图像描述


推荐阅读