简体   繁体   English

pandas 中每列的散点图

[英]Scatter subplots in pandas for every column

I'd like to plot a scatter plot for every pair [df_unempl,df_ipc] columns.我想为每对 [df_unempl,df_ipc] 列 plot 分散 plot 。 The two dataframes go from 2000 to 2020. So, in total 20 scatter plots.从 2000 年到 2020 年的两个数据帧 go。因此,总共有 20 个散点图。

Can this be done with a for loop?这可以用for循环来完成吗? In the way that it shows the 20 graphs at a time.以一次显示 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})

Thanks in advance!提前致谢!

You can define a matplotlib figure with 20 subplots and then redirect the seaborn plot to the matplotlib axes with the keyword argument ax . You can define a matplotlib figure with 20 subplots and then redirect the seaborn plot to the matplotlib axes with the keyword argument ax .

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()

However from your question it is unclear how the column names are formatted and how the dataframes look like.但是,从您的问题来看,尚不清楚列名的格式以及数据框的外观。 Therefore I can't tell you how to adjust the arguments of sns.relplot so that they fetch the data from the correct columns.因此,我无法告诉您如何调整 sns.relplot 的sns.relplot以便它们从正确的列中获取数据。 I need more info for that.我需要更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM