Take the 2-minute tour ×

I'm using this code :

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

fig = plt.figure()
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
share|improve this question
Hmm. I just generated a test csv datafile with 500 rows, columns labelled date and close, and semicolon delimiters, and the above worked for me. –  DSM Jun 9 '11 at 6:39
hmmm, would it work with this file ? db.tt/MIOqFA0 –  Finger twist Jun 9 '11 at 6:48

2 Answers 2

up vote 4 down vote accepted

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:

bad date plot

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})

good plot

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?

share|improve this answer
tried your modification, still the same issue, I`m gonna try on a other system –  Finger twist Jun 9 '11 at 8:08
That rules out my theory, but there's definitely something different between your matplotlib and mine. I don't think you're doing anything wrong. –  DSM Jun 9 '11 at 8:31
it was the install .. –  Finger twist Jun 9 '11 at 11:16

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


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.

share|improve this answer
sure, you can find the csv here : db.tt/MIOqFA0 . I was looking at a matplotlib example file, and this is how they are plotting their data, (using a npy file) . Edit above –  Finger twist Jun 9 '11 at 6:37
strange, I'm getting the same error, maybe something else is going wrong, I'm editing the question with the full error log –  Finger twist Jun 9 '11 at 7:00
I didn't have a r.date and r.close as methods for r. –  PateToni Jun 9 '11 at 7:00
img707.imageshack.us/i/snapmlab.png this is just a snapshot what I get so far. Which version of matplotlib do you have? –  PateToni Jun 9 '11 at 7:07
I've got matplotlib 1.0.1 and numpy 1.4.1 . I might try on an other system to see if I get the same result as you guys –  Finger twist Jun 9 '11 at 8:08

Your Answer


