Я пытаюсь запустить мой первый скриптовый сюжет Matplotlib, и я использую точный код из этой статьи, но получаю ошибку KeyError.Я понятия не имею, что это значит.Любые идеи?
Обрабатывать кадр данных pandas в скрипочном сюжете
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
x = np.random.poisson(lam =3, size=100)
y = np.random.choice(["S{}".format(i+1) for i in range(6)], size=len(x))
df = pd.DataFrame({"Scenario":y, "LMP":x})
fig, axes = plt.subplots()
axes.violinplot(dataset = [df[df.Scenario == 'S1']["LMP"],
df[df.Scenario == 'S2']["LMP"],
df[df.Scenario == 'S3']["LMP"],
df[df.Scenario == 'S4']["LMP"],
df[df.Scenario == 'S5']["LMP"],
df[df.Scenario == 'S6']["LMP"] ] )
Ошибка:
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
<ipython-input-6-cd0789171d00> in <module>
15 df[df.Scenario == 'S4']["LMP"],
16 df[df.Scenario == 'S5']["LMP"],
---> 17 df[df.Scenario == 'S6']["LMP"] ] )
18
19 # axes.set_title('Day Ahead Market')
c:\Anaconda\lib\site-packages\matplotlib\__init__.py in inner(ax, data, *args, **kwargs)
1808 "the Matplotlib list!)" % (label_namer, func.__name__),
1809 RuntimeWarning, stacklevel=2)
-> 1810 return func(ax, *args, **kwargs)
1811
1812 inner.__doc__ = _add_data_doc(inner.__doc__,
c:\Anaconda\lib\site-packages\matplotlib\axes\_axes.py in violinplot(self, dataset, positions, vert, widths, showmeans, showextrema, showmedians, points, bw_method)
7915 return kde.evaluate(coords)
7916
-> 7917 vpstats = cbook.violin_stats(dataset, _kde_method, points=points)
7918 return self.violin(vpstats, positions=positions, vert=vert,
7919 widths=widths, showmeans=showmeans,
c:\Anaconda\lib\site-packages\matplotlib\cbook\__init__.py in violin_stats(X, method, points)
1460 # Evaluate the kernel density estimate
1461 coords = np.linspace(min_val, max_val, points)
-> 1462 stats['vals'] = method(x, coords)
1463 stats['coords'] = coords
1464
c:\Anaconda\lib\site-packages\matplotlib\axes\_axes.py in _kde_method(X, coords)
7910 def _kde_method(X, coords):
7911 # fallback gracefully if the vector contains only one value
-> 7912 if np.all(X[0] == X):
7913 return (X[0] == coords).astype(float)
7914 kde = mlab.GaussianKDE(X, bw_method)
c:\Anaconda\lib\site-packages\pandas\core\series.py in __getitem__(self, key)
765 key = com._apply_if_callable(key, self)
766 try:
--> 767 result = self.index.get_value(self, key)
768
769 if not is_scalar(result):
c:\Anaconda\lib\site-packages\pandas\core\indexes\base.py in get_value(self, series, key)
3116 try:
3117 return self._engine.get_value(s, k,
-> 3118 tz=getattr(series.dtype, 'tz', None))
3119 except KeyError as e1:
3120 if len(self) > 0 and self.inferred_type in ['integer', 'boolean']:
pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_value()
pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_value()
pandas\_libs\index.pyx in pandas._libs.index.IndexEngine.get_loc()
pandas\_libs\hashtable_class_helper.pxi in pandas._libs.hashtable.Int64HashTable.get_item()
pandas\_libs\hashtable_class_helper.pxi in pandas._libs.hashtable.Int64HashTable.get_item()
KeyError: 0