janfrode
janfrode

Reputation: 348

Pandas bar charts groups together

I have a quite large csv file from a set of benchmarks, and would like to plot groups of results together in 3s. F.ex:

%matplotlib inline
from matplotlib import pyplot as plt
import pandas as pd
import numpy as np
from io import StringIO

TESTDATA = StringIO("""benchmark,smt,speedup
   b1, smt1, 100
   b1, smt2, 111
   b1, smt4, 118
   b2, smt1, 100
   b2, smt2, 108
   b2, smt4, 109
    """)

df = pd.read_csv(TESTDATA, sep=",")

df.plot(kind="bar")

This gives me a bar-plot with every bar evenly spaced. But how can I make results from b1 grouped together without any spacing, then have a space before the the b2 is grouped together?

I.e. I get:

enter image description here

But want something like:

enter image description here

with evert 3 bars representing the speedup for smt1,smt2 and smt4 for each given benchmark.

Upvotes: 0

Views: 45

Answers (2)

Kamil Niski
Kamil Niski

Reputation: 4765

You hve some delimiter inconsistency but you can overcome it as me

%matplotlib inline
from matplotlib import pyplot as plt
import pandas as pd
import numpy as np
from io import StringIO

TESTDATA = StringIO("""benchmark;smt;speedup
   b1, smt1, 100
   b1, smt2, 111
   b1, smt4, 118
   b2, smt1, 100
   b2, smt2, 108
   b2, smt4, 109
    """)

df = pd.read_csv(TESTDATA, sep=",", skiprows=1, names=['benchmark', 'smt', 'speedup'])

df.pivot(index='benchmark', columns='smt').plot(kind='bar')

enter image description here

Upvotes: 1

BENY
BENY

Reputation: 323226

1st change your sep in columns name it should be , not ;

TESTDATA = StringIO("""benchmark,smt,speedup
   b1, smt1, 100
   b1, smt2, 111
   b1, smt4, 118
   b2, smt1, 100
   b2, smt2, 108
   b2, smt4, 109
    """)
df = pd.read_csv(TESTDATA, sep=",")

Then we do pivot and plot

df.pivot(*df.columns)
Out[446]: 
smt         smt1   smt2   smt4
benchmark                     
   b1        100    111    118
   b2        100    108    109
df.pivot(*df.columns).plot(kind='bar')

enter image description here

Upvotes: 2

Related Questions