Cannot create a formatted pandas multindex barplot?

I have a multiindex pandas dataframe that looks like this:

            cis-aconitate  cis-aconitate +1  ...  cis-aconitate +5  cis-aconitate +6
     GROUP                                   ...                                    
mean ADE         1.481639          0.696184  ...          0.193380          0.018597
     VEH         1.000000          1.000000  ...          1.000000          1.000000
std  ADE         0.307211          0.209418  ...          0.114939          0.020461
     VEH         0.573162          0.412895  ...          0.384928          0.752999

With indexing like this:

MultiIndex([('mean', 'ADE'),
            ('mean', 'VEH'),
            ( 'std', 'ADE'),
            ( 'std', 'VEH')],
           names=[None, 'GROUP'])

I am trying to create a barplot of the mean values on the y-axis. Using df.unstack() and then df.plot.bar() I can get this: enter image description here But what I would ultimately like is to have the means only, with 'cis-aconitate, cis-aconitate +1 ...' across the x-axis, grouped into ADE and VEH (one color each) and then 'std' values used for the error bars. This is proving surprisingly tricky. Can anyone help? Thanks in advance!

1 answer

  • answered 2021-10-12 17:39 danpl

    To avoid doing a lot of transformations to the data frame and based on this example, the solution I found was to split the means from the errors using cross section.

    import pandas as pd
    
    # Example data
    index = pd.MultiIndex.from_tuples([('mean', 'ADE'), ('mean', 'VEH'), ( 'std', 'ADE'), ('std', 'VEH')], names=[None, 'GROUP'])
    
    df = pd.DataFrame({'cis-aconitate': [1.5, 1, 0.3, 0.2], 'cis-aconitate +1': [0.7, 1, 0.2, 0.4]}, index=index)
    df
    
    # Split dataframe into means and errors
    means = df.xs('mean').transpose()
    errors = df.xs('std').transpose()
    
    means.plot.bar(yerr=errors)
    
    

How many English words
do you know?
Test your English vocabulary size, and measure
how many words do you know
Online Test
Powered by Examplum