获取IndexError:在尝试使用Pandas.DataFrame.plot.hexbin时索引超出范围

时间:2016-01-04 23:51:20

标签: python pandas matplotlib

我正在使用Python的matplotlib库和Pandas对NBA镜头数据进行建模。 以下是导致错误的行:

shot_df.plot.hexbin(shot_df.LOC_X, shot_df.LOC_Y)

shot_df是包含所有镜头的大熊猫DataFrameLOC_X是所有x坐标,LOC_Y是所有对应的y坐标。 LOC_X可以位于[-252,252]LOC_Y可以来自[-47.5,~900],但我只是在LOC_Y小于424.5时绘制值

虽然

pyplot.hexbin(shot_df.LOC_X,shot_df.LOC_Y)

在给定相同输入时生成预期输出。

我已经梳理了抛出错误的数据,但仍然遗失了为什么我会收到此错误。任何建议,将不胜感激!

Traceback (most recent call last):
  File "shotChart.py", line 153, in <module>
    plt.sca(shot_df.plot.hexbin(shot_df.LOC_X, shot_df.LOC_Y))
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pandas/tools/plotting.py", line 3880, in hexbin
    return self(kind='hexbin', x=x, y=y, C=C, **kwds)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pandas/tools/plotting.py", line 3671, in __call__
    sort_columns=sort_columns, **kwds)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pandas/tools/plotting.py", line 2556, in plot_frame
    **kwds)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pandas/tools/plotting.py", line 2384, in _plot
    plot_obj.generate()
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pandas/tools/plotting.py", line 987, in generate
    self._make_plot()
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pandas/tools/plotting.py", line 1602, in _make_plot
    ax.hexbin(data[x].values, data[y].values, C=c_values, cmap=cmap,
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pandas/core/frame.py", line 1963, in __getitem__
    return self._getitem_array(key)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pandas/core/frame.py", line 2008, in _getitem_array
    return self.take(indexer, axis=1, convert=True)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pandas/core/generic.py", line 1371, in take
    convert=True, verify=True)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pandas/core/internals.py", line 3619, in take
    indexer = maybe_convert_indices(indexer, n)
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/pandas/core/indexing.py", line 1750, in maybe_convert_indices
    raise IndexError("indices are out-of-bounds")
IndexError: indices are out-of-bounds

2 个答案:

答案 0 :(得分:1)

好的,我想出了如何解决这个问题。它使用我的x值shot_df.LOC_X作为索引,这弄乱了整个图的总索引范围(仅允许0到11之间的索引值)。由于Pandas.DataFrame.plot.hexbin只是Matplotlib.pyplot.hexbin的包装,因此在没有索引编制问题的情况下实现相同结果的更简单方法是plt.hexbin(shot_df.LOC_X,shot_df.LOC_Y),并且可以使用所有相同的关键字。

答案 1 :(得分:0)

根据pandas' documentation,hexbin方法接受列索引(int)或列名(str)。尝试使用列名:

shot_df.plot.hexbin(x='LOC_X', y='LOC_Y')