0

I ran into a problem again thats causing me headache for several hours already...and I'm pretty sure it's just a minor thing.

I got a long datafile and created a list with the header names and their types:

col_headers = [('TIMESTAMP','|S21'),('XXX_1','<i8'),('YYY_2','<i8')

After that I created a dictionary variable that holds the options and finally imported it with

bladata = scipy.genfromtxt(datafile, **kwargs)

For extracting time information I used

import datetime as dt
for i in range (0,len(bladata['TIMESTAMP']),1):
    datestring = bladata['TIMESTAMP'][i]
    #create a datetime object holding dates and times
    d = dt.datetime.strptime(datestring, '"%Y-%m-%d %H:%M:%S"')

And finally I assigned the data to a variable by using

XXX = bladata['XXX_1']
YYY = bladata['YYY_2']

(btw when the header name in my datafile is not XXX_1 but XXX(1) instead, I receive the error message "field named XXX(1) not found" - how can I solve this problem as well?)

Now when it comes to plotting, I receive the error message

x and y must have same first dimension

when using the following code

pylab.plot(dt, XXX)
pylab.plot(dt, YYY)
pylab.xlim(d.datetime(2014, 01, 10), d.datetime(2014, 06, 10))

I googled that error message and found a solution that suggested converting a list into a numpy array...but where and which one? I'm pretty sure that isn't a big problem at all, but I couldn't figure it out for several hours...so I'm happy for any usefull reply.

€dit: for clarification...XXX and YYY are 2 different measurements that I want to visualize in 1 plot versus its corresponding date.

Jongware
  • 22,200
  • 8
  • 54
  • 100
GeoEki
  • 437
  • 1
  • 7
  • 20

1 Answers1

2

In pylab.plot(dt, XXX) you're trying to plot the imported datetime module itself against a list of values, which is not what you want.

Instead you want something like pylab.plot(times, XXX) where times should be a list containing the datetime objects corresponding to the list XXX.

The error message you are getting is pointing out the 2 arguments you pass to the plot function should be lists of the same length.

See Plotting time in Python with Matplotlib for further information including formatting the x-axis labels.

Community
  • 1
  • 1
Siwel
  • 705
  • 10
  • 25
  • Sorry for the confusion...XXX and YYY are 2 different type of data that I want to visualize in the same plot but with the same temporal range (from TIMESTAMP)! So I want to plot XXX and YYY versus its corresponding date. – GeoEki Aug 27 '15 at 12:26
  • Edited to expand on my explanation and to correct my answer with your clarification. Let me know if you need any more explanation. – Siwel Aug 27 '15 at 14:22
  • Oh god...all I needed was that tiny `date2num` function...took me the whole day to figure that out. Now everything works, thank you! – GeoEki Aug 27 '15 at 15:00