本文介绍了如何制作分组条形图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有此数据框,并希望将其绘制为分组的条形图.我检查了这个问题(分组的条形图熊猫),它有一个具有其值的组,而不是列.如何将带有栏的条形图分组?

I have this dataframe and want to plot it as a grouped bar chart. I checked this question (Grouped Bar graph Pandas), It has a group with its value, not columns. How can I have a grouped bar chart with column?

           num_thread   num_iter       time
category
 ORIGINAL           1  100000000  1360.0577
 ORIGINAL           1  200000000  2731.8207
 ORIGINAL           1  400000000  5440.8003
 OMP                2  100000000   692.5336
 OMP                2  200000000  1398.5305
 OMP                2  400000000  2765.7757
 OMP                4  100000000   362.1932
 OMP                4  200000000   724.6331
 OMP                4  400000000  1447.0628
 OMP                8  100000000   193.0222
 OMP                8  200000000   382.7540
 OMP                8  400000000   759.3889
 OMP               16  100000000   102.5276
 OMP               16  200000000   214.6385
 OMP               16  400000000   450.7183
 PTHREAD            2  100000000   697.3113
 PTHREAD            2  200000000  1388.6210
 PTHREAD            2  400000000  2779.8507
 PTHREAD            4  100000000   363.9816
 PTHREAD            4  200000000   721.6508
 PTHREAD            4  400000000  1432.9843
 PTHREAD            8  100000000   189.8591
 PTHREAD            8  200000000   379.8860
 PTHREAD            8  400000000   764.2684
 PTHREAD           16  100000000   124.2015
 PTHREAD           16  200000000   238.9460
 PTHREAD           16  400000000   478.0660

推荐答案

我认为您需要通过 set_index unstack 首先:

I think you need reshape by set_index with unstack first:

df1 = df.set_index(['category', 'num_thread', 'num_iter'])['time'].unstack()
#python 3.6+
df1.index = [f'{i}{j}' for i, j in df1.index]
#python under 3.6
#df1.index = ['{}{}'.format(i, j) for i, j in df1.index]
print (df1)
num_iter   100000000  200000000  400000000
OMP2        692.5336  1398.5305  2765.7757
OMP4        362.1932   724.6331  1447.0628
OMP8        193.0222   382.7540   759.3889
OMP16       102.5276   214.6385   450.7183
ORIGINAL1  1360.0577  2731.8207  5440.8003
PTHREAD2    697.3113  1388.6210  2779.8507
PTHREAD4    363.9816   721.6508  1432.9843
PTHREAD8    189.8591   379.8860   764.2684
PTHREAD16   124.2015   238.9460   478.0660

df1.plot.bar()

这篇关于如何制作分组条形图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-30 05:14