Reputation: 53
Is there a way to use the pandas.DataFrame.hist() function to create a grid of histograms, where each graph contains a specific variable, as well as a common variable in all of them? For example, in the following DataFrame, I'd like to have depth to be graphed with length in one of them, and with width in the other.
import pandas as pd
df = pd.DataFrame({
'length': [1.5, 0.5, 1.2, 0.9, 3, 0.4, 0.2],
'width': [0.7, 0.2, 0.15, 0.2, 1.1, 0.2, 0.1],
'depth': [1, 0.4, 0.5, 1.1, 2, 0.1, 0.9]
}, index= ['pig', 'rabbit', 'duck', 'chicken', 'horse', 'cow', 'dog'])
hist = df.hist(bins=3)
Upvotes: 1
Views: 1272
Reputation: 150805
I think you need to re-draw the histogram again:
# draw the histograms for other columns except `depth`
axes = df.drop('depth',axis=1).hist(bins=3)
# draw histogram for `depth` in each of the subplots
for ax in axes.flat:
df['depth'].hist(bins=3, ax=ax, alpha=0.7)
Output:
Upvotes: 1