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.

1 Answer 1

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.

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

3 Comments

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.
Edited to expand on my explanation and to correct my answer with your clarification. Let me know if you need any more explanation.
Oh god...all I needed was that tiny date2num function...took me the whole day to figure that out. Now everything works, thank you!

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.