绘制分组的pandas数据框图

4
我是一名有用的助手,可以为您翻译文本。

我花了几个小时寻找答案,但似乎找不到。

长话短说,我有一个数据框。以下代码将生成相关的数据框(尽管使用随机数字进行匿名化):

variable1 = ["Attribute 1","Attribute 1","Attribute 1","Attribute 1","Attribute 1","Attribute 1","Attribute 2","Attribute 2",
         "Attribute 2","Attribute 2","Attribute 2","Attribute 2","Attribute 3","Attribute 3","Attribute 3","Attribute 3",
         "Attribute 3","Attribute 3","Attribute 4","Attribute 4","Attribute 4","Attribute 4","Attribute 4","Attribute 4",
         "Attribute 5","Attribute 5","Attribute 5","Attribute 5","Attribute 5","Attribute 5"]


variable2 = ["Property1","Property2","Property3","Property4","Property5","Property6","Property1","Property2","Property3",
         "Property4","Property5","Property6","Property1","Property2","Property3",
         "Property4","Property5","Property6","Property1","Property2","Property3","Property4",
         "Property5","Property6","Property1","Property2","Property3","Property4","Property5","Property6"]

number = [93,224,192,253,186,266,296,100,135,169,373,108,211,194,164,375,211,71,120,334,59,164,348,50,249,18,251,343,172,41]

bar = pd.DataFrame({"variable1":variable1, "variable2":variable2, "number":number})

bar_grouped = bar.groupby(["variable1","variable2"]).sum()

结果应该看起来像:

enter image description here

第二个:

enter image description here

我一直在尝试使用条形图绘制它们,将属性作为组,不同的属性作为条形。类似于这样(虽然是手动在Excel中绘制的)。我更喜欢在分组数据框中进行操作,以便能够在不需要每次重置索引的情况下使用不同的分组进行绘制。

enter image description here

我希望这很清楚。

非常感谢任何关于此的帮助。

谢谢!:)


1
尝试使用 bar_grouped['number'].unstack(0).plot(kind='bar') - Chris Adams
3个回答

4

我建议不需要创建groupby结果(因为您没有聚合任何内容)。这是一个pivot


bar.pivot('variable2', 'variable1', 'number').plot(kind='bar')

plt.tight_layout()
plt.show()

enter image description here


如果需要聚合,您仍然可以从您的bar开始,并使用pivot_table
bar.pivot_table(index='variable2', columns='variable1', values='number', aggfunc='sum')

1
聚合是必需的,因为这只是一个更大的DataFrame的一部分,其中有更多的值可以最终进行聚合,所以感谢您! :) - Alex Rpd

3

首先使用 unstack

bar_grouped['number'].unstack(0).plot(kind='bar')

[输出]

enter image description here


2
以下代码可以实现你想要的功能:
import numpy as np
import matplotlib.pyplot as plt

# set width of bar
barWidth = 0.25
f = plt.figure(figsize=(15,8))

bars={}
bar_pos={}
for i,proprty in enumerate(bar_grouped.unstack().columns.droplevel(0).tolist()):
    bars[i] = bar_grouped.unstack()['number',proprty].tolist()
    if(i==0):
        bar_pos[i]=2*np.arange(len(bars1))
    else:
        bar_pos[i]=[x + barWidth for x in bar_pos[i-1]] 
    plt.bar(bar_pos[i], bars[i], width=barWidth, edgecolor='white', label=proprty, figure=f)

# Add xticks on the middle of the group bars
plt.xlabel('group', fontweight='bold')
plt.xticks([2*r + 2*barWidth for r in range(len(bars[0]))], bar_grouped.unstack().index.tolist())
# plt.figure(figsize=(10,5))

# Create legend & Show graphic
plt.legend(loc=0)
plt.show()

我从这里找到了解决方案,并进行了修改以适应您的需求。希望这可以帮到您!"最初的回答"

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接