Search code examples
pythonpandasplotseabornscatter

Scatter subplots in pandas for every column


I'd like to plot a scatter plot for every pair [df_unempl,df_ipc] columns. The two dataframes go from 2000 to 2020. So, in total 20 scatter plots.

Can this be done with a for loop? In the way that it shows the 20 graphs at a time.

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!


Solution

  • 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. I need more info for that.