Skip to content

Commit 4bef190

Browse files
timhoffmQuLogic
andauthored
FIX: Handle height units in broken_barh() (matplotlib#31685)
* FIX: Handle height units in brohen_barh() * Apply suggestions from code review Co-authored-by: Elliott Sales de Andrade <quantum.analyst@gmail.com> --------- Co-authored-by: Elliott Sales de Andrade <quantum.analyst@gmail.com>
1 parent 80d8911 commit 4bef190

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
@@ -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,20 @@ 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)
30413043

30423044
_api.check_in_list(['bottom', 'center', 'top'], align=align)
30433045
if align == "bottom":
3044-
y0, y1 = self.convert_yunits((y0, y0 + dy))
3046+
y1 = y0 + dy
30453047
elif align == "center":
3046-
y0, y1 = self.convert_yunits((y0 - dy/2, y0 + dy/2))
3048+
y0, y1 = y0 - dy / 2, y0 + dy / 2
30473049
else:
3048-
y0, y1 = self.convert_yunits((y0 - dy, y0))
3050+
y0, y1 = y0 - dy, y0
30493051

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

lib/matplotlib/tests/test_axes.py

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

77497749

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

0 commit comments

Comments
 (0)