Search code examples
pythonpandasplotseaborn

Create for loop to plot histograms for individual columns of DataFrame with Seaborn


So I'm trying to plot histograms for all my continous variables in my DatFrame using a for loop I've already managed to do this for my categorical variables using countplot with the following code:

df1 = df.select_dtypes([np.object])

for i, col in enumerate(df1.columns):
    plt.figure(i)
    sns.countplot(x=col, data=df1)

Which I found here by searching SO.

However now I want to do the same with distplot so I tried modifying the above code to:

df1 = dftest.select_dtypes([np.int, np.float])

for i, col in enumerate(df1.columns):
    plt.figure(i)
    sns.distplot(df1)

But it just gived me one empty plot. Any ideas on what I can do?

edit: e.g of DataFrame:

dftest = pd.DataFrame(np.random.randint(low=0, high=10, size=(5, 5)),
                    columns=['a', 'b', 'c', 'd', 'e']) 

Solution

  • sns.distplot has been replaced by sns.histplot. See Emulating deprecated seaborn distplots to match distplot.

    for i, col in enumerate(df1.columns):
        plt.figure(i)
        sns.histplot(df1[col], stat='density', kde=True, kde_kws={"cut": 3})
    

    It seems like you want to produce one figure with a distplot per column of the dataframe. Hence you need to specify the data in use for each specific figure.

    As the seaborn documentation says for distplot(a, ...)

    a : Series, 1d-array, or list. Observed data.

    So in this case:

    for i, col in enumerate(df1.columns):
        plt.figure(i)
        sns.distplot(df1[col])