How to make a grouped bar chart

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

enter image description here

1 answer

  • answered 2018-09-17 06:07 jezrael

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