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

Skip to content

Commit 25e7f73

Browse files
committed
Add test
1 parent 760e756 commit 25e7f73

1 file changed

Lines changed: 23 additions & 0 deletions

File tree

lib/matplotlib/tests/test_collections.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -660,3 +660,26 @@ def test_quadmesh_set_array():
660660
coll.set_array(np.ones(9))
661661
fig.canvas.draw()
662662
assert np.array_equal(coll.get_array(), np.ones(9))
663+
664+
def test_legend_size_with_inverse_relationship():
665+
'''
666+
Ensure legend markers scale appropriately when label and size are inversely related.
667+
Here label = 5 / size
668+
'''
669+
670+
np.random.seed(19680801)
671+
X = np.random.random(50)
672+
Y = np.random.random(50)
673+
C = 1 - np.random.random(50)
674+
S = 5 / C
675+
676+
leg_sizes = [0.2, 0.4, 0.6, 0.8]
677+
fig, ax = plt.subplots()
678+
sc = ax.scatter(X, Y, s=S)
679+
handles, labels = sc.legend_elements(prop='sizes', num=leg_sizes, func=lambda x: 5 / x)
680+
681+
# Convert markersize scale to 's' scale
682+
handle_sizes = [x.get_markersize() for x in handles]
683+
handle_sizes = [5 / x**2 for x in handle_sizes]
684+
685+
assert_almost_equal(handle_sizes, leg_sizes, decimal=2)

0 commit comments

Comments
 (0)