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
ax.bar doesn't work correctly when width is a timedelta64 object #11290
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]: <BarContainerobjectof100artists>
works fine, but if I set width to be the diff of the datetime64 array, which gives a timedelta64 array, things break:
Can you provide the full example with the timedelta64 array that produces that error? I've encountered similar looking problems before, that depend on when we do our unit conversions, and this might actually be fixed in the master branch.
x = self.convert_xunits(x)
width = self.convert_xunits(width)
However, we don't have a conversion implemented for timedelta64. A result, x is our internal float-based date format while width is still a timedelta64, which cannot be subtracted from each other.
The solution would be to implement a converter for timedelta64.
I just get a bunch of zeros; dividing np.diff(x) by a number seems to just zero it out. Do you know of a way to fix this? Internally Matplotlib has to divide the width by 2, which is why I can't get your example working.
'M8[D]' has a single day resolution, and your array goes up by one, so dividing by two rounds down to 0. You'd need to at least change it to hourly resolution.
@dstansby, not sure of the approach you are taking here, but I think as @timhoffm says, we should add timedelta64 to dates.py, and register it. Then all the math can be done in floating point numbers...
Trying to take that approach, but running into the problem that np.timedelta64 is a subclass of numbers.Number, so the conversion interface assumes it doesn't need converting:
Activity
dstansby commentedon May 25, 2018
Can you provide the full example with the
timedelta64array that produces that error? I've encountered similar looking problems before, that depend on when we do our unit conversions, and this might actually be fixed in the master branch.astrofrog commentedon May 25, 2018
@dstansby - the full example is:
timhoffm commentedon May 26, 2018
The problem is our unit conversion. We do
However, we don't have a conversion implemented for
timedelta64. A result,xis our internal float-based date format whilewidthis still atimedelta64, which cannot be subtracted from each other.The solution would be to implement a converter for
timedelta64.dstansby commentedon May 26, 2018
Ah, so this would be fixed by something like #9120 I think.
timhoffm commentedon May 26, 2018
Sort of. It needs a bit more infrastructure:
DateConvertermatplotlib.units.registrydstansby commentedon Sep 18, 2018
I'm having a look at this now, but having trouble with your example @astrofrog ; when I do
I just get a bunch of zeros; dividing
np.diff(x)by a number seems to just zero it out. Do you know of a way to fix this? InternallyMatplotlibhas to divide the width by 2, which is why I can't get your example working.QuLogic commentedon Sep 18, 2018
'M8[D]'has a single day resolution, and your array goes up by one, so dividing by two rounds down to 0. You'd need to at least change it to hourly resolution.jklymak commentedon Sep 18, 2018
@dstansby, not sure of the approach you are taking here, but I think as @timhoffm says, we should add timedelta64 to dates.py, and register it. Then all the math can be done in floating point numbers...
dstansby commentedon Sep 18, 2018
Trying to take that approach, but running into the problem that
np.timedelta64is a subclass ofnumbers.Number, so the conversion interface assumes it doesn't need converting:prints