Home > front end >  Pandas- plot results in sub results given a column name in a barplot and put value on top of each ba
Pandas- plot results in sub results given a column name in a barplot and put value on top of each ba

Time:03-03

I have the very simple dataset in a csv file

condition,method,error
Normalized,LinfPGD,100
Integer,LinfPGD,100
Print Scan(U),LinfPGD,59
Print Scan(P),LinfPGD,9
Normalized,LinfBasicInteractive,100
Integer,LinfBasicInteractive,100
Print Scan(U),LinfBasicInteractive,69
Print Scan(P),LinfBasicInteractive,9

I would like to plot it in a barplot in pandas, but considering the column "method" as the main result, "condition" as the sub results considering "error" as the value to be plotted.

The closest I got from this was using crosstab

data=pd.read_csv('my_results.csv', sep=",")
pd.crosstab(data['method'], data['condition']).plot.bar(color=('DarkBlue', 'LightBlue', 'Teal'))
plt.tight_layout()
plt.show()

which returns me this

enter image description here

This is not what I want, because crosstab counts the number of each "condition" and I don't want that. All I want is to plot the column "error" for each "condition" considering each "method". I would also like to put the value on top of each bar. How to do that with Pandas/Matplotlib/Seaborn?

CodePudding user response:

You can create a seaborn barplot directly from the original dataframe:

from matplotlib import pyplot as plt
import seaborn as sns
import pandas as pd
from io import StringIO

data_str = '''condition,method,error
Normalized,LinfPGD,100
Integer,LinfPGD,100
Print Scan(U),LinfPGD,59
Print Scan(P),LinfPGD,9
Normalized,LinfBasicInteractive,100
Integer,LinfBasicInteractive,100
Print Scan(U),LinfBasicInteractive,69
Print Scan(P),LinfBasicInteractive,9'''
data = pd.read_csv(StringIO(data_str), delimiter=',')
plt.figure(figsize=(12, 4))
sns.set_style('darkgrid')
ax = sns.barplot(data=data, x='method', y='error', hue='condition', palette=['darkblue', 'lightblue', 'teal'])
for bars in ax.containers:
    ax.bar_label(bars)
ax.margins(y=0.1) # some extra space for the labels
plt.tight_layout()
plt.show()

sns.barplot

CodePudding user response:

I think we can use Pivot to transform the data frame and create a graph.

data = data.pivot(index='method', columns='condition', values='error')
data.plot.bar(color=('DarkBlue', 'LightBlue', 'Teal'))

enter image description here

  • Related