3

I'm using this code :

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

fig = plt.figure()
fig.subplots_adjust(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
2
  • 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. Commented Jun 9, 2011 at 6:39
  • hmmm, would it work with this file ? db.tt/MIOqFA0 Commented Jun 9, 2011 at 6:48

2 Answers 2

4

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?

Sign up to request clarification or add additional context in comments.

2 Comments

tried your modification, still the same issue, I`m gonna try on a other system
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.
1

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.

5 Comments

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
strange, I'm getting the same error, maybe something else is going wrong, I'm editing the question with the full error log
I didn't have a r.date and r.close as methods for r.
img707.imageshack.us/i/snapmlab.png this is just a snapshot what I get so far. Which version of matplotlib do you have?
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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.