2

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.

user3527975
  • 1,683
  • 8
  • 25
  • 43

1 Answers1

1

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.

user3527975
  • 1,683
  • 8
  • 25
  • 43