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

Skip to content

Commit bcdba1c

Browse files
authored
Merge pull request #27347 from greglucas/collection-norm-autoscale
FIX: scale norm of collections when first array is set
2 parents adb9fd4 + d8595f2 commit bcdba1c

File tree

3 files changed

+19
-0
lines changed

3 files changed

+19
-0
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
ScalarMappables auto-scale their norm when an array is set
2+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3+
4+
Collections previously deferred auto-scaling of the norm until draw time.
5+
This has been changed to scale the norm whenever the first array is set
6+
to align with the docstring and reduce unexpected behavior when
7+
accessing the norm before drawing.

lib/matplotlib/cm.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -536,6 +536,8 @@ def set_array(self, A):
536536
"converted to float")
537537

538538
self._A = A
539+
if not self.norm.scaled():
540+
self.norm.autoscale_None(A)
539541

540542
def get_array(self):
541543
"""

lib/matplotlib/tests/test_collections.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,16 @@ def check_segments(coll, positions, linelength, lineoffset, orientation):
292292
assert segment[1, pos2] == positions[i]
293293

294294

295+
def test_collection_norm_autoscale():
296+
# norm should be autoscaled when array is set, not deferred to draw time
297+
lines = np.arange(24).reshape((4, 3, 2))
298+
coll = mcollections.LineCollection(lines, array=np.arange(4))
299+
assert coll.norm(2) == 2 / 3
300+
# setting a new array shouldn't update the already scaled limits
301+
coll.set_array(np.arange(4) + 5)
302+
assert coll.norm(2) == 2 / 3
303+
304+
295305
def test_null_collection_datalim():
296306
col = mcollections.PathCollection([])
297307
col_data_lim = col.get_datalim(mtransforms.IdentityTransform())

0 commit comments

Comments
 (0)