2

I'm sourcing date in second since the epoch as a floating point number ( from time.time() ) and I'm trying to plot it converting it like this (line[0]):

x,y = [],[]
csv_reader = csv.reader(open(csvFile))
for line in csv_reader:
    x.append(float(line[1]))
    y.append(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(float(line[0]))))


fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(x,y,'o-')
fig.autofmt_xdate()

plt.show()

but matplotlib keeps on erroring out like this :

ValueError: invalid literal for float(): 2013-07-08 15:04:50

Any idea on how to format it properly ?

Cheers

5
  • Why are you taking a float of a quantity of the form 2013-07-08 15:04:50? It's not a float... Commented Jul 8, 2013 at 20:10
  • because if I use float(line[0]) by itself if gives me a very strange result, I'm aware I'm not feeding proper data but I don't know how to format my time.time() for matplotlib properly Commented Jul 8, 2013 at 20:12
  • Could you give us more details about which is the repr() of line[0] & line[1]? Commented Jul 8, 2013 at 20:13
  • 2
    Currently, your y values a strings, like '2013-07-08 15:04:50', and your x values are floats. Do you want y values which are dates? That is possible, but it is usually done the other way around.(with dates on the x-axis.) Commented Jul 8, 2013 at 20:13
  • @Pablo : repr(line[0]'1373292290.671339' repr(line[1]) '57.2' Commented Jul 8, 2013 at 20:16

2 Answers 2

3

You get a ValueError since that format is invalid for float(). Instead of formatting it to a float value, try appending the formatted string to a list and use the yticks() function as follows.

>>> yAxis = ['2013-07-08 15:04:50', '2013-07-08 15:03:50', '2013-07-08 5:04:50']
>>> from random import randint
>>> xAxis = [randint(0, 10) for _ in range(3)]
>>> import pylab as plt
>>> plt.plot(xAxis, range(len(xAxis)))
>>> plt.yticks(range(len(yAxis)), yAxis, size='small')
>>> plt.show()

This gives you a plot like the following, hopefully, that was what you were looking for :

enter image description here

P.S - Did you want the dates on the X-Axis?

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

5 Comments

yes, I meant to have the date on the X-Axis ( still trying to figure out that xticks trick .. )
Just do plt.plot(yAxis) and then plt.xticks(range(len(xAxis)), xAxis, size="small"). And, ofcourse, appropriately change xAxis and yAxis.
nice ! seems to work fine. Would you know how to "skip" dates, it is obviously trying to cram all the dates on the graph and there are way too many !
duh, I'll just keep what I want from the date list :)
I think that it could be done better. What happens if the time interval is not constant? You must to take into account this...
3

Here there is another example, where the spacing between dates are not uniform, and could be randomly distributed:

import numpy as np
import matplotlib.pyplot as plt
import time

time_data = np.array([1373316370.877059, 
                      1373316373.95448, 
                      1373316378.018756, 
                      1373316381.960965, 
                      1373316383.586567, 
                      1373316387.111703, 
                      1373316387.611037, 
                      1373316391.923015, 
                      1373316393.80545, 
                      1373316398.294916])

ydata = np.random.rand(len(time_data))
time_formatted = []
for element in time_data:
    time_formatted.append(time.strftime('%Y-%m-%d %H:%M:%S',
                                        time.localtime(element)))

isort = np.argsort(time_data) #Sorting time_data
plt.xticks(time_data[isort],
           np.array(time_formatted)[isort],
           size='small',
           rotation=35)
plt.plot(time_data[isort],ydata[isort],'k-')
plt.show()

enter image description here

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.