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

Skip to content

Commit c92f106

Browse files
committed
Add parameter to use the center or a corner of the rectangle for the position
1 parent 4ff193c commit c92f106

File tree

3 files changed

+32
-4
lines changed

3 files changed

+32
-4
lines changed

lib/matplotlib/collections.py

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

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

18881896
class PatchCollection(Collection):
18891897
"""

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/tests/test_collections.py

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

432432

433433
@pytest.mark.parametrize(
434-
'Class,scale',
435-
[(mcollections.EllipseCollection, 0.5), (mcollections.RectangleCollection, 1)]
434+
'Class, scale, centered',
435+
[(mcollections.EllipseCollection, 0.5, None),
436+
(mcollections.RectangleCollection, 1, False),
437+
(mcollections.RectangleCollection, 1, True)]
436438
)
437-
def test_WidthHeightAngleCollection_setter_getter(Class, scale):
439+
def test_WidthHeightAngleCollection_setter_getter(Class, scale, centered):
438440
# Test widths, heights and angle setter
439441
rng = np.random.default_rng(0)
440442

@@ -445,13 +447,17 @@ def test_WidthHeightAngleCollection_setter_getter(Class, scale):
445447

446448
fig, ax = plt.subplots()
447449

450+
kwargs = {}
451+
if centered is not None:
452+
kwargs["centered"] = centered
448453
ec = Class(
449454
widths=widths,
450455
heights=heights,
451456
angles=angles,
452457
offsets=offsets,
453458
units='x',
454459
offset_transform=ax.transData,
460+
**kwargs,
455461
)
456462

457463
assert_array_almost_equal(ec._widths, np.array(widths).ravel() * scale)

0 commit comments

Comments
 (0)