YVS1997
YVS1997

Reputation: 680

Seaborn distplot only return one column when try to plot each Pandas column by loop for

I have problem when try to plot Pandas columns using for each loop when i use displot instead distplot it act well, besides it only show distribution globally, not based from its group. Let say i have list of column name called columns and Pandas' dataframe n, which has column name class. The goal is to show Distribution Plot based on column for each class:

for w in columns:
    if w!=<discarded column> or w!=<discarded column>:
        sns.displot(n[w],kde=True

but when I use distplot, it returns only first column:

for w in columns:
    if w!=<discarded column> or w!=<discarded column>:
        sns.distplot(n[w],kde=True

I'm still new using Seaborn, since i never use any visualization and rely on numerical analysis like p-value and correlation. Any help are appreciated.

Upvotes: 0

Views: 726

Answers (1)

You probably getting only the figure corresponding to the last loop.
So you have to explicitly ask for showing the picture in each loop.

import matplotlib.pyplot as plt

for w in columns:
    if w not in discarded_columns:
        sns.distplot(n[w], kde=True)
        plt.show()

or you can make subplots:

# Keep only target-columns
target_columns = list(filter(lambda x: x not in discarded_columns, columns))

# Plot with subplots
fig, axes = plt.subplots(len(target_columns)) # see the parameters, like: nrows, ncols ... figsize=(16,12)
for i,w in enumerate(target_columns):
    sns.distplot(n[w], kde=True, ax=axes[i])

Upvotes: 1

Related Questions