Home > Net >  How can I automate the creation of swarmplots for each column in a df
How can I automate the creation of swarmplots for each column in a df

Time:06-30

I am trying to automate the creation of a swarmplot for each column of a dataframe to avoid doing in manually. My dataframe has 9 columns, so in the end I would like to have 9 plots. But instead of typing sns.swarmplot(x='Classification',y='columnx',data=df) 9 times, I wanted to do it with a for-loop.

I was trying something like the following code but it doesn't seem to work. It creates a single swarmplot:

for col in df.columns: 
     sns.swarmplot(x='Classification',y=col,data=df)

Any ideas on how I could make it work would be much appreciated. Thanks in advance.

CodePudding user response:

import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
for col in df: 
    sns.swarmplot(x='Classification',y=col,data=df)
    plt.show()

Result you can check

  • Related