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']) 
Thomas Quick
  • 127
  • 1
  • 2
  • 7

2 Answers2

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])
Trenton McKinney
  • 56,955
  • 33
  • 144
  • 158
ImportanceOfBeingErnest
  • 321,279
  • 53
  • 665
  • 712
  • 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! – Thomas Quick Jun 09 '18 at 15:29
  • How can I achieve it in one big figure and have multiple small charts inside it? @mportanceOfBeingErnest – TapanHP Feb 26 '19 at 10:11
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)

valkyrie55
  • 355
  • 4
  • 10