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

Skip to content

Commit 14a09aa

Browse files
committed
Add parameter to use the center or a corner of the rectangle for the position
1 parent 78de816 commit 14a09aa

File tree

4 files changed

+34
-3
lines changed

4 files changed

+34
-3
lines changed

lib/matplotlib/collections.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1874,14 +1874,22 @@ class RectangleCollection(_CollectionWithWidthHeightAngle):
18741874
refer to the *offsets* data units. 'xy' differs from all others in
18751875
that the angle as plotted varies with the aspect ratio, and equals
18761876
the specified angle only when the aspect ratio is unity. Hence
1877-
it behaves the same as the `~.patches.Ellipse` with
1877+
it behaves the same as the `~.patches.Rectangle` with
18781878
``axes.transData`` as its transform.
1879+
centered : bool
1880+
Whether to use the center or the corner of the rectangle to
1881+
define the position of the rectangles. Default is False.
18791882
**kwargs
18801883
Forwarded to `Collection`.
18811884
18821885
"""
18831886
_path_generator = mpath.Path.unit_rectangle
18841887

1888+
def __init__(self, *args, **kwargs):
1889+
if kwargs.pop("centered", False):
1890+
self._path_generator = mpath.Path.unit_rectangle_centered
1891+
super().__init__(*args, **kwargs)
1892+
18851893

18861894
class PatchCollection(Collection):
18871895
"""

lib/matplotlib/path.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -741,6 +741,20 @@ def unit_rectangle(cls):
741741
closed=True, readonly=True)
742742
return cls._unit_rectangle
743743

744+
_unit_rectangle_centered = None
745+
746+
@classmethod
747+
def unit_rectangle_centered(cls):
748+
"""
749+
Return a `Path` instance of the unit rectangle from (-0.5, -0.5) to (0.5, 0.5).
750+
"""
751+
if cls._unit_rectangle_centered is None:
752+
cls._unit_rectangle_centered = cls(
753+
[[-0.5, -0.5], [0.5, -0.5], [0.5, 0.5], [-0.5, 0.5], [-0.5, -0.5]],
754+
closed=True, readonly=True
755+
)
756+
return cls._unit_rectangle_centered
757+
744758
_unit_regular_polygons = WeakValueDictionary()
745759

746760
@classmethod

lib/matplotlib/path.pyi

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,8 @@ class Path:
101101
@classmethod
102102
def unit_rectangle(cls) -> Path: ...
103103
@classmethod
104+
def unit_rectangle_centered(cls) -> Path: ...
105+
@classmethod
104106
def unit_regular_polygon(cls, numVertices: int) -> Path: ...
105107
@classmethod
106108
def unit_regular_star(cls, numVertices: int, innerCircle: float = ...) -> Path: ...

lib/matplotlib/tests/test_collections.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -431,9 +431,12 @@ def test_RectangleCollection():
431431

432432

433433
@pytest.mark.parametrize(
434-
'Class', [mcollections.EllipseCollection, mcollections.RectangleCollection]
434+
'Class, centered',
435+
[(mcollections.EllipseCollection, None),
436+
(mcollections.RectangleCollection, False),
437+
(mcollections.RectangleCollection, True)]
435438
)
436-
def test_WidthHeightAngleCollection_setter_getter(Class):
439+
def test_WidthHeightAngleCollection_setter_getter(Class, centered):
437440
# Test widths, heights and angle setter
438441
rng = np.random.default_rng(0)
439442

@@ -444,13 +447,17 @@ def test_WidthHeightAngleCollection_setter_getter(Class):
444447

445448
fig, ax = plt.subplots()
446449

450+
kwargs = {}
451+
if centered is not None:
452+
kwargs["centered"] = centered
447453
ec = Class(
448454
widths=widths,
449455
heights=heights,
450456
angles=angles,
451457
offsets=offsets,
452458
units='x',
453459
offset_transform=ax.transData,
460+
**kwargs,
454461
)
455462

456463
assert_array_almost_equal(ec._widths, np.array(widths).ravel())

0 commit comments

Comments
 (0)