Plotting Histograms Against Classes In Pandas / Matplotlib
Is there a idiomatic way to plot the histogram of a feature for two classes? In pandas, I basically want df.feature[df.class == 0].hist() df.feature[df.class == 1].hist() To be in
Solution 1:
How about df.groupby("class").feature.hist()
? To see overlapping distributions you'll probably need to pass alpha=0.4
to hist()
. Alternatively, I'd be tempted to use a kernel density estimate instead of a histogram with df.groupby("class").feature.plot(kind='kde')
.
As an example, I plotted the iris dataset's classes using:
iris.groupby("Name").PetalWidth.plot(kind='kde', ax=axs[1])
iris.groupby("Name").PetalWidth.hist(alpha=0.4, ax=axs[0])
Post a Comment for "Plotting Histograms Against Classes In Pandas / Matplotlib"