10000 FIX: make bar work with timedeltas · matplotlib/matplotlib@15edda9 · GitHub
[go: up one dir, main page]

Skip to content

Commit 15edda9

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

File tree

1 file changed

+26
-13
lines changed

1 file changed

+26
-13
lines changed

lib/matplotlib/axes/_axes.py

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2063,6 +2063,19 @@ 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+
try:
2070+
# attempt to add the width to x0; this works for
2071+
# datetime+timedelta, for instance
2072+
dx = convert(x0 + dx) - x
2073+
except (TypeError, AttributeError):
2074+
# but doesn't work for 'string' + float, so just
2075+
# see if the converter works on the float.
2076+
dx = convert(dx)
2077+
return dx
2078+
20662079
@_preprocess_data(replace_names=["x", "left",
20672080
"height", "width",
20682081
"y", "bottom",
@@ -2232,23 +2245,25 @@ def bar(self, x, height, width=0.8, bottom=None, *, align="center",
22322245
else:
22332246
raise ValueError('invalid orientation: %s' % orientation)
22342247

2248+
x, height, width, y, linewidth = np.broadcast_arrays(
2249+
# Make args iterable too.
2250+
np.atleast_1d(x), height, width, y, linewidth)
2251+
22352252
# lets do some conversions now since some types cannot be
22362253
# subtracted uniformly
22372254
if self.xaxis is not None:
2255+
x0 = x
22382256
x = self.convert_xunits(x)
2239-
width = self.convert_xunits(width)
2257+
width = self._convert_dx(x0, x, width, self.convert_xunits)
22402258
if xerr is not None:
2241-
xerr = self.convert_xunits(xerr)
2259+
xerr = self._convert_dx(x0, x, xerr, self.convert_xunits)
22422260

22432261
if self.yaxis is not None:
2262+
y0 = y
22442263
y = self.convert_yunits(y)
2245-
height = self.convert_yunits(height)
2264+
height = self._convert_dx(y0, y, height, self.convert_yunits)
22462265
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)
2266+
yerr = self._convert_dx(y0, y, yerr, self.convert_yunits)
22522267

22532268
# Now that units have been converted, set the tick locations.
22542269
if orientation == 'vertical':
@@ -2528,11 +2543,9 @@ def broken_barh(self, xranges, yrange, **kwargs):
25282543
xnew = []
25292544
for xr in xranges:
25302545
# convert the absolute values, not the x and dx...
2531-
x0 = [self.convert_xunits(xr[0]),
2532-
self.convert_xunits(xr[0]+xr[1])]
2533-
# make a dx again...
2534-
x0[1] = x0[1] - x0[0]
2535-
xnew.append(x0)
2546+
x0 = self.convert_xunits(xr[0])
2547+
x1 = self._convert_dx(xr[0], x0, xr[1], self.convert_xunits)
2548+
xnew.append((x0, x1))
25362549

25372550
yrange = self.convert_yunits(yrange)
25382551

0 commit comments

Comments
 (0)
0