2
votes

I am trying to build a plot from a pandas dataframe, but when I pass a slice of this dataframe as yerr, I get a Key error :0. I have checked and there was a similar issues reported on pandas page but it is supposed to be resolved so I am not sure where it comes from. Anybody has an idea on why, or how could I plot those two dataframe? EDIT :Here is the why https://github.com/pydata/pandas/issues/11858, anyway way I could work around that problem?

Here is means.plot(kind='bar') now I need yerr : That is means.plot(kind='bar') now I need yerr

My sliced dataframes looks like that :

#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)

I can plot the means without problem but then everytime I try to add the yerr it gives me :

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

I read that the index for the yerr had to contain 0, so I tried that as well but I get the same error. Frustratingly enough though if I just plot means of scenario_1 with errors of scneario_1 it works...

I just upgraded both pandas and matplotlib.

Here is the ugly code used to get the dataframe ready :

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()
1
Gives me yerr must be a scalar, the same dimensions as y, or 2xN. - User18981898198119

1 Answers

2
votes

It's a bit of a quick and dirty solution, but you could add a masked entry with an index value of zero, and adjust the plot boundaries to hide it.

# 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)