Skip to content

error with errorbar with datetime64  #12271

Closed
@rabernat

Description

@rabernat

Bug report

Bug summary

Calling plt.errorbar(date, y, yerr) with date as a numpy.datetime64 array fails with AttributeError: 'numpy.datetime64' object has no attribute 'toordinal'. The same command works without yerr.

Code for reproduction

from matplotlib import pyplot as plt
import numpy as np
import datetime

n = 24
base = datetime.datetime(2000, 1, 1)
x = np.array([base + datetime.timedelta(hours=i) for i in range(n)]
             ).astype(np.datetime64)
y = np.arange(n)

# works
plt.errorbar(x, y)
# error
plt.errorbar(x, y, yerr=2)

Actual outcome

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-72-14ee6a2415cf> in <module>()
     11 
     12 plt.errorbar(x, y)
---> 13 plt.errorbar(x, y, yerr=2)

/opt/conda/lib/python3.6/site-packages/matplotlib/pyplot.py in errorbar(x, y, yerr, xerr, fmt, ecolor, elinewidth, capsize, barsabove, lolims, uplims, xlolims, xuplims, errorevery, capthick, hold, data, **kwargs)
   2989                           xlolims=xlolims, xuplims=xuplims,
   2990                           errorevery=errorevery, capthick=capthick, data=data,
-> 2991                           **kwargs)
   2992     finally:
   2993         ax._hold = washold

/opt/conda/lib/python3.6/site-packages/matplotlib/__init__.py in inner(ax, *args, **kwargs)
   1865                         "the Matplotlib list!)" % (label_namer, func.__name__),
   1866                         RuntimeWarning, stacklevel=2)
-> 1867             return func(ax, *args, **kwargs)
   1868 
   1869         inner.__doc__ = _add_data_doc(inner.__doc__,

/opt/conda/lib/python3.6/site-packages/matplotlib/axes/_axes.py in errorbar(self, x, y, yerr, xerr, fmt, ecolor, elinewidth, capsize, barsabove, lolims, uplims, xlolims, xuplims, errorevery, capthick, **kwargs)
   3324                 xo, _ = xywhere(x, lower, noylims & everymask)
   3325                 lo, uo = xywhere(lower, upper, noylims & everymask)
-> 3326                 barcols.append(self.vlines(xo, lo, uo, **eb_lines_style))
   3327                 if capsize > 0:
   3328                     caplines.append(mlines.Line2D(xo, lo, marker='_',

/opt/conda/lib/python3.6/site-packages/matplotlib/__init__.py in inner(ax, *args, **kwargs)
   1865                         "the Matplotlib list!)" % (label_namer, func.__name__),
   1866                         RuntimeWarning, stacklevel=2)
-> 1867             return func(ax, *args, **kwargs)
   1868 
   1869         inner.__doc__ = _add_data_doc(inner.__doc__,

/opt/conda/lib/python3.6/site-packages/matplotlib/axes/_axes.py in vlines(self, x, ymin, ymax, colors, linestyles, label, **kwargs)
   1031 
   1032         # We do the conversion first since not all unitized data is uniform
-> 1033         x = self.convert_xunits(x)
   1034         ymin = self.convert_yunits(ymin)
   1035         ymax = self.convert_yunits(ymax)

/opt/conda/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):

/opt/conda/lib/python3.6/site-packages/matplotlib/axis.py in convert_units(self, x)
   1524             return x
   1525 
-> 1526         ret = self.converter.convert(x, self.units, self)
   1527         return ret
   1528 

/opt/conda/lib/python3.6/site-packages/matplotlib/dates.py in convert(value, unit, axis)
   1814         The *unit* and *axis* arguments are not used.
   1815         """
-> 1816         return date2num(value)
   1817 
   1818     @staticmethod

/opt/conda/lib/python3.6/site-packages/matplotlib/dates.py in date2num(d)
    450         if not d.size:
    451             return d
--> 452         return _to_ordinalf_np_vectorized(d)
    453 
    454 

/opt/conda/lib/python3.6/site-packages/numpy/lib/function_base.py in __call__(self, *args, **kwargs)
   1970             vargs.extend([kwargs[_n] for _n in names])
   1971 
-> 1972         return self._vectorize_call(func=func, args=vargs)
   1973 
   1974     def _get_ufunc_and_otypes(self, func, args):

/opt/conda/lib/python3.6/site-packages/numpy/lib/function_base.py in _vectorize_call(self, func, args)
   2040             res = func()
   2041         else:
-> 2042             ufunc, otypes = self._get_ufunc_and_otypes(func=func, args=args)
   2043 
   2044             # Convert args to object arrays first

/opt/conda/lib/python3.6/site-packages/numpy/lib/function_base.py in _get_ufunc_and_otypes(self, func, args)
   2000 
   2001             inputs = [arg.flat[0] for arg in args]
-> 2002             outputs = func(*inputs)
   2003 
   2004             # Performance note: profiling indicates that -- for simple

/opt/conda/lib/python3.6/site-packages/matplotlib/dates.py in _to_ordinalf(dt)
    253         tzi = UTC
    254 
--> 255     base = float(dt.toordinal())
    256 
    257     # If it's sufficiently datetime-like, it will have a `date()` method

AttributeError: 'numpy.datetime64' object has no attribute 'toordinal'

Expected outcome

The command should succeed, and the correct date should be shown on the x axis (as happens when yerr is not specified). This did work in previous versions of matplotlib, but I don't know exactly which ones.

Matplotlib version

  • Operating system: linux
  • Matplotlib version: 2.2.3
  • Matplotlib backend (print(matplotlib.get_backend())):
    module://ipykernel.pylab.backend_inline
  • Python version: 3.6
  • Jupyter version (if applicable):
  • Other libraries: numpy 1.15.1

everything installed from conda

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions