如何制作分组条形图

时间:2018-09-17 06:03:17

标签: python pandas plot bar-chart

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

           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

enter image description here

1 个答案:

答案 0 :(得分:2)

我认为您需要先用set_indexunstack重塑:

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