From c016538cea9f065ec42cadcda0f365df0a77fd26 Mon Sep 17 00:00:00 2001 From: Antony Lee Date: Fri, 17 Apr 2020 12:11:14 +0200 Subject: [PATCH] Deprecate ScalarMappable.check_update and associated machinery. It's a rather complex machinery that's ultimately just used by Collection to know whether the alpha array has been updated so that the value-to-rgba mapping needs to be updated. Users can't connect to it to know whether the mapped values have been updated, and they can't even use it to check whether the *alpha* values have been updated because this unsets the marker and thus would mess up Collection's own use of it. For now, just deprecate its public use to ultimately make it private and perhaps replace it later by something less brittle (e.g., normal a normal callback registry). --- doc/api/api_changes_3.3/deprecations.rst | 5 +++ lib/matplotlib/cm.py | 39 +++++++++++++++--------- lib/matplotlib/collections.py | 6 ++-- 3 files changed, 32 insertions(+), 18 deletions(-) diff --git a/doc/api/api_changes_3.3/deprecations.rst b/doc/api/api_changes_3.3/deprecations.rst index 54fb8fe63ee4..f250def2757a 100644 --- a/doc/api/api_changes_3.3/deprecations.rst +++ b/doc/api/api_changes_3.3/deprecations.rst @@ -450,3 +450,8 @@ needed. The associated helper methods ``NavigationToolbar2.draw()`` and ``ToolViewsPositions.refresh_locators()`` are deprecated, and should be replaced by calls to ``draw_idle()`` on the corresponding canvas. + +`.ScalarMappable` checkers +~~~~~~~~~~~~~~~~~~~~~~~~~~ +The ``add_checker`` and ``check_update`` methods and ``update_dict`` attribute +of `.ScalarMappable` are deprecated. diff --git a/lib/matplotlib/cm.py b/lib/matplotlib/cm.py index f3787c11bba0..b90e9b730150 100644 --- a/lib/matplotlib/cm.py +++ b/lib/matplotlib/cm.py @@ -177,7 +177,7 @@ 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} + self._update_dict = {'array': False} def _scale_norm(self, norm, vmin, vmax): """ @@ -279,7 +279,7 @@ def set_array(self, A): A : ndarray """ self._A = A - self.update_dict['array'] = True + self._update_dict['array'] = True def get_array(self): """Return the data array.""" @@ -386,30 +386,39 @@ def autoscale_None(self): self.norm.autoscale_None(self._A) self.changed() - def add_checker(self, checker): + 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 + self._update_dict[checker] = False - def check_update(self, checker): - """ - If mappable has changed since the last check, - return True; else return False - """ - if self.update_dict[checker]: - 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 + callbackSM listeners to the 'changed' signal. """ self.callbacksSM.process('changed', self) - - for key in self.update_dict: - self.update_dict[key] = True + for key in self._update_dict: + self._update_dict[key] = True self.stale = True + + @cbook.deprecated("3.3") + @property + def update_dict(self): + return self._update_dict + + @cbook.deprecated("3.3") + def add_checker(self, checker): + return self._add_checker(checker) + + @cbook.deprecated("3.3") + def check_update(self, checker): + return self.check_update(checker) diff --git a/lib/matplotlib/collections.py b/lib/matplotlib/collections.py index 1586f983c6bb..181e46b4584f 100644 --- a/lib/matplotlib/collections.py +++ b/lib/matplotlib/collections.py @@ -768,7 +768,7 @@ def set_edgecolor(self, c): def set_alpha(self, alpha): # docstring inherited super().set_alpha(alpha) - self.update_dict['array'] = True + self._update_dict['array'] = True self._set_facecolor(self._original_facecolor) self._set_edgecolor(self._original_edgecolor) @@ -784,7 +784,7 @@ def update_scalarmappable(self): return if self._A.ndim > 1: raise ValueError('Collections can only map rank 1 arrays') - if not self.check_update("array"): + if not self._check_update("array"): return if self._is_filled: self._facecolors = self.to_rgba(self._A, self._alpha) @@ -815,7 +815,7 @@ def update_from(self, other): self._A = other._A self.norm = other.norm self.cmap = other.cmap - # self.update_dict = other.update_dict # do we need to copy this? -JJL + # do we need to copy self._update_dict? -JJL self.stale = True