我正在尝试绘制泰坦尼克号幸存者数据的饼图。我一直试图将其绘制为饼图,但我一直得到一个KeyError 0.我该如何解决这个问题?
figure(1, figsize=(6,6))
ax = axes([0.1, 0.1, 0.8, 0.8])
s_survival = (titanic_data.Survived[titanic_data.Embarked == 'S'][titanic_data.Survived == 1].value_counts()
) / survivors.sum()
c_survival = (titanic_data.Survived[titanic_data.Embarked == 'C'][titanic_data.Survived == 1].value_counts()
) / survivors.sum()
q_survival = (titanic_data.Survived[titanic_data.Embarked == 'Q'][titanic_data.Survived == 1].value_counts()
) / survivors.sum()
labels = ['s_survival', 'c_survival', 'q_survival']
sizes = [s_survival, c_survival, q_survival]
pie(sizes, explode=None, labels=labels, autopct='%1.1f%%', shadow=False, startangle=90)
title('Survivor Percentage from Embarked Port')
非常感谢任何建议。
StackTrace:
KeyError Traceback (most recent call last)
<ipython-input-18-a8c68ae3422f> in <module>()
11 sizes = [s_survival, c_survival, q_survival]
12 # add a list zip here
---> 13 plt.pie(sizes, explode=None, labels=labels, autopct='%1.1f%%', shadow=False, startangle=90)
14 title('Survivor Percentage from Embarked Port')
//anaconda/lib/python2.7/site-packages/matplotlib/pyplot.pyc in pie(x, explode, labels, colors, autopct, pctdistance, shadow, labeldistance, startangle, radius, counterclock, wedgeprops, textprops, center, frame, hold, data)
3135 radius=radius, counterclock=counterclock,
3136 wedgeprops=wedgeprops, textprops=textprops, center=center,
-> 3137 frame=frame, data=data)
3138 finally:
3139 ax.hold(washold)
//anaconda/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:
//anaconda/lib/python2.7/site-packages/matplotlib/axes/_axes.pyc in pie(self, x, explode, labels, colors, autopct, pctdistance, shadow, labeldistance, startangle, radius, counterclock, wedgeprops, textprops, center, frame)
2546 """
2547
-> 2548 x = np.asarray(x).astype(np.float32)
2549
2550 sx = float(x.sum())
//anaconda/lib/python2.7/site-packages/numpy/core/numeric.pyc in asarray(a, dtype, order)
472
473 """
--> 474 return array(a, dtype, copy=False, order=order)
475
476 def asanyarray(a, dtype=None, order=None):
//anaconda/lib/python2.7/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):
//anaconda/lib/python2.7/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: 0