2016-01-13 33 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