-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Add widths
, heights
and angles
setter to EllipseCollection
#26375
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
5a9cd4e
673fb6a
f7be1b6
e4c5938
45a6a6f
729467c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
Add ``widths``, ``heights`` and ``angles`` setter to ``EllipseCollection`` | ||
-------------------------------------------------------------------------- | ||
|
||
The ``widths``, ``heights`` and ``angles`` values of the `~matplotlib.collections.EllipseCollection` | ||
can now be changed after the collection has been created. | ||
|
||
.. plot:: | ||
:include-source: true | ||
|
||
import matplotlib.pyplot as plt | ||
from matplotlib.collections import EllipseCollection | ||
import numpy as np | ||
|
||
rng = np.random.default_rng(0) | ||
|
||
widths = (2, ) | ||
heights = (3, ) | ||
angles = (45, ) | ||
offsets = rng.random((10, 2)) * 10 | ||
|
||
fig, ax = plt.subplots() | ||
|
||
ec = EllipseCollection( | ||
widths=widths, | ||
heights=heights, | ||
angles=angles, | ||
offsets=offsets, | ||
units='x', | ||
offset_transform=ax.transData, | ||
) | ||
|
||
ax.add_collection(ec) | ||
ax.set_xlim(-2, 12) | ||
ax.set_ylim(-2, 12) | ||
|
||
new_widths = rng.random((10, 2)) * 2 | ||
new_heights = rng.random((10, 2)) * 3 | ||
new_angles = rng.random((10, 2)) * 180 | ||
|
||
ec.set(widths=new_widths, heights=new_heights, angles=new_angles) |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -408,6 +408,49 @@ def test_EllipseCollection(): | |
ax.autoscale_view() | ||
|
||
|
||
def test_EllipseCollection_setter_getter(): | ||
# Test widths, heights and angle setter | ||
rng = np.random.default_rng(0) | ||
|
||
widths = (2, ) | ||
heights = (3, ) | ||
angles = (45, ) | ||
offsets = rng.random((10, 2)) * 10 | ||
|
||
fig, ax = plt.subplots() | ||
|
||
ec = mcollections.EllipseCollection( | ||
widths=widths, | ||
heights=heights, | ||
angles=angles, | ||
offsets=offsets, | ||
units='x', | ||
offset_transform=ax.transData, | ||
) | ||
|
||
assert_array_almost_equal(ec._widths, np.array(widths).ravel() * 0.5) | ||
assert_array_almost_equal(ec._heights, np.array(heights).ravel() * 0.5) | ||
assert_array_almost_equal(ec._angles, np.deg2rad(angles).ravel()) | ||
Comment on lines
+431
to
+433
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we need to check internals now that we have the getters? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These check that the internal values are correct (multiplication of 0.5) and these are different from the values returned by the getter, so I would say that this is still worth checking these values? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Testing internals is not ideal, but OTOH these tests do not impose a big liability. It's unlikely that we will refactor the internals and if so, the tests can be easily adapted. Alternatives would be an image comparison (not favored) or checking the bounding box of an EllipseCollection with a single entry. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If this is find to keep to check as it is I would prefer that option because it is straighforward and more simple than the bounding box alternative. |
||
|
||
assert_array_almost_equal(ec.get_widths(), widths) | ||
assert_array_almost_equal(ec.get_heights(), heights) | ||
assert_array_almost_equal(ec.get_angles(), angles) | ||
|
||
ax.add_collection(ec) | ||
ax.set_xlim(-2, 12) | ||
ax.set_ylim(-2, 12) | ||
|
||
new_widths = rng.random((10, 2)) * 2 | ||
new_heights = rng.random((10, 2)) * 3 | ||
new_angles = rng.random((10, 2)) * 180 | ||
|
||
ec.set(widths=new_widths, heights=new_heights, angles=new_angles) | ||
|
||
ericpre marked this conversation as resolved.
Show resolved
Hide resolved
|
||
assert_array_almost_equal(ec.get_widths(), new_widths.ravel()) | ||
assert_array_almost_equal(ec.get_heights(), new_heights.ravel()) | ||
assert_array_almost_equal(ec.get_angles(), new_angles.ravel()) | ||
|
||
|
||
@image_comparison(['polycollection_close.png'], remove_text=True, style='mpl20') | ||
def test_polycollection_close(): | ||
from mpl_toolkits.mplot3d import Axes3D # type: ignore | ||
|
Uh oh!
There was an error while loading. Please reload this page.