Replicate pivot table from Excel with three "rows" (groups)

45 Views Asked by At

I'm trying to replicate a plot in python (pandas or matplotlib), but can't seem to get it.

The data looks like this - I have multiple types of plants, treated with different concentrations of a compound and either exposed or not to stress.

Excel gives this plot, which works, but I can't put error bars on it. I will have to add error bars (SEM).

I did some plots with data.groupby(['Plant', 'Concentration', 'Stress']).mean().unstack().plot.bar() but they are not very pretty. Mostly the grouping on the bottom is not good. I also tried pivot_table, but the results were similar.

1

There are 1 best solutions below

0
JohanC On

Such a plot can be created with Seaborn (a library extending matplotlib, for statistical visualization). Seaborn automatically adds error bars (by default "ci": 95% confidence interval for the mean, calculated via bootstrapping).

import matplotlib.pyplot as plt
import seaborn as sns
import pandas as pd
import numpy as np

N = 120
df = pd.DataFrame({'Plant': np.random.choice(['Pepper', 'Tomato'], N),
                   'Concentration': np.random.randint(0, 3, N),
                   'Parameter': np.random.randint(5, 25, N),
                   'Stress': np.random.choice(['Yes', 'No'], N), })
sns.set_style('whitegrid')
sns.catplot(df, kind='bar', x='Concentration', y='Parameter', hue='Stress', col='Plant')

plt.show()

seaborn barplot with error bars