问题描述
我正在尝试绘制一个图表,以显示男性和女性针对不同年龄组的特定活动的不同行为.
所以,如果年龄组是:['1-10','11-20','21-30'...]我想为每个年龄组(每个年龄范围将是 x 轴上的标签)绘制直方图,用于男性和女性进行活动.我知道如何在一个图中绘制两个直方图,但我不知道如何并行绘制多个直方图,尤其是当每个直方图都针对给定的 x 标签时.
有人可以帮忙吗?
我不确定您是否希望在一个图中两个直方图,但如果我生成一些随机数据:
将 numpy 导入为 np导入 matplotlib.pyplot 作为 pltage = ['{}-{}'.format(i*10, (i+1)*10) for i in range(10)]男性 = np.random.randint(0,100,10)女性 = np.random.randint(0,100,10)
如果您需要根据某些数据手动创建直方图,您可以使用 numpy
而不是 matplotlib 直方图(male_data
和 female_data
是什么你会插入 plt.hist()
):
bins = [i*10 for i in range(11)] # = [0,10,20,30,40,50,60,70,80,90,100]男性,_ = np.histogram(male_data, bins=bins)女性,_ = np.histogram(female_data, bins=bins)
然后将其绘制为 bar
图(我从
或者你想用共享 y 轴来分隔图?
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')对于 i in (ax1, ax2):getattr(i, 'set_xticks')(np.arange(10))getattr(i, 'set_xticklabels')(age)getattr(i, 'set_xlabel')('年龄范围')getattr(i, 'set_ylabel')('做这件事的人(百分比)')getattr(i, 'set_xlim')(-0.5,9.5)plt.show()
在第二个示例中,您可能需要减小文本大小,以便正确显示年龄范围...
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?
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()
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()
In the second example you might need to decrease the text size so that the age ranges are properly shown...
这篇关于多个直方图,每个直方图对应一个 x 轴的标签,在同一个图 matplotlib 上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!