I'm getting the following error with plot_date in matplotlib - example in jupyter notebook below.
python version 3.6.3
pandas version 0.21.0
matplotlib 2.1.0
import pandas as pd
%pylab inline
datelist = pd.date_range(pd.datetime.today(), periods=100).tolist()
vals = np.random.rand(100,1)
No error - list of pandas._libs.tslib.Timestamp and array of values:
plt.plot_date(datelist, vals, xdate=True);
AttributeError - pandas DataFrame with DataTimeIndex:
Note: there is no error when I use python 3.4.5, matplotlib 1.5.1, pandas 0.19.2
Edit start
Insert below lines for workaround as per Pandas v0.21.0 What's New and duplicate answer.
from pandas.tseries import converter
converter.register()
Edit end
df = pd.DataFrame(data=vals, index=datelist)
plt.plot_date(df.index, df[0], xdate=True);
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-22-c438634aa06b> in <module>()
----> 1 plt.plot_date(df.index, df[0], xdate=True);
~/anaconda3/lib/python3.6/site-packages/matplotlib/pyplot.py in plot_date(x, y, fmt, tz, xdate, ydate, hold, data, **kwargs)
3261 try:
3262 ret = ax.plot_date(x, y, fmt=fmt, tz=tz, xdate=xdate, ydate=ydate,
-> 3263 data=data, **kwargs)
3264 finally:
3265 ax._hold = washold
~/anaconda3/lib/python3.6/site-packages/matplotlib/__init__.py in inner(ax, *args, **kwargs)
1708 warnings.warn(msg % (label_namer, func.__name__),
1709 RuntimeWarning, stacklevel=2)
-> 1710 return func(ax, *args, **kwargs)
1711 pre_doc = inner.__doc__
1712 if pre_doc is None:
~/anaconda3/lib/python3.6/site-packages/matplotlib/axes/_axes.py in plot_date(self, x, y, fmt, tz, xdate, ydate, **kwargs)
1515 self.yaxis_date(tz)
1516
-> 1517 ret = self.plot(x, y, fmt, **kwargs)
1518
1519 self.autoscale_view()
~/anaconda3/lib/python3.6/site-packages/matplotlib/__init__.py in inner(ax, *args, **kwargs)
1708 warnings.warn(msg % (label_namer, func.__name__),
1709 RuntimeWarning, stacklevel=2)
-> 1710 return func(ax, *args, **kwargs)
1711 pre_doc = inner.__doc__
1712 if pre_doc is None:
~/anaconda3/lib/python3.6/site-packages/matplotlib/axes/_axes.py in plot(self, *args, **kwargs)
1436
1437 for line in self._get_lines(*args, **kwargs):
-> 1438 self.add_line(line)
1439 lines.append(line)
1440
~/anaconda3/lib/python3.6/site-packages/matplotlib/axes/_base.py in add_line(self, line)
1757 line.set_clip_path(self.patch)
1758
-> 1759 self._update_line_limits(line)
1760 if not line.get_label():
1761 line.set_label('_line%d' % len(self.lines))
~/anaconda3/lib/python3.6/site-packages/matplotlib/axes/_base.py in _update_line_limits(self, line)
1779 Figures out the data limit of the given line, updating self.dataLim.
1780 """
-> 1781 path = line.get_path()
1782 if path.vertices.size == 0:
1783 return
~/anaconda3/lib/python3.6/site-packages/matplotlib/lines.py in get_path(self)
949 """
950 if self._invalidy or self._invalidx:
--> 951 self.recache()
952 return self._path
953
~/anaconda3/lib/python3.6/site-packages/matplotlib/lines.py in recache(self, always)
649 def recache(self, always=False):
650 if always or self._invalidx:
--> 651 xconv = self.convert_xunits(self._xorig)
652 x = _to_unmasked_float_array(xconv).ravel()
653 else:
~/anaconda3/lib/python3.6/site-packages/matplotlib/artist.py in convert_xunits(self, x)
189 if ax is None or ax.xaxis is None:
190 return x
--> 191 return ax.xaxis.convert_units(x)
192
193 def convert_yunits(self, y):
~/anaconda3/lib/python3.6/site-packages/matplotlib/axis.py in convert_units(self, x)
1489 return x
1490
-> 1491 ret = self.converter.convert(x, self.units, self)
1492 return ret
1493
~/anaconda3/lib/python3.6/site-packages/matplotlib/dates.py in convert(value, unit, axis)
1601 if units.ConversionInterface.is_numlike(value):
1602 return value
-> 1603 return date2num(value)
1604
1605 @staticmethod
~/anaconda3/lib/python3.6/site-packages/matplotlib/dates.py in date2num(d)
371 if not d.size:
372 return d
--> 373 return _to_ordinalf_np_vectorized(d)
374
375
~/anaconda3/lib/python3.6/site-packages/numpy/lib/function_base.py in __call__(self, *args, **kwargs)
2732 vargs.extend([kwargs[_n] for _n in names])
2733
-> 2734 return self._vectorize_call(func=func, args=vargs)
2735
2736 def _get_ufunc_and_otypes(self, func, args):
~/anaconda3/lib/python3.6/site-packages/numpy/lib/function_base.py in _vectorize_call(self, func, args)
2802 res = func()
2803 else:
-> 2804 ufunc, otypes = self._get_ufunc_and_otypes(func=func, args=args)
2805
2806 # Convert args to object arrays first
~/anaconda3/lib/python3.6/site-packages/numpy/lib/function_base.py in _get_ufunc_and_otypes(self, func, args)
2762
2763 inputs = [arg.flat[0] for arg in args]
-> 2764 outputs = func(*inputs)
2765
2766 # Performance note: profiling indicates that -- for simple
~/anaconda3/lib/python3.6/site-packages/matplotlib/dates.py in _to_ordinalf(dt)
220 tzi = UTC
221
--> 222 base = float(dt.toordinal())
223
224 # If it's sufficiently datetime-like, it will have a `date()` method
AttributeError: 'numpy.datetime64' object has no attribute 'toordinal'