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

Imanol Luengo
  • 15,366
  • 2
  • 49
  • 67
kkard
  • 79
  • 3
  • 10
  • 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 http://stackoverflow.com/questions/19079143/how-to-plot-time-series-in-python for reference. – woozyking Mar 18 '16 at 18:47
  • 1
    http://stackoverflow.com/q/1574088/2749397 – gboffi Mar 18 '16 at 19:28
  • Do you also have date information, not just time? – MaxNoe Mar 18 '16 at 21:15

1 Answers1

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

Kevin K.
  • 1,327
  • 2
  • 13
  • 18