Ian Okeyo
Ian Okeyo

Reputation: 329

Multiple Histograms in Python

I have a dataframe df with below attributes

ID                  X1                       X2
0                  3969518                   24700
1                  8111123                   20000
2                  250000                    987000
3                  10745929                  5000

I m trying to plot a multiple histogram like this

enter image description here

I am using seaborn and matplotlib

x1 = df2[['X1']]
x2 = df2[['X2']]
kwargs = dict(histtype='stepfilled', alpha=0.3, normed=True, bins=20)
plt.hist(x1, **kwargs)
plt.hist(x2, **kwargs);

but i get the below error

TypeError: len() of unsized object

Upvotes: 0

Views: 6865

Answers (1)

Alessandro
Alessandro

Reputation: 875

Quick fix, you need to consider the array not the pandas series:

x1 = df2[['X1']]
x2 = df2[['X2']]
kwargs = dict(histtype='stepfilled', alpha=0.3, normed=True, bins=20)
plt.hist(x1.values, **kwargs)
plt.hist(x2.values, **kwargs);

Alternatively:

x1 = df2[['X1']]
x2 = df2[['X2']]
kwargs = dict(histtype='stepfilled', alpha=0.3, normed=True, bins=20)
plt.hist(data=x1, x='X1', **kwargs)
plt.hist(data=x2, x='X2', **kwargs);

Or even better you don't need to define two new series:

kwargs = dict(histtype='stepfilled', alpha=0.3, normed=True, bins=20)
plt.hist(data=df2, x='X1', **kwargs)
plt.hist(data=df2, x='X2', **kwargs);

Upvotes: 2

Related Questions