-1

I am trying to plot a graph to show different behaviors by males and females with regards to a certain activity, for different age-groups.

So, if the age groups are: ['1-10','11-20','21-30'...] I would like a to plot a histogram for each age-group (every age-range would be a label on x-axis), for males and females doing the activity. I know how to plot two histograms together in one graph, but I don't know how to plot many in parallel, especially when each histogram is for a given x-label.

Could someone please help?

Ashish Kumar
  • 347
  • 4
  • 12

1 Answers1

1

I'm not sure if you want both of the histograms in one plot, but if I generate some random data:

import numpy as np
import matplotlib.pyplot as plt
age = ['{}-{}'.format(i*10, (i+1)*10) for i in range(10)]
males = np.random.randint(0,100,10)
females = np.random.randint(0,100,10)

If you need to create your histograms manually from some data you can use numpy instead of matplotlib histogram (the male_data and female_data is what you would have inserted into plt.hist()):

bins = [i*10 for i in range(11)] # = [0,10,20,30,40,50,60,70,80,90,100]
males , _ = np.histogram(male_data, bins=bins)
females , _ = np.histogram(female_data, bins=bins)

and then plot it as bar plot (I've adapted some of it from the matplotlib examples page)I get something that might be what you want:

fig, ax = plt.subplots()
# Normalize the counts by dividing it by the sum:
ax.bar(np.arange(10)-0.15, males/np.sum(males), width=0.1, color='b', label='male')
ax.bar(np.arange(10)+0.05, females/np.sum(females), width=0.1, color='r', label='female')
ax.set_xticks(np.arange(10))
ax.set_xticklabels(age)
ax.legend()
ax.set_xlim(-0.5,9.5)
plt.show()

enter image description here

or do you want to seperate plots with a shares y-axis?

fig, (ax1, ax2) = plt.subplots(1, 2, sharey=True)
ax1.bar(np.arange(10)-0.3, 100*males/np.sum(males), width=0.6, color='b', label='male')
ax2.bar(np.arange(10)-0.3, 100*females/np.sum(females), width=0.6, color='r', label='female')
for i in (ax1, ax2):
    getattr(i, 'set_xticks')(np.arange(10))
    getattr(i, 'set_xticklabels')(age)
    getattr(i, 'set_xlabel')('Age range')
    getattr(i, 'set_ylabel')('People doing it (in percent)')
    getattr(i, 'set_xlim')(-0.5,9.5)
plt.show()

enter image description here

In the second example you might need to decrease the text size so that the age ranges are properly shown...

MSeifert
  • 145,886
  • 38
  • 333
  • 352
  • Thanks for the answer. I understand the bar-plot, its the histogram that troubles me, since it needs many bins for each gender for every age-group. Yes, the y-axis would be shared. I think I should use subplots with histograms for each age-group separately and show it in one graph. – Ashish Kumar Mar 14 '16 at 22:43
  • I don't quite understand it. Is the problem that you don't know how to find the values for the histogram? Or something else? Could you show me a plot how it should look like in the end? Because if that answer doesn't answer your question I might need to guess what you are looking for. :-( – MSeifert Mar 14 '16 at 22:51