Matt
Matt

Reputation: 813

Superimposition of histogram and density in Pandas/Matplotlib in Python

I've got a Pandas dataframe named clean which contains a column v for which I would like to draw a histogram and superimpose a density plot. I know I can plot one under the other this way:

import pandas as pd
import matplotlib.pyplot as plt

Maxv=200

plt.subplot(211)
plt.hist(clean['v'],bins=40, range=(0, Maxv), color='g')
plt.ylabel("Number")

plt.subplot(212)
ax=clean['v'].plot(kind='density')
ax.set_xlim(0, Maxv)
plt.xlabel("Orbital velocity (km/s)")
ax.get_yaxis().set_visible(False)

enter image description here

But when I try to superimpose, y scales doesn't match (and I loose y axis ticks and labels):

yhist, xhist, _hist = plt.hist(clean['v'],bins=40, range=(0, Maxv), color='g')
plt.ylabel("Number")

ax=clean['v'].plot(kind='density') #I would like to insert here a normalization to max(yhist)/max(ax)
ax.set_xlim(0, Maxv)
plt.xlabel("Orbital velocity (km/s)")
ax.get_yaxis().set_visible(False)

enter image description here

Some hint? (Additional question: how can I change the width of density smoothing?)

Upvotes: 5

Views: 2828

Answers (3)

nanogoats
nanogoats

Reputation: 159

Seaborn makes this easy

import seaborn as sns
sns.distplot(df['numeric_column'],bins=25)

enter image description here

Upvotes: 1

IanS
IanS

Reputation: 16251

Based on your code, this should work:

ax = clean.v.plot(kind='hist', bins=40, normed=True)
clean.v.plot(kind='kde', ax=ax, secondary_y=True)
ax.set(xlim=[0, Maxv])

You might not even need the secondary_y anymore.

Upvotes: 5

Matt
Matt

Reputation: 813

No I try this:

ax = clean.v.plot(kind='hist', bins=40, range=(0, Maxv))
clean.v.plot(kind='kde', ax=ax, secondary_y=True)

But the range part doesn't work, and ther's still the left y-axis problem

enter image description here

Upvotes: 1

Related Questions