开发者

Numpy index out of range error

I'm using this code :

r = mlab.csv2rec(datafile, delimiter=';')

fig = plt.figure()
fig.subplots_adjust(开发者_C百科bottom=0.2)
ax = fig.add_subplot(111)
ax.plot(r.date, r.close)

but it's returning this :

ax.plot(r.date, r.close)

IndexError: index out of range for array

How do I make sure that I`m staying inside the array range ?

if I print out len(r.date) and len(r.close) they are both returning : 500


EDIT, this is a sample code from matplotlib, using a npy file, I'd like to do the same for e CSV file :

datafile = cbook.get_sample_data('goog.npy')
r = np.load(datafile).view(np.recarray)

fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(r.date, r.adj_close)

EDIT, full error log:

Traceback (most recent call last):
  File "main02.py", line 66, in <module>
    ax.plot(r['date'], r['close'])
  File "/usr/lib/python2.6/site-packages/matplotlib/axes.py", line 3788, in plot
    self.autoscale_view(scalex=scalex, scaley=scaley)
  File "/usr/lib/python2.6/site-packages/matplotlib/axes.py", line 1824, in autoscale_view
    y0, y1 = ylocator.view_limits(y0, y1)
  File "/usr/lib/python2.6/site-packages/matplotlib/ticker.py", line 1170, in view_limits
    return np.take(self.bin_boundaries(dmin, dmax), [0,-1])
  File "/film/tools/PythonExtensions/v41/py26_linux-x64/numpy/core/fromnumeric.py", line 103, in take
    return take(indices, axis, out, mode)
IndexError: index out of range for array


Okay, I can plot the original dataset without errors -- but I think it's misinterpreting the date information as MM/DD/YYYY when it's really DD/MM/YYYY.

Here's what I get for the original code:

Numpy index out of range error

And here's what I get when I fix the date:

import datetime
fixdate = lambda d: datetime.datetime.strptime(d, '%d/%m/%Y')
r = mlab.csv2rec(datafile, delimiter=';', converterd={0: fixdate})

Numpy index out of range error

So if I had to guess, I'd say that your version of matplotlib is rejecting the impossible dates, so it thinks your r.date column has fewer "real" values than it should. Since I can't reproduce the error, it's hard to be sure.

Could you try the above datetime modification?


I guess you want to plot your data of the csv-file, right? My problem is that I think you will not access your data with r.date and r.close. Your r is an array with your data and you don't need the methods for plotting the data.

If it's possible can you send some lines of the file that I can check it?

Best regards

EDIT

It's easier than I thought. Replace r.date with r['date'] and r.close with r['close']. This should work.

You load with the command mlab.csv2rec the csv-file. I guees matplotlib use numpy for that. After you have load the data, you can access the data with the field names. The names are defined by the first row (for your case). If you want to learn more about that. You can google numpy.dtypes for a quick tutorial on fields. If you want more informations or help I will be glad to help.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜