Skip to content

Commit 1cd5c7b

Browse files
authored
Merge pull request matplotlib#29716 from timhoffm/broken_barh_align
ENH: Add align parameter to broken_barh()
2 parents 6df7e34 + fb77685 commit 1cd5c7b

File tree

6 files changed

+52
-11
lines changed

6 files changed

+52
-11
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
``broken_barh()`` vertical alignment though ``align`` parameter
2+
---------------------------------------------------------------
3+
`~.Axes.broken_barh` now supports vertical alignment of the bars through the
4+
``align`` parameter.

galleries/examples/lines_bars_and_markers/broken_barh.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,13 @@
1818
network = np.column_stack([10*np.random.random(10), np.full(10, 0.05)])
1919

2020
fig, ax = plt.subplots()
21-
# broken_barh(xranges, (ymin, height))
22-
ax.broken_barh(cpu_1, (-0.2, 0.4))
23-
ax.broken_barh(cpu_2, (0.8, 0.4))
24-
ax.broken_barh(cpu_3, (1.8, 0.4))
25-
ax.broken_barh(cpu_4, (2.8, 0.4))
26-
ax.broken_barh(disk, (3.8, 0.4), color="tab:orange")
27-
ax.broken_barh(network, (4.8, 0.4), color="tab:green")
21+
# 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")
2828
ax.set_xlim(0, 10)
2929
ax.set_yticks(range(6),
3030
labels=["CPU 1", "CPU 2", "CPU 3", "CPU 4", "disk", "network"])

lib/matplotlib/axes/_axes.py

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2966,7 +2966,7 @@ def sign(x):
29662966

29672967
@_preprocess_data()
29682968
@_docstring.interpd
2969-
def broken_barh(self, xranges, yrange, **kwargs):
2969+
def broken_barh(self, xranges, yrange, align="bottom", **kwargs):
29702970
"""
29712971
Plot a horizontal sequence of rectangles.
29722972
@@ -2979,8 +2979,16 @@ def broken_barh(self, xranges, yrange, **kwargs):
29792979
The x-positions and extents of the rectangles. For each tuple
29802980
(*xmin*, *xwidth*) a rectangle is drawn from *xmin* to *xmin* +
29812981
*xwidth*.
2982-
yrange : (*ymin*, *yheight*)
2982+
yrange : (*ypos*, *yheight*)
29832983
The y-position and extent for all the rectangles.
2984+
align : {"bottom", "center", "top"}, default: 'bottom'
2985+
The alignment of the yrange with respect to the y-position. One of:
2986+
2987+
- "bottom": Resulting y-range [ypos, ypos + yheight]
2988+
- "center": Resulting y-range [ypos - yheight/2, ypos + yheight/2]
2989+
- "top": Resulting y-range [ypos - yheight, ypos]
2990+
2991+
.. versionadded:: 3.11
29842992
29852993
Returns
29862994
-------
@@ -3015,7 +3023,15 @@ def broken_barh(self, xranges, yrange, **kwargs):
30153023

30163024
vertices = []
30173025
y0, dy = yrange
3018-
y0, y1 = self.convert_yunits((y0, y0 + dy))
3026+
3027+
_api.check_in_list(['bottom', 'center', 'top'], align=align)
3028+
if align == "bottom":
3029+
y0, y1 = self.convert_yunits((y0, y0 + dy))
3030+
elif align == "center":
3031+
y0, y1 = self.convert_yunits((y0 - dy/2, y0 + dy/2))
3032+
else:
3033+
y0, y1 = self.convert_yunits((y0 - dy, y0))
3034+
30193035
for xr in xranges: # convert the absolute values, not the x and dx
30203036
try:
30213037
x0, dx = xr

lib/matplotlib/axes/_axes.pyi

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,7 @@ class Axes(_AxesBase):
271271
self,
272272
xranges: Sequence[tuple[float, float]],
273273
yrange: tuple[float, float],
274+
align: Literal["bottom", "center", "top"] = ...,
274275
*,
275276
data=...,
276277
**kwargs

lib/matplotlib/pyplot.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3222,12 +3222,17 @@ def boxplot(
32223222
def broken_barh(
32233223
xranges: Sequence[tuple[float, float]],
32243224
yrange: tuple[float, float],
3225+
align: Literal["bottom", "center", "top"] = "bottom",
32253226
*,
32263227
data=None,
32273228
**kwargs,
32283229
) -> PolyCollection:
32293230
return gca().broken_barh(
3230-
xranges, yrange, **({"data": data} if data is not None else {}), **kwargs
3231+
xranges,
3232+
yrange,
3233+
align=align,
3234+
**({"data": data} if data is not None else {}),
3235+
**kwargs,
32313236
)
32323237

32333238

lib/matplotlib/tests/test_axes.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7330,6 +7330,21 @@ def test_broken_barh_timedelta():
73307330
assert pp.get_paths()[0].vertices[2, 0] == mdates.date2num(d0) + 1 / 24
73317331

73327332

7333+
def test_broken_barh_align():
7334+
fig, ax = plt.subplots()
7335+
pc = ax.broken_barh([(0, 10)], (0, 2))
7336+
for path in pc.get_paths():
7337+
assert_array_equal(path.get_extents().intervaly, [0, 2])
7338+
7339+
pc = ax.broken_barh([(0, 10)], (10, 2), align="center")
7340+
for path in pc.get_paths():
7341+
assert_array_equal(path.get_extents().intervaly, [9, 11])
7342+
7343+
pc = ax.broken_barh([(0, 10)], (20, 2), align="top")
7344+
for path in pc.get_paths():
7345+
assert_array_equal(path.get_extents().intervaly, [18, 20])
7346+
7347+
73337348
def test_pandas_pcolormesh(pd):
73347349
time = pd.date_range('2000-01-01', periods=10)
73357350
depth = np.arange(20)

0 commit comments

Comments
 (0)