Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make a seaborn distplot for each column in a pandas dataframe?

I 'm using Seaborn in a Jupyter notebook to plot histograms like this:

import numpy as np
import pandas as pd
from pandas import DataFrame
import matplotlib.pyplot as plt
import seaborn as sns

%matplotlib inline

df = pd.read_csv('CTG.csv', sep=',')

sns.distplot(df['LBE'])

I have an array of columns with values that I want to plot histogram for and I tried plotting a histogram for each of them:

continous = ['b', 'e', 'LBE', 'LB', 'AC']

for column in continous:
    sns.distplot(df[column])

And I get this result - only one plot with (presumably) all histograms:

enter image description here

My desired result is multiple histograms that looks like this (one for each variable):

enter image description here

How can I do this?

like image 471
Marin Leontenko Avatar asked Feb 03 '20 19:02

Marin Leontenko


1 Answers

Insert plt.figure() before each call to sns.distplot() .

Here's an example with plt.figure():

enter image description here

Here's an example without plt.figure():

enter image description here

Complete code:

# imports
import numpy as np
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
plt.rcParams['figure.figsize'] = [6, 2]

%matplotlib inline

# sample time series data
np.random.seed(123)
df = pd.DataFrame(np.random.randint(-10,12,size=(300, 4)), columns=list('ABCD'))
datelist = pd.date_range(pd.datetime(2014, 7, 1).strftime('%Y-%m-%d'), periods=300).tolist()
df['dates'] = datelist 
df = df.set_index(['dates'])
df.index = pd.to_datetime(df.index)
df.iloc[0]=0
df=df.cumsum()

# create distplots
for column in df.columns:
    plt.figure()             # <==================== here!
    sns.distplot(df[column])
like image 118
vestland Avatar answered Sep 22 '22 13:09

vestland