Skip to content

Commit 9726e24

Browse files
committed
Return a list of BarContainer
1 parent b62f516 commit 9726e24

File tree

3 files changed

+14
-8
lines changed

3 files changed

+14
-8
lines changed

lib/matplotlib/axes/_axes.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3140,6 +3140,10 @@ def grouped_bar(self, x, heights, *, group_spacing=1.5, bar_spacing=0,
31403140
31413141
%(Rectangle:kwdoc)s
31423142
3143+
Returns
3144+
-------
3145+
A list of `.BarContainer` instances, one for each dataset.
3146+
31433147
"""
31443148
if hasattr(heights, 'keys'):
31453149
if labels is not None:
@@ -3196,24 +3200,26 @@ def grouped_bar(self, x, heights, *, group_spacing=1.5, bar_spacing=0,
31963200

31973201
# place the bars, but only use numerical positions, categorical tick labels
31983202
# are handled separately below
3203+
result = []
31993204
for i, (hs, label, color) in enumerate(
32003205
zip(heights, labels, colors)):
32013206
lefts = (group_centers - 0.5 * group_distance + margin_abs
32023207
+ i * (bar_width + bar_spacing_abs))
32033208
if orientation == "vertical":
3204-
self.bar(lefts, hs, width=bar_width, align="edge",
3205-
label=label, color=color, **kwargs)
3209+
bc = self.bar(lefts, hs, width=bar_width, align="edge",
3210+
label=label, color=color, **kwargs)
32063211
else:
3207-
self.barh(lefts, hs, height=bar_width, align="edge",
3208-
label=label, color=color, **kwargs)
3212+
bc = self.barh(lefts, hs, height=bar_width, align="edge",
3213+
label=label, color=color, **kwargs)
3214+
result.append(bc)
32093215

32103216
if tick_labels is not None:
32113217
if orientation == "vertical":
32123218
self.xaxis.set_ticks(group_centers, labels=tick_labels)
32133219
else:
32143220
self.yaxis.set_ticks(group_centers, labels=tick_labels)
32153221

3216-
# TODO: does not return anything for now
3222+
return result
32173223

32183224
@_preprocess_data()
32193225
def stem(self, *args, linefmt=None, markerfmt=None, basefmt=None, bottom=0,

lib/matplotlib/axes/_axes.pyi

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ class Axes(_AxesBase):
290290
orientation: Literal["vertical", "horizontal"] = ...,
291291
colors: Iterable[ColorType] | None = ...,
292292
**kwargs
293-
) -> None: ...
293+
) -> list[BarContainer]: ...
294294
def stem(
295295
self,
296296
*args: ArrayLike | str,

lib/matplotlib/pyplot.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3400,8 +3400,8 @@ def grouped_bar(
34003400
orientation: Literal["vertical", "horizontal"] = "vertical",
34013401
colors: Iterable[ColorType] | None = None,
34023402
**kwargs,
3403-
) -> None:
3404-
gca().grouped_bar(
3403+
) -> list[BarContainer]:
3404+
return gca().grouped_bar(
34053405
x,
34063406
heights,
34073407
group_spacing=group_spacing,

0 commit comments

Comments
 (0)