Skip to content

Commit df6e976

Browse files
committed
FIX: make bar work with timedeltas
1 parent 3e8129b commit df6e976

File tree

1 file changed

+24
-8
lines changed

1 file changed

+24
-8
lines changed

lib/matplotlib/axes/_axes.py

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2063,6 +2063,20 @@ def step(self, x, y, *args, where='pre', **kwargs):
20632063
kwargs['drawstyle'] = 'steps-' + where
20642064
return self.plot(x, y, *args, **kwargs)
20652065

2066+
@staticmethod
2067+
def _convert_dx(x0, x, dx, convert):
2068+
""" Small helper to do logic of width conversion flexibly """
2069+
print(x0, x, dx, convert)
2070+
try:
2071+
# attempt to add the width to x0; this works for
2072+
# datetime+timedelta, for instance
2073+
dx = convert(x0 + dx) - x
2074+
except (TypeError, AttributeError):
2075+
# but doesn't work for 'string' + float, so just
2076+
# see if the converter works on the float.
2077+
dx = convert(dx)
2078+
return dx
2079+
20662080
@_preprocess_data(replace_names=["x", "left",
20672081
"height", "width",
20682082
"y", "bottom",
@@ -2232,23 +2246,25 @@ def bar(self, x, height, width=0.8, bottom=None, *, align="center",
22322246
else:
22332247
raise ValueError('invalid orientation: %s' % orientation)
22342248

2249+
x, height, width, y, linewidth = np.broadcast_arrays(
2250+
# Make args iterable too.
2251+
np.atleast_1d(x), height, width, y, linewidth)
2252+
22352253
# lets do some conversions now since some types cannot be
22362254
# subtracted uniformly
22372255
if self.xaxis is not None:
2256+
x0 = x
22382257
x = self.convert_xunits(x)
2239-
width = self.convert_xunits(width)
2258+
width = self._convert_dx(x0, x, width, self.convert_xunits)
22402259
if xerr is not None:
2241-
xerr = self.convert_xunits(xerr)
2260+
xerr = self._convert_dx(x0, x, xerr, self.convert_xunits)
22422261

22432262
if self.yaxis is not None:
2263+
y0 = y
22442264
y = self.convert_yunits(y)
2245-
height = self.convert_yunits(height)
2265+
height = self._convert_dx(y0, y, height, self.convert_yunits)
22462266
if yerr is not None:
2247-
yerr = self.convert_yunits(yerr)
2248-
2249-
x, height, width, y, linewidth = np.broadcast_arrays(
2250-
# Make args iterable too.
2251-
np.atleast_1d(x), height, width, y, linewidth)
2267+
yerr = self._convert_dx(y0, y, yerr, self.convert_yunits)
22522268

22532269
# Now that units have been converted, set the tick locations.
22542270
if orientation == 'vertical':

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy