熊猫条形图组合在一起

时间:2018-10-26 21:41:39

标签: python pandas plot pivot bar-chart

我从一组基准测试中获得了一个相当大的csv文件,并希望将结果组以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")

这给了我一个条形图,每个条形均等分布。但是如何才能将b1分组在一起而没有任何间隔,然后在b2分组之前有一个空格呢?

即我得到:

enter image description here

但是想要类似的东西:

enter image description here

外翻3条表示每个给定基准的smt1,smt2和smt4加速。

2 个答案:

答案 0 :(得分:2)

第一个更改列名称的sep应该为,而不是;

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=",")

然后我们做pivotplot

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

答案 1 :(得分:1)

您有一些定界符不一致的地方,但是您可以像我一样克服它

%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