8

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']) 
0

2 Answers 2

12

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])
Sign up to request clarification or add additional context in comments.

2 Comments

What an easy fix! Thanks so much! now I just need to figure out what to do with my missing values and plot everything! All the best!
How can I achieve it in one big figure and have multiple small charts inside it? @mportanceOfBeingErnest
5

Define a function to plot histograms

def histograms_plot(dataframe, features, rows, cols):

fig=plt.figure(figsize=(20,20))
for i, feature in enumerate(features):
    ax=fig.add_subplot(rows,cols,i+1)
    dataframe[feature].hist(bins=20,ax=ax,facecolor='green')
    ax.set_title(feature+" Distribution",color='red')

fig.tight_layout()  
plt.show()

histograms_plot(df,df.columns,6,3)

1 Comment

Thanks. How do I scale the subplots so that the x axis is the same?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.