1

I am trying to plot time vs velocity data. I am able to parse them from the required file and created the dict structure for them.

Following is my try

import matplotlib.pyplot as plt
import sys
import os

data = {'velocity' : [2,4,6,8,12,50], 
        'time' : [12:08:00, 12:08:02, 12:08:04, 12:08:06, 12:08:08, 2:08:10]}

plt.figure(1)
plt.plot(data['time'] ,data['velocity'])
plt.gcf().autofmt_xdate()
plt.title('velocity vs time')
plt.show()

So when I try to plot them I am getting ValueError: invalid literal for float(): 12:08:00 error. So far I am not having any luck. Is it something I missed here?

Thanks

3
  • The error shown indicates that plot() expects float type, So you might want to convert your time values into reasonable float or int type values before plotting. A quick search on Google also indicates that they can be datetime.datetime type, see stackoverflow.com/questions/19079143/… for reference. Commented Mar 18, 2016 at 18:47
  • 1
    stackoverflow.com/q/1574088/2749397 Commented Mar 18, 2016 at 19:28
  • Do you also have date information, not just time? Commented Mar 18, 2016 at 21:15

1 Answer 1

2

Your list values for time need to be either strings or a time format. You can change your data variable to

data = {'velocity' : [2,4,6,8,12,50], 
        'time' : ['12:08:00', '12:08:02', '12:08:04', '12:08:06', '12:08:08', '2:08:10']}

And use plt.xticks() like so:

plt.plot(data['velocity'])
plt.xticks(range(len(data['time'])), data['time'])

Which gives the plot: Velocity vs. Time

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

Comments

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.