You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Cannot use a timedelta Rectangle width with a datetime axis #4916
The big problem is that the above code dies with an exception.
AttributeError Traceback (most recent call last)
<ipython-input-13-b4545adc3ff2> in <module>()
12 xy=(dt(2015, 1, 3), 50.0),
13 width=datetime.timedelta(days=2),
---> 14 height=1.0,
15 ))
16 plot(dates, data)
...
/home/brhodes/.v/misc/lib/python2.7/site-packages/matplotlib/dates.pyc in _to_ordinalf(dt)
202 dt -= delta
203
--> 204 base = float(dt.toordinal())
205 if hasattr(dt, 'hour'):
206 base += (dt.hour / HOURS_PER_DAY + dt.minute / MINUTES_PER_DAY +
AttributeError: 'datetime.timedelta' object has no attribute 'toordinal'
The other problem is that my backup plan, of replacing the timedelta() with the floating-point value 2.0, destroys the date-ness of the x axis. It reverts to being a float that does not display as a date.
Why is a timedelta not a valid increment or offset with which I can express width along a datetime x-axis?
I don't really have time, and I haven't tested if I could break it
(or read the code to see why matplotlib needed to `toordinal`),
but it does work for my usecase.
It might be good to have an example though since it makes plotting
something like a Gantt plot more elegant.
Activity
tacaswell commentedon Aug 13, 2015
As a work around I would try using
ax.fill_betweenxor one of the lower-level polygons which lets you specify all of the vertex coordinates.liubenyuan commentedon Dec 23, 2016
@brandon-rhodes I faced the same problem as you, to annotate a graph using a rectangle.
My workaround is to define a function, it can automatically use timestamp as x axis.
bnguyenvanyen commentedon May 11, 2017
Another workaround is to subclass
timezone.timedeltato implement atoordinalmethod(which has no true meaning).
Basically I used
WeatherGod commentedon May 12, 2017
Clever! Would you like to make an example show-casing this?
bnguyenvanyen commentedon May 15, 2017