Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

ENH: Add properties position_centers and tops to BarContainer#30226

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
timhoffm wants to merge1 commit intomatplotlib:main
base:main
Choose a base branch
Loading
fromtimhoffm:barcontainer_props
Open
Show file tree
Hide file tree
Changes fromall 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
4 changes: 4 additions & 0 deletionsdoc/users/next_whats_new/barcontainer_properties.rst
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
``BarContainer`` properties
---------------------------
`.BarContainer` gained two properties: `~.BarContainer.position_centers`
and `~.BarContainer.tops`.
28 changes: 28 additions & 0 deletionslib/matplotlib/container.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -73,6 +73,34 @@ def __init__(self, patches, errorbar=None, *, datavalues=None,
self.orientation = orientation
super().__init__(patches, **kwargs)

@property
def tops(self):
"""
Return the values at the upper end of the bars.

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

@property
def position_centers(self):
"""
Return the centers of bar positions.

.. versionadded:: 3.11
"""
if self.orientation == 'vertical':
return [p.get_x() + p.get_width() / 2 for p in self.patches]
elif self.orientation == 'horizontal':
return [p.get_y() + p.get_height() / 2 for p in self.patches]
else:
raise ValueError("Orientation must be 'vertical' or 'horizontal'.")


class ErrorbarContainer(Container):
"""
Expand Down
4 changes: 4 additions & 0 deletionslib/matplotlib/container.pyi
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -32,6 +32,10 @@ class BarContainer(Container):
orientation: Literal["vertical", "horizontal"] | None = ...,
**kwargs
) -> None: ...
@property
def position_centers(self) -> list[float]: ...
@property
def tops(self) -> list[float]: ...

class ErrorbarContainer(Container):
lines: tuple[Line2D, tuple[Line2D, ...], tuple[LineCollection, ...]]
Expand Down
16 changes: 16 additions & 0 deletionslib/matplotlib/tests/test_container.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
import numpy as np
from numpy.testing import assert_array_equal
import matplotlib.pyplot as plt


Expand DownExpand Up@@ -35,3 +36,18 @@ def test_nonstring_label():
# Test for #26824
plt.bar(np.arange(10), np.random.rand(10), label=1)
plt.legend()


def test_barcontainer_position_centers_and_tops():
fig, ax = plt.subplots()
pos = [1, 2, 4]
bottoms = np.array([1, 5, 3])
heights = np.array([2, 3, 4])

container = ax.bar(pos, heights, bottom=bottoms)
assert_array_equal(container.position_centers, pos)
assert_array_equal(container.tops, bottoms + heights)

container = ax.barh(pos, heights, left=bottoms)
assert_array_equal(container.position_centers, pos)
assert_array_equal(container.tops, bottoms + heights)
Loading

[8]ページ先頭

©2009-2025 Movatter.jp