我想知道在matplotlib中绘制条形图时出错。
我试图制作一个条形图。它使用year_avg
和yerr
。
year_avg
1992 34484.080607
1993 39975.673587
1994 37565.689950
1995 47798.504333
name: year_avg, dtype: float64
yerr
1992 4883.203514
1993 2873.929360
1994 3904.568221
1995 1779.298157
name: yerr, dtype: float64
import pandas as pd
import numpy as np
bars = plt.bar(range(df.shape[0]), year_avg, color = 'blue', yerr = yerr)
plt.show()
给出以下错误。我想知道如何解决错误。
KeyError Traceback (most recent call last)
<ipython-input-39-2962d639bd49> in <module>()
1 #plt.figure()
----> 2 bars = plt.bar(range(df.shape[0]), year_avg, color = 'lightslategrey', yerr = yerr).values
3 plt.show()
C:\Users\Anaconda2\envs\gl-env\lib\site-packages\matplotlib\pyplot.pyc in bar(left, height, width, bottom, hold, data, **kwargs)
2641 try:
2642 ret = ax.bar(left, height, width=width, bottom=bottom, data=data,
-> 2643 **kwargs)
2644 finally:
2645 ax.hold(washold)
C:\Users\Anaconda2\envs\gl-env\lib\site-packages\matplotlib\__init__.pyc in inner(ax, *args, **kwargs)
1809 warnings.warn(msg % (label_namer, func.__name__),
1810 RuntimeWarning, stacklevel=2)
-> 1811 return func(ax, *args, **kwargs)
1812 pre_doc = inner.__doc__
1813 if pre_doc is None:
C:\Users\Anaconda2\envs\gl-env\lib\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
C:\Users\Anaconda2\envs\gl-env\lib\site-packages\matplotlib\__init__.pyc in inner(ax, *args, **kwargs)
1809 warnings.warn(msg % (label_namer, func.__name__),
1810 RuntimeWarning, stacklevel=2)
-> 1811 return func(ax, *args, **kwargs)
1812 pre_doc = inner.__doc__
1813 if pre_doc is None:
C:\Users\Anaconda2\envs\gl-env\lib\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.")
C:\Users\Anaconda2\envs\gl-env\lib\site-packages\pandas\core\series.pyc in __getitem__(self, key)
558 def __getitem__(self, key):
559 try:
--> 560 result = self.index.get_value(self, key)
561
562 if not lib.isscalar(result):
C:\Users\Anaconda2\envs\gl-env\lib\site-packages\pandas\indexes\base.pyc in get_value(self, series, key)
1909 try:
1910 return self._engine.get_value(s, k,
-> 1911 tz=getattr(series.dtype, 'tz', None))
1912 except KeyError as e1:
1913 if len(self) > 0 and self.inferred_type in ['integer', 'boolean']:
pandas\index.pyx in pandas.index.IndexEngine.get_value (pandas\index.c:3234)()
pandas\index.pyx in pandas.index.IndexEngine.get_value (pandas\index.c:2931)()
pandas\index.pyx in pandas.index.IndexEngine.get_loc (pandas\index.c:3891)()
pandas\hashtable.pyx in pandas.hashtable.Int64HashTable.get_item (pandas\hashtable.c:6527)()
pandas\hashtable.pyx in pandas.hashtable.Int64HashTable.get_item (pandas\hashtable.c:6465)()
KeyError: 0L