Reputation: 31
I have two separate sets of data using pandas:
>>> suicides_sex = suicides_russia.groupby("sex")["suicides_no"].sum()
>>> suicides_sex
sex
female 214330
male 995412
&
>>> suicides_age = suicides_russia.groupby("age")
>>> ["suicides_no"].sum().sort_values()
>>> suicides_age
age
5-14 years 8840
75+ years 74211
15-24 years 148611
25-34 years 231187
55-74 years 267753
35-54 years 479140
I want to learn how to create either a double bar chart using matplotlib
or two separate bar charts where I can separate each age group by gender.
How can I combine both sets of data to create either a single bar chart with double columns or two separate bar charts for each gender?
Upvotes: 0
Views: 77
Reputation: 598
You can use boolean masks to separate the data and then group by age as you did.
import matplotlib.pyplot as plt
suicide_male = suicide_russia.loc[suicide_russia['sex']=='male', :]
# now you basically have the same dataframe but for male only
suicide_male_age = suicides_male.groupby("age")["suicides_no"].sum()
plt.bar(height=suicide_male_age.values, x=np.arange(suicide_male_age.index))
plt.xticks(labels=suicide_male_age.index)
plt.show()
Then you can repeat the same for female. That is probably not the most efficient way of doing it, but it works.
Also, I assumed the 'age' column values are strings, so I put np.arange
as x positions of the bars and the values themselves as xticks.
Hope it helps!
Upvotes: 1