pylab errorbar和pandas的问题

时间:2014-02-07 22:25:44

标签: python matplotlib pandas

当我尝试一起使用pandas和pylab.errorbar时,我收到一条非常令人困惑的错误消息。

我有一个名为summary的数据框:

   year  treatment        SE      mean test
0  2012    Control  0.452069  0.904387    A
1  2012  Pesticide  0.246149  1.365210    B
2  2013    Control  1.073697  3.649746    A
3  2013  Pesticide  1.234574  2.593122    B

当我尝试制作错误栏图时:

for n in summary['treatment'].unique():
    py.errorbar(summary[summary['treatment'] == n]['year'], 
                summary[summary['treatment'] == n]['mean'], 
                summary[summary['treatment'] == n]['SE'])

我收到此错误:

Traceback (most recent call last):
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/IPython/core/interactiveshell.py", line 2827, in run_code
    exec code_obj in self.user_global_ns, self.user_ns
  File "<ipython-input-171-12bf2023b562>", line 2, in <module>
    py.errorbar(summary[summary['treatment'] == n]['year'], summary[summary['treatment'] == n]['mean'], summary[summary['treatment'] == n]['SE'])
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/matplotlib/pyplot.py", line 2697, in errorbar
    errorevery=errorevery, capthick=capthick, **kwargs)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/matplotlib/axes.py", line 5749, in errorbar
    iterable(yerr[0]) and iterable(yerr[1])):
  File "/Library/Python/2.7/site-packages/pandas/core/series.py", line 618, in __getitem__
    return self.index.get_value(self, key)
  File "/Library/Python/2.7/site-packages/pandas/core/index.py", line 724, in get_value
    return self._engine.get_value(series, key)
  File "index.pyx", line 96, in pandas.index.IndexEngine.get_value (pandas/index.c:2873)
  File "index.pyx", line 104, in pandas.index.IndexEngine.get_value (pandas/index.c:2685)
  File "index.pyx", line 148, in pandas.index.IndexEngine.get_loc (pandas/index.c:3422)
  File "hashtable.pyx", line 382, in pandas.hashtable.Int64HashTable.get_item (pandas/hashtable.c:6570)
  File "hashtable.pyx", line 388, in pandas.hashtable.Int64HashTable.get_item (pandas/hashtable.c:6511)
KeyError: 0

我试图解决这个问题。我可以这样画:

py.errorbar(summary[summary['treatment'] == 'Control']['year'],
            summary[summary['treatment'] == 'Control']['mean'],
            yerr = summary[summary['treatment'] == 'Control']['SE'], fmt = 'o')

和此:

py.errorbar(summary[summary['treatment'] == 'Pesticide']['year'],
            summary[summary['treatment'] == 'Pesticide']['mean'],
            yerr = summary[summary['treatment'] == 'Control']['SE'], fmt = 'o')

但不是这样:

py.errorbar(summary[summary['treatment'] == 'Pesticide']['year'],
            summary[summary['treatment'] == 'Pesticide']['mean'],
            yerr = summary[summary['treatment'] == 'Pesticide']['SE'], fmt = 'o')

所以有一些关于yerr中'农药'治疗的问题导致了这个问题。我过去使用了类似的循环并取得了成功,所以这个设置有一些特定的东西。注意:我还使用'test'列代替'treatment'列并得到相同的错误。

想到为什么会失败?

0 个答案:

没有答案