Matplotlib PyPlot堆叠直方图-在每个栏中堆叠不同的属性

伴侣维塔

我有以下代码来绘制有关数据库中主题的一些直方图:

import matplotlib.pyplot as plt

attr_info = {
    'Gender': ['m', 'f', 'm', 'm', 'f', 'm', 'm', 'f', 'm', 'f'],
    'Age': [9, 43, 234, 23, 2, 95, 32, 63, 58, 42],
    'Smoker': ['y', 'n', 'y', 'y', 'n', 'n', 'n', 'n', 'y', 'y']
}
bin_info = {key: None for key in attr_info}
bin_info['Age'] = 10

for name, a_info in attr_info.items():
    plt.figure(num=name)
    counts, bins, _ = plt.hist(a_info, bins=bin_info[name], color='blue', edgecolor='black')

    plt.margins(0)
    plt.title(name)
    plt.xlabel(name)
    plt.ylabel("# Subjects")
    plt.yticks(range(0, 11, 2))
    plt.grid(axis='y')
    plt.tight_layout(pad=0)

    plt.show()

该代码有效,但是它在单独的直方图中绘制每个属性的分布。我想要实现的是这样的:

堆积直方图

我知道plt.hist有一个stacked参数,但这似乎是为稍微不同的用途而设计的,即您将相同的属性彼此堆叠在不同的主题类型上。例如,您可以绘制一个直方图,其中每个整个条形图都代表某个年龄范围,并且条形图本身将是一堆吸烟者使用一种颜色,而不吸烟者使用另一种颜色。

我还无法弄清楚如何使用它来堆叠(并正确标记为图像中的)不同的属性,每个属性相互叠加。

弗朗西斯卡·孔查·拉米雷斯

您需要稍微处理一下数据,但是没有可以做到pandas另外,您想要的是堆叠的条形图,而不是直方图:

import matplotlib.pyplot as plt

attr_info = {
'Gender': ['m', 'f', 'm', 'm', 'f', 'm', 'm', 'f', 'm', 'f'],
'Age': [9, 43, 234, 23, 2, 95, 32, 63, 58, 42],
'Smoker': ['y', 'n', 'y', 'y', 'n', 'n', 'n', 'n', 'y', 'y']
}

# Filter your data for each bar section that you want
ages_0_10 = [x for x in attr_info['Age'] if x < 10]
ages_10_40 = [x for x in attr_info['Age'] if x >= 10 and x < 40]
ages_40p = [x for x in attr_info['Age'] if x > 40]

gender_m = [x for x in attr_info['Gender'] if 'm' in x]
gender_f = [x for x in attr_info['Gender'] if 'f' in x]

smoker_y = [x for x in attr_info['Smoker'] if 'y' in x]
smoker_n = [x for x in attr_info['Smoker'] if 'n' in x]

# Locations for each bin (you can move them around)
locs = [0, 1, 2]

# I'm going to plot the Ages bin separate than the Smokers and Gender ones, 
# since Age has 3 stacked bars and the other have just 2 each
plt.bar(locs[0], len(ages_0_10), width=0.5)  # This is the bottom bar

# Second stacked bar, note the bottom variable assigned to the previous bar
plt.bar(locs[0], len(ages_10_40), bottom=len(ages_0_10), width=0.5) 

# Same as before but now bottom is the 2 previous bars    
plt.bar(locs[0], len(ages_40p), bottom=len(ages_0_10) + len(ages_10_40), width=0.5)

# Add labels, play around with the locations
#plt.text(x, y, text)
plt.text(locs[0], len(ages_0_10) / 2, r'$<10$')
plt.text(locs[0], len(ages_0_10) + 1, r'$[10, 40]$')
plt.text(locs[0], len(ages_0_10) + 5, r'$>40$')


# Define the top bars and bottom bars for the Gender and Smokers stack
# In both cases is just 2 stacked bars,
# so we can use a list for this instead of doing it separate as for Age
tops = [len(gender_m), len(smoker_y)]
bottoms = [len(gender_f), len(smoker_n)]

plt.bar(locs[1:], bottoms, width=0.5)
plt.bar(locs[1:], tops, bottom=bottoms, width=0.5)

# Labels again
# Gender
plt.text(locs[1], len(gender_m) / 2, 'm')
plt.text(locs[1], len(gender_m) + 2, 'f')

# Smokers
plt.text(locs[2], len(smoker_y) / 2, 'y')
plt.text(locs[2], len(smoker_n) + 2, 'n')

# Set tick labels
plt.xticks(locs, ('Age', 'Gender', 'Smoker'))
plt.show()

结果: 在此处输入图片说明

查看pyplot.bar和此示例文档

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

使用matplotlib在python中绘制堆叠的直方图

来自分类Dev

如何在matplotlib中并排绘制堆叠的直方图?

来自分类Dev

Matplotlib,从三个不等长的数组创建堆叠的直方图

来自分类Dev

直方图条不能在python中使用matplotlib堆叠

来自分类Dev

直方图中的 Matplotlib PyPlot 线

来自分类Dev

Pandas Python 中的堆叠直方图

来自分类Dev

堆叠组件的直方图

来自分类Dev

gnuplot堆叠直方图重叠

来自分类Dev

Python堆叠直方图

来自分类Dev

Python堆叠直方图分组数据

来自分类Dev

Gnuplot:堆叠直方图条的颜色

来自分类Dev

gnuplot中的堆叠自动合并直方图

来自分类Dev

gnuplot中的堆叠自动合并直方图

来自分类Dev

matplotlib中的多步直方图

来自分类Dev

如何在堆叠的栏中显示每个日期范围的多个堆叠列

来自分类Dev

如何在堆叠的栏中显示每个日期范围的多个堆叠列

来自分类Dev

Python:如何使用Plotly堆叠或叠加直方图

来自分类Dev

如何在kibana上制作堆叠的直方图?

来自分类Dev

直方图Gnuplot混合成簇/堆叠

来自分类Dev

Seaborn:从元组列表创建堆叠的直方图

来自分类Dev

黑白阴影图案的Gnuplot行堆叠直方图

来自分类Dev

熊猫/ matplotlib中的堆叠式交错水平条形图

来自分类Dev

连接matplotlib中的多个直方图

来自分类Dev

通过Python的Matplotlib彼此堆叠3条

来自分类Dev

Matplotlib-图形相互堆叠

来自分类Dev

使用Matplotlib.image堆叠图像

来自分类Dev

如何在Python中的单个图形中堆叠多个直方图?

来自分类Dev

在R中为堆叠的直方图创建条形边框

来自分类Dev

在matplotlib.pyplot中缩放?