Join the Stack Overflow Community
Stack Overflow is a community of 6.8 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

I have wasted so much time looking through the examples given on internet. But I just cant figure out why can't I plot a simple graph of a list of datetime.date objects against a list of integers

     appleProd1     appleProd2       appleProd3       appleProd4 ..... 70 Products

apple.com  2010-09-12     2008-01-01       2009-03-02        .......................

I wanted to plot a scatter plot for the launch dates with x axis as dates and y axis as the products. And I do this

plt.subplot(1, 1, 1)
product_list = []
label_ticks = []
date_list = []
yval = 1

for prod, date in df.iteritems():  #date is a datetime.date object
    date = pd.to_datetime(date)   
    product = prod
    date_list.append(date)
    prod_list.append(prod)
    label_ticks.append(yval)

    yval+=1

plt.plot_date(date_list, label_ticks)

The last line plt.plot gives me an error saying TypeError: float() argument must be a string or a number . I have also tried converting both the lists to numpy array and use same plt.scatter. Same error. Length of both lists is same. Referred to this site also

http://matplotlib.org/api/pyplot_api.html#matplotlib.pyplot.scatter

Some dates are Nan also. So converting using date2num is giving error there.

share|improve this question
    
You can use date2num See also here: stackoverflow.com/a/1574146/1898982 – jonie83 Aug 5 '14 at 6:34
    
@jonie83 : This converts the datetime to float and the list looks very weird. dates get converted to something like 733962.0, 733962.0, 733604.0, 733657.0. So it doesn't quite help. – user3527975 Aug 5 '14 at 7:23
    
The number is days since January 1st 0001, floating point numbers are hours and seconds – MaxNoe Aug 5 '14 at 8:25
    
@MaxNoe : But I need the dates in looking like dates. – user3527975 Aug 5 '14 at 13:48
up vote 1 down vote accepted

I figured out what the problem was. There were indeed some string values in the date_list so the plt.plot complained. The plt.plot works just fine with a list of datetime.date values. There is no need to convert the datetime.date to any other format.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.