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

Commitc92f106

Browse files
committed
Add parameter to use the center or a corner of the rectangle for the position
1 parent4ff193c commitc92f106

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+
ifkwargs.pop("centered",False):
1892+
self._path_generator=mpath.Path.unit_rectangle_centered
1893+
super().__init__(*args,**kwargs)
1894+
18871895

18881896
classPatchCollection(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
returncls._unit_rectangle
743743

744+
_unit_rectangle_centered=None
745+
746+
@classmethod
747+
defunit_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+
ifcls._unit_rectangle_centeredisNone:
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+
returncls._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-
deftest_WidthHeightAngleCollection_setter_getter(Class,scale):
439+
deftest_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+
ifcenteredisnotNone:
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)

[8]ページ先頭

©2009-2025 Movatter.jp