Skip to content

Commit 39497f5

Browse files
committed
FIX: Handle height units in brohen_barh()
1 parent 7499f38 commit 39497f5

2 files changed

Lines changed: 27 additions & 11 deletions

File tree

lib/matplotlib/axes/_axes.py

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2243,13 +2243,10 @@ def _convert_dx(dx, x0, xconv, convert):
22432243
except (TypeError, IndexError, KeyError):
22442244
x = xconv
22452245

2246-
delist = False
2247-
if not np.iterable(dx):
2248-
dx = [dx]
2249-
delist = True
2250-
dx = [convert(x0 + ddx) - x for ddx in dx]
2251-
if delist:
2252-
dx = dx[0]
2246+
if np.iterable(dx):
2247+
dx = [convert(x0 + ddx) - x for ddx in dx]
2248+
else:
2249+
dx = convert(x0 + dx) - x
22532250
except (ValueError, TypeError, AttributeError):
22542251
# if the above fails (for any reason) just fallback to what
22552252
# we do by default and convert dx by itself.
@@ -3037,15 +3034,21 @@ def broken_barh(self, xranges, yrange, align="bottom", **kwargs):
30373034
[("x", xdata), ("y", ydata)], kwargs, convert=False)
30383035

30393036
vertices = []
3040-
y0, dy = yrange
3037+
ypos, height = yrange
3038+
3039+
# Unit conversion: handling of the difference quantity height is done through
3040+
# _convert_dx() in the same way as width handling in bar().
3041+
y0 = self.convert_yunits(ypos)
3042+
dy = self._convert_dx(height, ypos, np.array(y0), self.convert_yunits)
3043+
print(type(y0), type(dy))
30413044

30423045
_api.check_in_list(['bottom', 'center', 'top'], align=align)
30433046
if align == "bottom":
3044-
y0, y1 = self.convert_yunits((y0, y0 + dy))
3047+
y1 = y0 + dy
30453048
elif align == "center":
3046-
y0, y1 = self.convert_yunits((y0 - dy/2, y0 + dy/2))
3049+
y0, y1 = y0 - dy / 2, y0 + dy / 2
30473050
else:
3048-
y0, y1 = self.convert_yunits((y0 - dy, y0))
3051+
y0, y1 = y0 - dy, y0
30493052

30503053
for xr in xranges: # convert the absolute values, not the x and dx
30513054
try:
@@ -3057,6 +3060,7 @@ def broken_barh(self, xranges, yrange, align="bottom", **kwargs):
30573060
x0, x1 = self.convert_xunits((x0, x0 + dx))
30583061
vertices.append([(x0, y0), (x0, y1), (x1, y1), (x1, y0)])
30593062

3063+
print(vertices)
30603064
col = mcoll.PolyCollection(np.array(vertices), **kwargs)
30613065
self.add_collection(col)
30623066

lib/matplotlib/tests/test_axes.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7748,6 +7748,18 @@ def test_broken_barh_align():
77487748
assert_array_equal(path.get_extents().intervaly, [18, 20])
77497749

77507750

7751+
def test_broken_barh_categorical():
7752+
fig, ax = plt.subplots()
7753+
pc = ax.broken_barh([(0, 10)], ('a', 0.8))
7754+
assert tuple(pc.get_datalim(ax.transData).intervaly) == (0, 0.8)
7755+
7756+
pc = ax.broken_barh([(0, 10)], ('a', 0.8), align="center")
7757+
assert tuple(pc.get_datalim(ax.transData).intervaly) == (-0.4, 0.4)
7758+
7759+
pc = ax.broken_barh([(0, 10)], ('a', 0.8), align="top")
7760+
assert tuple(pc.get_datalim(ax.transData).intervaly) == (-0.8, 0)
7761+
7762+
77517763
def test_pandas_pcolormesh(pd):
77527764
time = pd.date_range('2000-01-01', periods=10)
77537765
depth = np.arange(20)

0 commit comments

Comments
 (0)