Merge pull request #32282 from robertoffmoura/add-grouped-bar-metadata · matplotlib/matplotlib@8a621d3 · GitHub
Skip to content

Commit 8a621d3

Browse files
authored
Merge pull request #32282 from robertoffmoura/add-grouped-bar-metadata
Add group_positions attribute to BarContainer
2 parents 4077a5e + f345a98 commit 8a621d3

5 files changed

Lines changed: 44 additions & 1 deletion

File tree

Lines changed: 16 additions & 0 deletions

lib/matplotlib/axes/_axes.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3363,6 +3363,7 @@ def grouped_bar(self, heights, *, positions=None, group_spacing=1.5, bar_spacing
33633363
else:
33643364
bc = self.barh(lefts, hs, height=bar_width, align="edge",
33653365
label=label, color=color, **styles, **kwargs)
3366+
bc.group_positions = group_centers
33663367
bar_containers.append(bc)
33673368

33683369
if tick_labels is not None:

lib/matplotlib/container.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,14 +63,20 @@ class BarContainer(Container):
6363
If 'vertical', the bars are assumed to be vertical.
6464
If 'horizontal', the bars are assumed to be horizontal.
6565
66+
group_positions : None or array-like
67+
The center positions of the bar groups if the container is part of a
68+
grouped bar plot (e.g. created by `.Axes.grouped_bar`). *None* otherwise.
69+
70+
.. versionadded:: 3.12
6671
"""
6772

6873
def __init__(self, patches, errorbar=None, *, datavalues=None,
69-
orientation=None, **kwargs):
74+
orientation=None, group_positions=None, **kwargs):
7075
self.patches = patches
7176
self.errorbar = errorbar
7277
self.datavalues = datavalues
7378
self.orientation = orientation
79+
self.group_positions = group_positions
7480
super().__init__(patches, **kwargs)
7581

7682
@property
@@ -115,6 +121,20 @@ def position_centers(self):
115121
else:
116122
raise ValueError("orientation must be 'vertical' or 'horizontal'.")
117123

124+
@property
125+
def widths(self):
126+
"""
127+
Return the widths of the bars.
128+
129+
.. versionadded:: 3.12
130+
"""
131+
if self.orientation == 'vertical':
132+
return [p.get_width() for p in self.patches]
133+
elif self.orientation == 'horizontal':
134+
return [p.get_height() for p in self.patches]
135+
else:
136+
raise ValueError("orientation must be 'vertical' or 'horizontal'.")
137+
118138

119139
class ErrorbarContainer(Container):
120140
"""

lib/matplotlib/container.pyi

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,15 @@ class BarContainer(Container):
2525
errorbar: None | ErrorbarContainer
2626
datavalues: None | ArrayLike
2727
orientation: None | Literal["vertical", "horizontal"]
28+
group_positions: None | ArrayLike
2829
def __init__(
2930
self,
3031
patches: list[Rectangle],
3132
errorbar: ErrorbarContainer | None = ...,
3233
*,
3334
datavalues: ArrayLike | None = ...,
3435
orientation: Literal["vertical", "horizontal"] | None = ...,
36+
group_positions: ArrayLike | None = ...,
3537
**kwargs
3638
) -> None: ...
3739
@property
@@ -40,6 +42,8 @@ class BarContainer(Container):
4042
def tops(self) -> list[float]: ...
4143
@property
4244
def position_centers(self) -> list[float]: ...
45+
@property
46+
def widths(self) -> list[float]: ...
4347

4448
class ErrorbarContainer(Container):
4549
lines: tuple[Line2D, tuple[Line2D, ...], tuple[LineCollection, ...]]

lib/matplotlib/tests/test_axes.py

Lines changed: 2 additions & 0 deletions

0 commit comments

Comments
 (0)