Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/301.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 绘制分组数据帧_Python_Pandas_Matplotlib_Bar Chart - Fatal编程技术网

Python 绘制分组数据帧

Python 绘制分组数据帧,python,pandas,matplotlib,bar-chart,Python,Pandas,Matplotlib,Bar Chart,我花了几个小时寻找答案,但似乎找不到答案 长话短说,我有一个数据帧。以下代码将生成有问题的数据帧(尽管使用随机数匿名): 结果应该是: 第二个: 我一直在尝试用条形图绘制它们,并将属性作为组,将不同的属性作为条形图。与此类似(但手动在Excel中绘制)。我更喜欢在分组数据场中进行,这样就可以使用不同的分组进行绘图,而无需每次重置索引 我希望这是清楚的 非常感谢在这方面的任何帮助 谢谢!:) 首先使用: [外] 我不会费心创建您的groupby结果(因为您没有聚合任何内容)。这是一个pivo

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

长话短说,我有一个数据帧。以下代码将生成有问题的数据帧(尽管使用随机数匿名):

结果应该是:

第二个:

我一直在尝试用条形图绘制它们,并将属性作为组,将不同的属性作为条形图。与此类似(但手动在Excel中绘制)。我更喜欢在分组数据场中进行,这样就可以使用不同的分组进行绘图,而无需每次重置索引

我希望这是清楚的

非常感谢在这方面的任何帮助

谢谢!:)

首先使用:

[外]


我不会费心创建您的
groupby
结果(因为您没有聚合任何内容)。这是一个
pivot




如果需要聚合,您仍然可以从
栏开始
并使用
pivot\u表

bar.pivot_table(index='variable2', columns='variable1', values='number', aggfunc='sum')

以下代码将执行您试图建立的操作:

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()

我从中获取了解决方案,并根据您的需要对其进行了修改。希望这有帮助

尝试
bar\u grouped['number']。取消堆栈(0)。需要绘图(kind='bar')
聚合,因为这只是一个较大数据帧的一部分,最终可以聚合更多值,所以感谢!)
bar.pivot('variable2', 'variable1', 'number').plot(kind='bar')

plt.tight_layout()
plt.show()
bar.pivot_table(index='variable2', columns='variable1', values='number', aggfunc='sum')
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()