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

Skip to content

Commit bdbc78e

Browse files
committed
Use setters in constructor, add docstring to setters and improve tests
1 parent 013f69b commit bdbc78e

File tree

2 files changed

+27
-7
lines changed

2 files changed

+27
-7
lines changed

lib/matplotlib/collections.py

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1763,9 +1763,9 @@ def __init__(self, widths, heights, angles, *, units='points', **kwargs):
17631763
Forwarded to `Collection`.
17641764
"""
17651765
super().__init__(**kwargs)
1766-
self._widths = 0.5 * np.asarray(widths).ravel()
1767-
self._heights = 0.5 * np.asarray(heights).ravel()
1768-
self._angles = np.deg2rad(angles).ravel()
1766+
self._set_widths(widths)
1767+
self._set_heights(heights)
1768+
self._set_angles(angles)
17691769
self._units = units
17701770
self.set_transform(transforms.IdentityTransform())
17711771
self._transforms = np.empty((0, 3, 3))
@@ -1813,16 +1813,28 @@ def _set_transforms(self):
18131813
m[:2, 2:] = 0
18141814
self.set_transform(_affine(m))
18151815

1816-
def set_widths(self, widths):
1816+
def _set_widths(self, widths):
18171817
self._widths = 0.5 * np.asarray(widths).ravel()
1818-
self.stale = True
18191818

1820-
def set_angles(self, angles):
1819+
def _set_heights(self, heights):
1820+
self._heights = 0.5 * np.asarray(heights).ravel()
1821+
1822+
def _set_angles(self, angles):
18211823
self._angles = np.deg2rad(angles).ravel()
1824+
1825+
def set_widths(self, widths):
1826+
"""Set the lengths of the first axes (e.g., major axis lengths)."""
1827+
self._set_widths(widths)
18221828
self.stale = True
18231829

18241830
def set_heights(self, heights):
1825-
self._heights = 0.5 * np.asarray(heights).ravel()
1831+
"""Set the lengths of second axes.."""
1832+
self._set_heights(heights)
1833+
self.stale = True
1834+
1835+
def set_angles(self, angles):
1836+
"""Set the angles of the first axes, degrees CCW from the x-axis."""
1837+
self._set_angles(angles)
18261838
self.stale = True
18271839

18281840
@artist.allow_rasterization

lib/matplotlib/tests/test_collections.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -418,6 +418,10 @@ def test_EllipseCollection_setter():
418418
offset_transform=ax.transData,
419419
)
420420

421+
assert_array_almost_equal(ec._widths, np.array(widths).ravel() * 0.5)
422+
assert_array_almost_equal(ec._heights, np.array(heights).ravel() * 0.5)
423+
assert_array_almost_equal(ec._angles, np.deg2rad(angles).ravel())
424+
421425
ax.add_collection(ec)
422426
ax.set_xlim(-2, 12)
423427
ax.set_ylim(-2, 12)
@@ -428,6 +432,10 @@ def test_EllipseCollection_setter():
428432

429433
ec.set(widths=new_widths, heights=new_heights, angles=new_angles)
430434

435+
assert_array_almost_equal(ec._widths, np.array(new_widths).ravel() * 0.5)
436+
assert_array_almost_equal(ec._heights, np.array(new_heights).ravel() * 0.5)
437+
assert_array_almost_equal(ec._angles, np.deg2rad(new_angles).ravel())
438+
431439

432440
@image_comparison(['polycollection_close.png'], remove_text=True, style='mpl20')
433441
def test_polycollection_close():

0 commit comments

Comments
 (0)