How to create a grouped bar plot
Pandas will show grouped bars by columns. Entries in each row but different columns will constitute a group in the resulting plot. Hence you need to "reshape" your dataframe to have the "group" as columns.In this case you can pivot like
df.pivot("column", "group", "val")
producing
group g1 g2column c1 10 8c2 12 10c3 13 12
Plotting this will result in a grouped bar chart.
import pandas as pdimport matplotlib.pyplot as pltdf = pd.DataFrame([['g1','c1',10],['g1','c2',12],['g1','c3',13],['g2','c1',8], ['g2','c2',10],['g2','c3',12]],columns=['group','column','val'])df.pivot("column", "group", "val").plot(kind='bar')plt.show()
You can simply do this using the code given below:
import pandas as pdimport matplotlib.pyplot as pltpositive_values = [20, 17.5, 40]negative_values = [15, 8, 70]index = ['Precision', 'Recall', 'f1-score',]df = pd.DataFrame({'Positive Values': positive_values, 'Negative Values': negative_values}, index=index)ax = df.plot.bar(rot=0, color={"Positive Values": "green", "Negative Values": "red"})
Output:
- Given a dataframe of long (tidy) data, as shown in the OP, an implementation that does not require transforming the dataframe is to use
seaborn.barplot
with thehue
parameter. seaborn
is a high-level API formatplotlib
- Tested with
seaborn 0.11.1
andmatplotlib 3.4.2
import pandas as pdimport seaborn as sns# the sample dataframe from the OPdf = pd.DataFrame([['g1', 'c1', 10], ['g1', 'c2', 12], ['g1', 'c3', 13], ['g2', 'c1', 8], ['g2', 'c2', 10], ['g2', 'c3', 12]], columns=['group', 'column', 'val'])# plot with seaborn barplotsns.barplot(data=df, x='column', y='val', hue='group')