2017-06-23 98 views
1

下面是我要绘制的残差的日期为x轴的各个部门在数据帧多条曲线数据帧

date  dept  residual 
4/22/17  8   100.00 
4/29/17  8   23.34 
....  8   ... 
....  8   ... 

4/22/17  12   10.10 
....  12   ... 
....  12   ... 

的领域,我想单独地块。我能够绘制线图的每个部门,但作为一个单一的情节使用下面的代码:

data = pd.DataFrame.from_csv('hardlines_error.csv') 

for label, df in data.groupby('dept'): 
    df.residual.plot( label=label,) 
plt.legend() 

有人能告诉我如何将它们绘制在网格独立的情节?

回答

2

我想你需要pivot然后plot如果需要一个图表:

df = df.pivot(index='date',columns='dept', values='residual') 
print (df) 
dept   8  12 
date     
4/22/17 100.00 10.1 
4/29/17 23.34 NaN 

替代解决方案:

df = df.set_index(['date','dept'])['residual'].unstack() 
print (df) 
dept   8  12 
date     
4/22/17 100.00 10.1 
4/29/17 23.34 NaN 


df.plot() 

但是,如果有重复,得到错误:

ValueError: Index contains duplicate entries, cannot reshape

那么需要pivot_table或带聚合功能的3210 - 检查this answer

但如果需要单独的每个图:

for i, group in df.groupby('dept'): 
    plt.figure() 
    group.plot(x='date', y='residual', title=str(i)) 

对于网格使用:

import matplotlib.pyplot as plt 

grouped = df.groupby('dept') 

ncols=2 
nrows = int(np.ceil(grouped.ngroups/ncols)) 

fig, axes = plt.subplots(nrows=nrows, ncols=ncols, figsize=(12,4), sharey=True) 
for (key, ax) in zip(grouped.groups.keys(), axes.flatten()): 
    grouped.get_group(key).plot(x='date', y='residual', ax=ax) 

ax.legend() 
plt.show() 
+0

我得到这个错误:'KeyError异常。 – user1274878

+0

什么是'print(df.columns.tolist())'?也许有一些像''date''这样的空格 – jezrael

+0

那有用,谢谢!还有一件事。你能告诉我如何让这些情节出现在网格中,而不是一个在另一个之下吗? – user1274878