Add group_positions attribute to BarContainer by robertoffmoura · Pull Request #32282 · matplotlib/matplotlib · GitHub
Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions doc/release/next_whats_new/new_barcontainer_properties.rst
1 change: 1 addition & 0 deletions lib/matplotlib/axes/_axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -3363,6 +3363,7 @@ def grouped_bar(self, heights, *, positions=None, group_spacing=1.5, bar_spacing
else:
bc = self.barh(lefts, hs, height=bar_width, align="edge",
label=label, color=color, **styles, **kwargs)
bc.group_positions = group_centers
bar_containers.append(bc)

if tick_labels is not None:
Expand Down
22 changes: 21 additions & 1 deletion lib/matplotlib/container.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,20 @@ class BarContainer(Container):
If 'vertical', the bars are assumed to be vertical.
If 'horizontal', the bars are assumed to be horizontal.

group_positions : None or array-like
The center positions of the bar groups if the container is part of a
grouped bar plot (e.g. created by `.Axes.grouped_bar`). *None* otherwise.

.. versionadded:: 3.12
"""

def __init__(self, patches, errorbar=None, *, datavalues=None,
orientation=None, **kwargs):
orientation=None, group_positions=None, **kwargs):
self.patches = patches
self.errorbar = errorbar
self.datavalues = datavalues
self.orientation = orientation
self.group_positions = group_positions
super().__init__(patches, **kwargs)

@property
Expand Down Expand Up @@ -115,6 +121,20 @@ def position_centers(self):
else:
raise ValueError("orientation must be 'vertical' or 'horizontal'.")

@property
def widths(self):
"""
Return the widths of the bars.

.. versionadded:: 3.12
"""
if self.orientation == 'vertical':
return [p.get_width() for p in self.patches]
elif self.orientation == 'horizontal':
return [p.get_height() for p in self.patches]
else:
raise ValueError("orientation must be 'vertical' or 'horizontal'.")


class ErrorbarContainer(Container):
"""
Expand Down
4 changes: 4 additions & 0 deletions lib/matplotlib/container.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,15 @@ class BarContainer(Container):
errorbar: None | ErrorbarContainer
datavalues: None | ArrayLike
orientation: None | Literal["vertical", "horizontal"]
group_positions: None | ArrayLike
def __init__(
self,
patches: list[Rectangle],
errorbar: ErrorbarContainer | None = ...,
*,
datavalues: ArrayLike | None = ...,
orientation: Literal["vertical", "horizontal"] | None = ...,
group_positions: ArrayLike | None = ...,
**kwargs
) -> None: ...
@property
Expand All @@ -40,6 +42,8 @@ class BarContainer(Container):
def tops(self) -> list[float]: ...
@property
def position_centers(self) -> list[float]: ...
@property
def widths(self) -> list[float]: ...

class ErrorbarContainer(Container):
lines: tuple[Line2D, tuple[Line2D, ...], tuple[LineCollection, ...]]
Expand Down
2 changes: 2 additions & 0 deletions lib/matplotlib/tests/test_axes.py
Loading