Skip to content

Commit

Permalink
MNT: Remove scalarmappable private update attributes
Browse files Browse the repository at this point in the history
The updates to scalar mappable would only take effect if the
array was updated. This meant that updating the vmin/vmax of
the sm's norm would not be propagated to the drawn artists. This
update removes the update checks from scalar mappable to calculate
the colors regardless of updates to the array.
  • Loading branch information
greglucas committed Mar 20, 2021
1 parent 91cb8a8 commit ed6d92b
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 32 deletions.
5 changes: 5 additions & 0 deletions doc/api/next_api_changes/removals/19552-GL.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
ScalarMappable update checkers
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
``ScalarMappable.update_dict``, ``ScalarMappable.add_checker()``, and
``ScalarMappable.check_update()`` have been removed. A callback can
be registered in ``ScalarMappable.callbacksSM`` to be notified of updates.
22 changes: 0 additions & 22 deletions lib/matplotlib/cm.py
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,6 @@ def __init__(self, norm=None, cmap=None):
#: The last colorbar associated with this ScalarMappable. May be None.
self.colorbar = None
self.callbacksSM = cbook.CallbackRegistry()
self._update_dict = {'array': False}

def _scale_norm(self, norm, vmin, vmax):
"""
Expand Down Expand Up @@ -369,7 +368,6 @@ def set_array(self, A):
A : ndarray or None
"""
self._A = A
self._update_dict['array'] = True

def get_array(self):
"""Return the data array."""
Expand Down Expand Up @@ -476,30 +474,10 @@ def autoscale_None(self):
self.norm.autoscale_None(self._A)
self.changed()

def _add_checker(self, checker):
"""
Add an entry to a dictionary of boolean flags
that are set to True when the mappable is changed.
"""
self._update_dict[checker] = False

def _check_update(self, checker):
"""Return whether mappable has changed since the last check."""
if self._update_dict[checker]:
self._update_dict[checker] = False
return True
return False

def changed(self):
"""
Call this whenever the mappable is changed to notify all the
callbackSM listeners to the 'changed' signal.
"""
self.callbacksSM.process('changed', self)
for key in self._update_dict:
self._update_dict[key] = True
self.stale = True

update_dict = _api.deprecate_privatize_attribute("3.3")
add_checker = _api.deprecate_privatize_attribute("3.3")
check_update = _api.deprecate_privatize_attribute("3.3")
4 changes: 1 addition & 3 deletions lib/matplotlib/collections.py
Original file line number Diff line number Diff line change
Expand Up @@ -851,7 +851,6 @@ def set_alpha(self, alpha):
supported.
"""
artist.Artist._set_alpha_for_array(self, alpha)
self._update_dict['array'] = True
self._set_facecolor(self._original_facecolor)
self._set_edgecolor(self._original_edgecolor)

Expand Down Expand Up @@ -907,7 +906,7 @@ def update_scalarmappable(self):
if not self._set_mappable_flags():
return
# Allow possibility to call 'self.set_array(None)'.
if self._check_update("array") and self._A is not None:
if self._A is not None:
# QuadMesh can map 2d arrays (but pcolormesh supplies 1d array)
if self._A.ndim > 1 and not isinstance(self, QuadMesh):
raise ValueError('Collections can only map rank 1 arrays')
Expand Down Expand Up @@ -958,7 +957,6 @@ def update_from(self, other):
self._A = other._A
self.norm = other.norm
self.cmap = other.cmap
# do we need to copy self._update_dict? -JJL
self.stale = True


Expand Down
16 changes: 16 additions & 0 deletions lib/matplotlib/tests/test_collections.py
Original file line number Diff line number Diff line change
Expand Up @@ -715,6 +715,22 @@ def test_quadmesh_set_array():
assert np.array_equal(coll.get_array(), np.ones(9))


def test_quadmesh_vmin_vmax():
# test when vmin/vmax on the norm changes, the quadmesh gets updated
fig, ax = plt.subplots()
cmap = mpl.cm.get_cmap('plasma')
norm = mpl.colors.Normalize(vmin=0, vmax=1)
coll = ax.pcolormesh([[1]], cmap=cmap, norm=norm)
fig.canvas.draw()
assert np.array_equal(coll.get_facecolors()[0, :], cmap(norm(1)))

# Change the vmin/vmax of the norm so that the color is from
# the bottom of the colormap now
norm.vmin, norm.vmax = 1, 2
fig.canvas.draw()
assert np.array_equal(coll.get_facecolors()[0, :], cmap(norm(1)))


def test_quadmesh_alpha_array():
x = np.arange(4)
y = np.arange(4)
Expand Down
12 changes: 5 additions & 7 deletions lib/mpl_toolkits/mplot3d/art3d.py
Original file line number Diff line number Diff line change
Expand Up @@ -695,13 +695,11 @@ def _update_scalarmappable(sm):
"""
if sm._A is None:
return
copy_state = sm._update_dict['array']
ret = sm.update_scalarmappable()
if copy_state:
if sm._face_is_mapped:
sm._facecolor3d = sm._facecolors
elif sm._edge_is_mapped: # Should this be plain "if"?
sm._edgecolor3d = sm._edgecolors
sm.update_scalarmappable()
if sm._face_is_mapped:
sm._facecolor3d = sm._facecolors
elif sm._edge_is_mapped: # Should this be plain "if"?
sm._edgecolor3d = sm._edgecolors


def patch_collection_2d_to_3d(col, zs=0, zdir='z', depthshade=True):
Expand Down

0 comments on commit ed6d92b

Please sign in to comment.