Ticket #10366 (new enhancement)

Opened 3 years ago

plotting dates requires a toordinal() call, which is inconvenient

Reported by: jason Owned by: jason, was
Priority: major Milestone: sage-5.11
Component: graphics Keywords:
Cc: Work issues:
Report Upstream: N/A Reviewers:
Authors: Merged in:
Dependencies: Stopgaps:

Description

This doesn't work:

sage: version()
'Sage Version 4.6, Release Date: 2010-10-30'
sage: from matplotlib.dates import YearLocator, DateFormatter
sage: import datetime
sage: 
sage: data=[
....: (datetime.date( 1995, 1, 1 ), 30),
....: (datetime.date( 1996, 3, 1 ), 50),
....: (datetime.date( 1997, 2, 4 ), 40),
....: ]
sage: 
sage: 
sage: list_plot(data,ticks=[YearLocator(),None],tick_formatter=[DateFormatter('%Y-%m-%d'),None])
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)

/Users/grout/<ipython console> in <module>()

/Users/grout/sage/local/lib/python2.6/site-packages/sage/plot/misc.pyc in wrapper(*args, **kwds)
    136                 options['__original_opts'] = kwds
    137             options.update(kwds)
--> 138             return func(*args, **options)
    139 
    140         

/Users/grout/sage/local/lib/python2.6/site-packages/sage/plot/plot.pyc in list_plot(data, plotjoined, **kwargs)
   3523         P = line(data, **kwargs)
   3524     else:
-> 3525         P = point(data, **kwargs)
   3526     return P
   3527 

/Users/grout/sage/local/lib/python2.6/site-packages/sage/plot/point.pyc in point(points, **kwds)
    310     except (ValueError, TypeError):
    311         from sage.plot.plot3d.shapes2 import point3d
--> 312         return point3d(points, **kwds)
    313 
    314 @rename_keyword(color='rgbcolor', pointsize='size')

/Users/grout/sage/local/lib/python2.6/site-packages/sage/plot/plot3d/shapes2.pyc in point3d(v, size, **kwds)
   1009         return Point(v, size, **kwds)
   1010     else:
-> 1011         A = sum([Point(z, size, **kwds) for z in v])
   1012         A._set_extra_kwds(kwds)
   1013         return A

/Users/grout/sage/local/lib/python2.6/site-packages/sage/plot/plot3d/shapes2.pyc in __init__(self, center, size, **kwds)
    672         """
    673         PrimitiveObject.__init__(self, **kwds)
--> 674         self.loc = (float(center[0]), float(center[1]), float(center[2]))
    675         self.size = size
    676         self._set_extra_kwds(kwds)

TypeError: float() argument must be a string or a number

But inserting a toordinal() call for each date does work. It would be really nice if Sage intelligently handled date data and did something like this automatically.

sage: list_plot([(i.toordinal(), j) for i,j in data],ticks=[YearLocator(),None],tick_formatter=[DateFormatter('%Y-%m-%d'),None])

Note: See TracTickets for help on using tickets.