在 pandas 中为每一列散布子图

Scatter subplots in pandas for every column

我想为每对 [df_unempl,df_ipc] 列绘制一个散点图。这两个数据帧从 2000 年到 2020 年。因此,总共有 20 个散点图。

这可以用 for 循环来完成吗?以一次显示20个图表的方式。

unempl=df_unempl
deflac=df_ipc
z=pd.merge_ordered(unempl,deflac,on='Country Code',how='inner')
z=z.fillna(0)


sns.lmplot(x='2000 [YR2000]_x', y='2000 [YR2000]_y', data=z,order=1, 
ci=None, scatter_kws={"s": 10})

sns.lmplot(x='2001 [YR2001]_x', y='2001 [YR2001]_y', data=z,order=1, 
ci=None, scatter_kws={"s": 10})

sns.lmplot(x='2002 [YR2002]_x', y='2002 [YR2002]_y', data=z,order=1, 
ci=None, scatter_kws={"s": 10})

sns.lmplot(x='2003 [YR2003]_x', y='2003 [YR2003]_y', data=z,order=1, 
ci=None, scatter_kws={"s": 10})
.
.
.
sns.lmplot(x='2020 [YR2020]_x', y='2020 [YR2020]_y', data=z,order=1, 
ci=None, scatter_kws={"s": 10})

提前致谢!

您可以定义一个包含 20 个子图的 matplotlib 图,然后使用关键字参数 ax.

将 seaborn 图重定向到 matplotlib 轴
import matplotlib.pyplot as plt
import seaborn as sns

fig, axs = plt.subplots(5, 4)
for i, ax in enumerate(axs.ravel()):
    year = 2000 + i
    sns.regplot(x=f'{year} [YR{year}]_x', y=f'{year} [YR{year}]_y', data=z,order=1, 
               ci=None, scatter_kws={"s": 10}, ax=ax)
plt.show()

但是,从您的问题来看,列名的格式以及数据框的外观并不清楚。因此,我无法告诉您如何调整 sns.relplot 的参数,以便它们从正确的列中获取数据。我需要更多信息。