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.

4
  • You can use date2num See also here: stackoverflow.com/a/1574146/1898982
    – jonie83
    Commented Aug 5, 2014 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. Commented Aug 5, 2014 at 7:23
  • The number is days since January 1st 0001, floating point numbers are hours and seconds
    – MaxNoe
    Commented Aug 5, 2014 at 8:25
  • @MaxNoe : But I need the dates in looking like dates. Commented Aug 5, 2014 at 13:48

1 Answer 1

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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.