Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
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
16 changes: 16 additions & 0 deletions doc/release/next_whats_new/new_barcontainer_properties.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
``BarContainer`` properties and attributes
------------------------------------------

`.BarContainer` gained a new `~.BarContainer.widths` property. It returns a
list of the widths of the individual bars in the container (the dimension
perpendicular to the bar height).

For standard bar plots (e.g. created by `.Axes.bar` or `.Axes.barh`), this
reflects the width of each bar in the plot. For grouped bar plots (e.g. created
by `.Axes.grouped_bar`), each `~.BarContainer` represents one group of bars across
categories, so `~.BarContainer.widths` returns the width of each
individual bar in that group, rather than the total width of the entire group.

Additionally, `.BarContainer` gained a new ``group_positions`` attribute, which
exposes the center positions of the bar groups if the container is part of a
grouped bar plot (e.g. created by `.Axes.grouped_bar`), or ``None`` otherwise.
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
Original file line number Diff line number Diff line change
Expand Up @@ -2296,6 +2296,8 @@ def test_grouped_bar_return_value():
for bc in ret.bar_containers:
assert isinstance(bc, BarContainer)
assert bc in ax.containers
np.testing.assert_array_equal(bc.group_positions, [0, 1, 2])
assert bc.widths == [p.get_width() for p in bc.patches]

ret.remove()
for bc in ret.bar_containers:
Expand Down
Loading