2
votes

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.

1
You can use date2num See also here: stackoverflow.com/a/1574146/1898982jonie83
@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
The number is days since January 1st 0001, floating point numbers are hours and secondsMaxNoe
@MaxNoe : But I need the dates in looking like dates.user3527975

1 Answers

1
votes

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.