2016-01-13 34 views
3

我有以下数据框:绘制errorbar均值和std分组后

    mean  std 
insert quality      
0.0 good  0.009905 0.003662 
0.1 good  0.450190 0.281895 
     poor  0.376818 0.306806 
0.2 good  0.801856 0.243288 
     poor  0.643859 0.322378 
0.3 good  0.833235 0.172025 
     poor  0.698972 0.263266 
0.4 good  0.842288 0.141925 
     poor  0.706708 0.241269 
0.5 good  0.853634 0.118604 
     poor  0.685716 0.208073 
0.6 good  0.845496 0.118609 
     poor  0.675907 0.207755 
0.7 good  0.826335 0.133820 
     poor  0.656934 0.222823 
0.8 good  0.829707 0.130154 
     poor  0.627111 0.213046 
0.9 good  0.816636 0.137371 
     poor  0.589331 0.232756 
1.0 good  0.801211 0.147864 
     poor  0.554589 0.245867 

,我应该怎么做,如果想使用作为X轴的索引列"Insert"和差异绘制两条曲线(点+错误)两条曲线按"Quality" [好,差]?它们也应该具有不同的颜色。

我有点卡住了,我制作了每一种情节,除了我需要的情节。

+0

你自己想一个吧平均值的标准差,标准偏差作为错误的附近?或者是一个线条图,用'std'作为阴影区域?你想要的输出是什么? –

+0

的意思是一个点e std是一个垂直线(就像matplotlib.errorbar) –

回答

7

您可以遍历df.groupby('quality')中的组并在每个组上呼叫group.plot

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

df = pd.DataFrame({ 
    'insert': [0.0, 0.1, 0.1, 0.2, 0.2, 0.3, 0.3, 0.4, 0.4, 0.5, 0.5, 0.6, 0.6, 
    0.7, 0.7, 0.8, 0.8, 0.9, 0.9, 1.0, 1.0], 
    'mean': [0.009905, 0.45019, 0.376818, 0.801856, 0.643859, 0.833235, 
    0.698972, 0.842288, 0.706708, 0.853634, 0.685716, 0.845496, 0.675907, 
    0.826335, 0.656934, 0.829707, 0.627111, 0.816636, 0.589331, 0.801211, 
    0.554589], 
    'quality': ['good', 'good', 'poor', 'good', 'poor', 'good', 'poor', 'good', 
    'poor', 'good', 'poor', 'good', 'poor', 'good', 'poor', 'good', 'poor', 
    'good', 'poor', 'good', 'poor'], 
    'std': [0.003662, 0.281895, 0.306806, 0.243288, 0.322378, 0.172025, 
    0.263266, 0.141925, 0.241269, 0.118604, 0.208073, 0.118609, 0.207755, 
    0.13382, 0.222823, 0.130154, 0.213046, 0.137371, 0.232756, 0.147864, 
    0.245867]}) 

fig, ax = plt.subplots() # 1 

for key, group in df.groupby('quality'): 
    group.plot('insert', 'mean', yerr='std', label=key, ax=ax) # 2 

plt.show() 

enter image description here

为了使这两个地块出现在同一坐标:

  1. 创建自己的axes对象,斧。
  2. 设置ax参数,在每个呼叫的axes对象group.plot

它可能看起来像柱状图更好:

# fill in missing data with 0, so the bar plots are aligned 
df = df.pivot(index='insert', columns='quality').fillna(0).stack().reset_index() 

colors = ['green', 'red'] 
positions = [0, 1] 

for group, color, pos in zip(df.groupby('quality'), colors, positions): 
    key, group = group 
    print(group) 
    group.plot('insert', 'mean', yerr='std', kind='bar', width=0.4, label=key, 
       position=pos, color=color, alpha=0.5, ax=ax) 

ax.set_xlim(-1, 11) 
plt.show() 

enter image description here