尝试绘制图形会返回IndexError:元组索引超出范围

时间:2019-11-19 15:00:33

标签: python dataframe matplotlib

我试图制作一些图形以了解算法在哪里返回错误值,因此我尝试通过bin过滤这些值以获取某种平均值,然后获取均值并制作带有均值的图形。

x(及其所有形式)是一个熊猫数据框,并且进行了四舍五入以获得更好看的bin(由于binned值非常大,不会对数据造成太大的影响)

x_tr_g = x_stat_train_good.copy()
x_te_g = x_stat_test_good.copy()
x_tr_b = x_stat_train_bad.copy()
x_te_b = x_stat_test_bad.copy()
x_tr = x_stat_train.copy()
x_te = x_stat_test.copy()
labels = ('train actual',     'test_actual','test_good','test_bad','train_good','train_bad')
train_labels = ('train actual','train_good','train_bad')
test_labels = ('test_actual','test_good','test_bad')
x_trains = (x_tr,x_tr_g,x_tr_b)
x_tests = (x_te,x_te_g,x_te_b)
def roundup(x):
    return int(math.ceil(x / 10.0)) * 10

all_x = (x_tr, x_te, x_tr_g, x_te_g, x_tr_b, x_te_b)
for x in all_x:
    bins =np.linspace(x['price'].min(), x['price'].max(), 50)
    for i in range(0, len(bins)):
        bins[i] = roundup(bins[i])
    x['bin'] = pd.cut(x['price'], bins=bins, labels=bins[1:], precision=0)

for strname in numericals:
    temp = x[x[strname].notnull()]
    x[strname] = temp[strname].astype(int)

for strname in numericals:
    for x,label in zip(x_tests, test_labels):
        df = x[['bin',strname]].groupby('bin').mean()
        df = df.dropna()
        plt.plot(test) #Error occurs here
    plt.xlabel(strname)
    plt.ylabel('price')
    plt.legend(loc=2)
    plt.show()

完整错误:

---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-6-9d00df0dee51> in <module>
      3         df = x[['bin',strname]].groupby('bin').mean()
      4         df = df.dropna()
----> 5         plt.plot(df)
      6     plt.xlabel(strname)
      7     plt.ylabel('price')

/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/matplotlib/pyplot.py in plot(scalex, scaley, data, *args, **kwargs)
   2793     return gca().plot(
   2794         *args, scalex=scalex, scaley=scaley, **({"data": data} if data
-> 2795         is not None else {}), **kwargs)
   2796 
   2797 

/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/matplotlib/axes/_axes.py in plot(self, scalex, scaley, data, *args, **kwargs)
   1664         """
   1665         kwargs = cbook.normalize_kwargs(kwargs, mlines.Line2D._alias_map)
-> 1666         lines = [*self._get_lines(*args, data=data, **kwargs)]
   1667         for line in lines:
   1668             self.add_line(line)

/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/matplotlib/axes/_base.py in __call__(self, *args, **kwargs)
    223                 this += args[0],
    224                 args = args[1:]
--> 225             yield from self._plot_args(this, kwargs)
    226 
    227     def get_next_color(self):

/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/matplotlib/axes/_base.py in _plot_args(self, tup, kwargs)
    397             func = self._makefill
    398 
--> 399         ncx, ncy = x.shape[1], y.shape[1]
    400         if ncx > 1 and ncy > 1 and ncx != ncy:
    401             cbook.warn_deprecated(

IndexError:元组索引超出范围

编辑:添加了错误,希望有助于获得答案

0 个答案:

没有答案