Skip to content

ax.bar doesn't work correctly when width is a timedelta64 object #11290

@astrofrog

Description

@astrofrog

The following example

In [1]: x = np.array(range(100), dtype='M8[D]')

In [2]: ax = plt.subplot(1,1,1)

In [3]: y = np.random.random(100)

In [4]: ax.bar(x, y)
Out[4]: <BarContainer object of 100 artists>

works fine, but if I set width to be the diff of the datetime64 array, which gives a timedelta64 array, things break:

In [7]: ax.bar(x[:-1], y[:-1], width=np.diff(x))
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-7-f05692d600cb> in <module>()
----> 1 ax.bar(x[:-1], y[:-1], width=np.diff(x))

~/miniconda3/envs/dev/lib/python3.6/site-packages/matplotlib/__init__.py in inner(ax, *args, **kwargs)
   1853                         "the Matplotlib list!)" % (label_namer, func.__name__),
   1854                         RuntimeWarning, stacklevel=2)
-> 1855             return func(ax, *args, **kwargs)
   1856 
   1857         inner.__doc__ = _add_data_doc(inner.__doc__,

~/miniconda3/envs/dev/lib/python3.6/site-packages/matplotlib/axes/_axes.py in bar(self, *args, **kwargs)
   2257         if align == 'center':
   2258             if orientation == 'vertical':
-> 2259                 left = x - width / 2
   2260                 bottom = y
   2261             elif orientation == 'horizontal':

TypeError: ufunc subtract cannot use operands with types dtype('float64') and dtype('<m8[D]')

This should however be unambiguous, so it would be nice if it worked.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions