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

Skip to content

Commit ed91d6e

Browse files
committed
Use setters in constructor, add docstring to setters and improve tests
1 parent b413be1 commit ed91d6e

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
@@ -1745,9 +1745,9 @@ def __init__(self, widths, heights, angles, *, units='points', **kwargs):
17451745
Forwarded to `Collection`.
17461746
"""
17471747
super().__init__(**kwargs)
1748-
self._widths = 0.5 * np.asarray(widths).ravel()
1749-
self._heights = 0.5 * np.asarray(heights).ravel()
1750-
self._angles = np.deg2rad(angles).ravel()
1748+
self._set_widths(widths)
1749+
self._set_heights(heights)
1750+
self._set_angles(angles)
17511751
self._units = units
17521752
self.set_transform(transforms.IdentityTransform())
17531753
self._transforms = np.empty((0, 3, 3))
@@ -1795,16 +1795,28 @@ def _set_transforms(self):
17951795
m[:2, 2:] = 0
17961796
self.set_transform(_affine(m))
17971797

1798-
def set_widths(self, widths):
1798+
def _set_widths(self, widths):
17991799
self._widths = 0.5 * np.asarray(widths).ravel()
1800-
self.stale = True
18011800

1802-
def set_angles(self, angles):
1801+
def _set_heights(self, heights):
1802+
self._heights = 0.5 * np.asarray(heights).ravel()
1803+
1804+
def _set_angles(self, angles):
18031805
self._angles = np.deg2rad(angles).ravel()
1806+
1807+
def set_widths(self, widths):
1808+
"""Set the lengths of the first axes (e.g., major axis lengths)."""
1809+
self._set_widths(widths)
18041810
self.stale = True
18051811

18061812
def set_heights(self, heights):
1807-
self._heights = 0.5 * np.asarray(heights).ravel()
1813+
"""Set the lengths of second axes.."""
1814+
self._set_heights(heights)
1815+
self.stale = True
1816+
1817+
def set_angles(self, angles):
1818+
"""Set the angles of the first axes, degrees CCW from the x-axis."""
1819+
self._set_angles(angles)
18081820
self.stale = True
18091821

18101822
@artist.allow_rasterization

lib/matplotlib/tests/test_collections.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -428,6 +428,10 @@ def test_EllipseCollection_setter():
428428
offset_transform=ax.transData,
429429
)
430430

431+
assert_array_almost_equal(ec._widths, np.array(widths).ravel() * 0.5)
432+
assert_array_almost_equal(ec._heights, np.array(heights).ravel() * 0.5)
433+
assert_array_almost_equal(ec._angles, np.deg2rad(angles).ravel())
434+
431435
ax.add_collection(ec)
432436
ax.set_xlim(-2, 12)
433437
ax.set_ylim(-2, 12)
@@ -438,6 +442,10 @@ def test_EllipseCollection_setter():
438442

439443
ec.set(widths=new_widths, heights=new_heights, angles=new_angles)
440444

445+
assert_array_almost_equal(ec._widths, np.array(new_widths).ravel() * 0.5)
446+
assert_array_almost_equal(ec._heights, np.array(new_heights).ravel() * 0.5)
447+
assert_array_almost_equal(ec._angles, np.deg2rad(new_angles).ravel())
448+
441449

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

0 commit comments

Comments
 (0)