Skip to content

Commit bdb7bbe

Browse files
timhoffmmeeseeksmachine
authored andcommitted
Backport PR #31685: FIX: Handle height units in broken_barh()
1 parent 6782669 commit bdb7bbe

3 files changed

Lines changed: 32 additions & 20 deletions

File tree

galleries/examples/lines_bars_and_markers/broken_barh.py

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,14 @@
1919

2020
fig, ax = plt.subplots()
2121
# broken_barh(xranges, (ypos, height))
22-
ax.broken_barh(cpu_1, (0, 0.4), align="center")
23-
ax.broken_barh(cpu_2, (1, 0.4), align="center")
24-
ax.broken_barh(cpu_3, (2, 0.4), align="center")
25-
ax.broken_barh(cpu_4, (3, 0.4), align="center")
26-
ax.broken_barh(disk, (4, 0.4), align="center", color="tab:orange")
27-
ax.broken_barh(network, (5, 0.4), align="center", color="tab:green")
22+
ax.broken_barh(cpu_1, ("CPU 1", 0.4), align="center")
23+
ax.broken_barh(cpu_2, ("CPU 2", 0.4), align="center")
24+
ax.broken_barh(cpu_3, ("CPU 3", 0.4), align="center")
25+
ax.broken_barh(cpu_4, ("CPU 4", 0.4), align="center")
26+
ax.broken_barh(disk, ("disk", 0.4), align="center", color="tab:orange")
27+
ax.broken_barh(network, ("network", 0.4), align="center", color="tab:green")
2828
ax.set_xlim(0, 10)
29-
ax.set_yticks(range(6),
30-
labels=["CPU 1", "CPU 2", "CPU 3", "CPU 4", "disk", "network"])
31-
ax.invert_yaxis()
29+
ax.invert_yaxis() # order the bars from top to bottom
3230
ax.set_title("Resource usage")
3331

3432
plt.show()

lib/matplotlib/axes/_axes.py

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

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

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

30413043
_api.check_in_list(['bottom', 'center', 'top'], align=align)
30423044
if align == "bottom":
3043-
y0, y1 = self.convert_yunits((y0, y0 + dy))
3045+
y1 = y0 + dy
30443046
elif align == "center":
3045-
y0, y1 = self.convert_yunits((y0 - dy/2, y0 + dy/2))
3047+
y0, y1 = y0 - dy / 2, y0 + dy / 2
30463048
else:
3047-
y0, y1 = self.convert_yunits((y0 - dy, y0))
3049+
y0, y1 = y0 - dy, y0
30483050

30493051
for xr in xranges: # convert the absolute values, not the x and dx
30503052
try:

lib/matplotlib/tests/test_axes.py

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

77057705

7706+
def test_broken_barh_categorical():
7707+
fig, ax = plt.subplots()
7708+
pc = ax.broken_barh([(0, 10)], ('a', 0.8))
7709+
assert tuple(pc.get_datalim(ax.transData).intervaly) == (0, 0.8)
7710+
7711+
pc = ax.broken_barh([(0, 10)], ('a', 0.8), align="center")
7712+
assert tuple(pc.get_datalim(ax.transData).intervaly) == (-0.4, 0.4)
7713+
7714+
pc = ax.broken_barh([(0, 10)], ('a', 0.8), align="top")
7715+
assert tuple(pc.get_datalim(ax.transData).intervaly) == (-0.8, 0)
7716+
7717+
77067718
def test_pandas_pcolormesh(pd):
77077719
time = pd.date_range('2000-01-01', periods=10)
77087720
depth = np.arange(20)

0 commit comments

Comments
 (0)