ggplot geom_histogram behaves differently between Python and R

Viewed 316

I am trying to do some exploratory data analysis and I have a data frame with an integer age column and a "category" column. Making a histogram of the age is easy enough. What I want to do is maintain this age histogram but color the bars based on the categorical variables.

import numpy as np 
import pandas as pd
ageSeries.hist(bins=np.arange(-0.5, 116.5, 1))

I was able to do what I wanted easily in one line with ggplot2 in R

ggplot(data, aes(x=Age, fill=Category)) + geom_histogram(binwidth = 1)

ggplot histogram in R that properly displays totals

I wasn't able to find a good solution in Python, but then I realized there was a ggplot2 library for Python and installed it. I tried to do the same ggplot command...

ggplot(data, aes(x="Age", fill="Category")) + geom_histogram(binwidth = 1)

ggplot histogram in Python, data are overlaid separately and not stacked

Looking at these results we can see that the different categories are treated as different series and and overlaid rather than stacked. I don't want to mess around with transperancies, and I still want to maintain the overall distribution of the the population.

Is this something I can fix with a parameter in the ggplot call, or is there a straightforward way to do this in Python at all without doing a bunch of extra dataframe manipulations?

0 Answers
Related