Skip to main content
 首页 » 编程设计

python之Pandas 用 yerr 绘图给出关键错误 :0

2024年10月24日9虾米哥

我正在尝试从 pandas 数据帧构建一个图,但是当我将这个数据帧的一部分作为 yerr 传递时,我得到一个关键错误:0。 我已经检查过,在 pandas page 上报告了类似的问题,但它应该已解决,所以我不确定它来自哪里。 任何人都知道为什么,或者我如何绘制这两个数据框? 编辑:这是 https://github.com/pydata/pandas/issues/11858 的原因,无论如何我可以解决这个问题?

这是 means.plot(kind='bar') 现在我需要 yerr :

我的切片数据框看起来像这样:

#means 
condition  scenario_1  scenario_2  scenario_3 
date                                          
1950         7857.495    4418.180    4418.180 
1960         1361.255     769.095     769.095 
1970          947.855     649.875     649.875 
1980          743.565     542.350     542.350 
1990         4157.495    3780.185    3780.185 
2000         4473.730    5953.955    5940.020 
2010          962.565     736.735     486.140 
2020          663.065     464.065     358.580 
2030          609.285     375.195     264.415 
2040         1706.620    1167.375    1051.090 
2050          882.075     518.810     393.240 
2060         1483.740     863.260     778.115 
2070         2540.660    2417.420    2392.325 
2080         3329.965    2484.215    2332.140 
2090         1981.605    1326.115    1363.695 
 
#errors 
       scenario_1   scneario_2   scenario_3 
date                                        
1950  6882.473594  3977.169284  3977.169284 
1960  2063.900035  1209.355869  1209.355869 
1970  1641.563211  1132.895230  1132.895230 
1980  1304.848179   893.150490   893.150490 
1990  4833.384691  4071.813927  4071.813927 
2000  3071.053838  3300.206511  3262.672983 
2010  1519.563511  1099.878070   703.131441 
2020  1085.763048   694.784212   456.909226 
2030  1012.836592   647.889589   342.873572 
2040  2242.979678  1573.683502  1298.028517 
2050  1462.245123   891.855864   517.363621 
2060  2225.214719  1213.065737   975.519051 
2070  2927.880585  2423.424810  2076.170121 
2080  3572.416062  2436.318656  2316.993311 
2090  2510.864263  1487.605484  1499.623779 
 
#which I try to plot that way: 
means.plot(kind='bar',yerr=errors) 

我可以毫无问题地绘制方法,但每次我尝试添加 yerr 时,它都会给我:

KeyError                                  Traceback (most recent call last) 
<ipython-input-68-2e629f7a18f3> in <module>() 
----> 1 means.plot(kind='bar', yerr=errors) 
 
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pandas/tools/plotting.pyc in __call__(self, x, y, kind, ax, subplots, sharex, sharey, layout, figsize, use_index, title, grid, legend, style, logx, logy, loglog, xticks, yticks, xlim, ylim, rot, fontsize, colormap, table, yerr, xerr, secondary_y, sort_columns, **kwds) 
   3669                           fontsize=fontsize, colormap=colormap, table=table, 
   3670                           yerr=yerr, xerr=xerr, secondary_y=secondary_y, 
-> 3671                           sort_columns=sort_columns, **kwds) 
   3672     __call__.__doc__ = plot_frame.__doc__ 
   3673  
 
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pandas/tools/plotting.pyc in plot_frame(data, x, y, kind, ax, subplots, sharex, sharey, layout, figsize, use_index, title, grid, legend, style, logx, logy, loglog, xticks, yticks, xlim, ylim, rot, fontsize, colormap, table, yerr, xerr, secondary_y, sort_columns, **kwds) 
   2554                  yerr=yerr, xerr=xerr, 
   2555                  secondary_y=secondary_y, sort_columns=sort_columns, 
-> 2556                  **kwds) 
   2557  
   2558  
 
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pandas/tools/plotting.pyc in _plot(data, x, y, subplots, ax, kind, **kwds) 
   2382         plot_obj = klass(data, subplots=subplots, ax=ax, kind=kind, **kwds) 
   2383  
-> 2384     plot_obj.generate() 
   2385     plot_obj.draw() 
   2386     return plot_obj.result 
 
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pandas/tools/plotting.pyc in generate(self) 
    985         self._compute_plot_data() 
    986         self._setup_subplots() 
--> 987         self._make_plot() 
    988         self._add_table() 
    989         self._make_legend() 
 
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pandas/tools/plotting.pyc in _make_plot(self) 
   1920                 w = self.bar_width / K 
   1921                 rect = self._plot(ax, self.ax_pos + (i + 0.5) * w, y, w, 
-> 1922                                   start=start, label=label, log=self.log, **kwds) 
   1923             self._add_legend_handle(rect, label, index=i) 
   1924  
 
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pandas/tools/plotting.pyc in _plot(cls, ax, x, y, w, start, log, **kwds) 
   1870     @classmethod 
   1871     def _plot(cls, ax, x, y, w, start=0, log=False, **kwds): 
-> 1872         return ax.bar(x, y, w, bottom=start, log=log, **kwds) 
   1873  
   1874     @property 
 
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/matplotlib/__init__.pyc in inner(ax, *args, **kwargs) 
   1810                     warnings.warn(msg % (label_namer, func.__name__), 
   1811                                   RuntimeWarning, stacklevel=2) 
-> 1812             return func(ax, *args, **kwargs) 
   1813         pre_doc = inner.__doc__ 
   1814         if pre_doc is None: 
 
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/matplotlib/axes/_axes.pyc in bar(self, left, height, width, bottom, **kwargs) 
   2154             errorbar = self.errorbar(x, y, 
   2155                                      yerr=yerr, xerr=xerr, 
-> 2156                                      fmt='none', **error_kw) 
   2157         else: 
   2158             errorbar = None 
 
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/matplotlib/__init__.pyc in inner(ax, *args, **kwargs) 
   1810                     warnings.warn(msg % (label_namer, func.__name__), 
   1811                                   RuntimeWarning, stacklevel=2) 
-> 1812             return func(ax, *args, **kwargs) 
   1813         pre_doc = inner.__doc__ 
   1814         if pre_doc is None: 
 
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/matplotlib/axes/_axes.pyc in errorbar(self, x, y, yerr, xerr, fmt, ecolor, elinewidth, capsize, barsabove, lolims, uplims, xlolims, xuplims, errorevery, capthick, **kwargs) 
   2961                 # Check for scalar or symmetric, as in xerr. 
   2962                 if len(yerr) > 1 and not ((len(yerr) == len(y) and not ( 
-> 2963                         iterable(yerr[0]) and len(yerr[0]) > 1))): 
   2964                     raise ValueError("yerr must be a scalar, the same " 
   2965                                      "dimensions as y, or 2xN.") 
 
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pandas/core/series.pyc in __getitem__(self, key) 
    555     def __getitem__(self, key): 
    556         try: 
--> 557             result = self.index.get_value(self, key) 
    558  
    559             if not np.isscalar(result): 
 
/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pandas/core/index.pyc in get_value(self, series, key) 
   1788  
   1789         try: 
-> 1790             return self._engine.get_value(s, k) 
   1791         except KeyError as e1: 
   1792             if len(self) > 0 and self.inferred_type in ['integer','boolean']: 
 
pandas/index.pyx in pandas.index.IndexEngine.get_value (pandas/index.c:3204)() 
 
pandas/index.pyx in pandas.index.IndexEngine.get_value (pandas/index.c:2903)() 
 
pandas/index.pyx in pandas.index.IndexEngine.get_loc (pandas/index.c:3843)() 
 
pandas/hashtable.pyx in pandas.hashtable.Int64HashTable.get_item (pandas/hashtable.c:6525)() 
 
pandas/hashtable.pyx in pandas.hashtable.Int64HashTable.get_item (pandas/hashtable.c:6463)() 
 
KeyError: 0 

我读到 yerr 的索引必须包含 0,所以我也尝试了,但我得到了同样的错误。令人沮丧的是,如果我只是绘制带有 scneario_1 错误的 scenario_1 的方法,它会起作用......

我刚刚升级了 pandas 和 matplotlib。

这是用于准备数据框的丑陋代码:

frames = [] 
# Cleaning and adding some fields for each individual scenario's dataframe for concat 
for scenario in scenarios : 
    i = glob.glob( os.path.join( input_path,graph_variable, '*'.join([ 'ALF',graph_variable.replace('_',''), model, scenario,'.csv' ]) ) )[0] 
    tab = pd.read_csv( i, index_col=0 ).ix[begin:end] 
    tab['std'] = tab.std(axis=1) 
    tab['date'] = tab.index 
    tab['scenario']= scenario 
    tab = pd.melt(tab, id_vars=["date", "scenario",'std'], var_name="condition") 
    frames.append(tab) 
 
#Complete dataframe creation with both scenario in long data form 
df = pd.concat(frames, ignore_index= True) 
df = df.drop('condition', 1) 
df = df.rename(columns = {'scenario':'condition'}) 
df = df.sort_values(by=['condition','date']) 
df = df.reset_index(drop=True) 
df = df.groupby(["condition", "date"]).mean().unstack("condition") 
 
errors = df['std'].groupby(df.index // 10 * 10).sum()  
means = df.value.groupby(df.index // 10 * 10).sum() 

请您参考如下方法:

这是一种快速而肮脏的解决方案,但您可以添加一个索引值为零的屏蔽条目,并调整绘图边界以将其隐藏。

# log the plot bounds we'll actually want at the end 
xlim = means.index.min(), means.index.max() 
# add data points that will not be plotted anyways 
means = means.append(DataFrame(np.empty((1, len(means.columns)))+np.nan,   
                     columns=means.columns, index=[0.0,])) 
errors = errors.append(DataFrame(np.empty((1, len(df.columns)))+np.nan,   
                       columns=means.columns, index=[0.0,])) 
ax = means.plot(kind='bar', yerr=errors) 
ax.set_xlim(*xlim)