Closed
Description
Hello!
Bug summary
While trying to use matplotlib
with custom subclass of datetime
objects as x-values, a TypeError
is raised.
It seems to exist a workaround using matplotlib.units
: https://stackoverflow.com/a/10239347/2291710
However, I would like to know is it could be possible or not to support any subclass inherited from datetime
natively (using issubclass()
for example)? I tried to dig into the matplotlib
source code but this is too complex for me.
The main use-case is for users of alternative datetime libraries like Pendulum
or Arrow
.
Code for reproduction
import datetime
import matplotlib.pyplot as plt
class MyDatetime(datetime.datetime):
pass
values = range(4)
datetimes = [MyDatetime.now() for _ in values]
fig, ax = plt.subplots()
ax.plot(datetimes, values)
Actual outcome
Traceback (most recent call last):
File "C:\Users\delgan\Desktop\test.py", line 11, in <module>
ax.plot(datetimes, values)
File "C:\Users\delgan\AppData\Local\Programs\Python\Python36\lib\site-packages\matplotlib\__init__.py", line 1710, in inner
return func(ax, *args, **kwargs)
File "C:\Users\delgan\AppData\Local\Programs\Python\Python36\lib\site-packages\matplotlib\axes\_axes.py", line 1438, in plot
self.add_line(line)
File "C:\Users\delgan\AppData\Local\Programs\Python\Python36\lib\site-packages\matplotlib\axes\_base.py", line 1759, in add_line
self._update_line_limits(line)
File "C:\Users\delgan\AppData\Local\Programs\Python\Python36\lib\site-packages\matplotlib\axes\_base.py", line 1781, in _update_line_limits
path = line.get_path()
File "C:\Users\delgan\AppData\Local\Programs\Python\Python36\lib\site-packages\matplotlib\lines.py", line 951, in get_path
self.recache()
File "C:\Users\delgan\AppData\Local\Programs\Python\Python36\lib\site-packages\matplotlib\lines.py", line 652, in recache
x = _to_unmasked_float_array(xconv).ravel()
File "C:\Users\delgan\AppData\Local\Programs\Python\Python36\lib\site-packages\matplotlib\cbook\__init__.py", line 2008, in _to_unmasked_float_array
return np.asarray(x, float)
File "C:\Users\delgan\AppData\Local\Programs\Python\Python36\lib\site-packages\numpy\core\numeric.py", line 531, in asarray
return array(a, dtype, copy=False, order=order)
TypeError: float() argument must be a string or a number, not 'MyDatetime'
Expected outcome
There should be no error, and the graph should be plotted as if the dates was proper datetime
objects.
Matplotlib version
- Operating system: WSL
- Matplotlib version: 2.1.0
- Matplotlib backen: TkAgg
- Python version: 3.6.4