From 0de8ef5ca296a0254c41030ebf7300f88dfbe06e Mon Sep 17 00:00:00 2001 From: Antony Lee Date: Wed, 13 May 2020 13:08:31 +0200 Subject: [PATCH] TexManager fixes. - Fix calling get_grey with fontsize=None or dpi=None (this would previously just pass the string "None" down the stack and crash). - Fix get_rgba stacking colors and get rid of its cache (the slow part is accessing the filesystem in get_grey, just stacking some colors should be cheap and not caching the result is also nicer memory-wise). --- doc/api/api_changes_3.3/deprecations.rst | 7 ++--- lib/matplotlib/texmanager.py | 33 ++++++++++++------------ 2 files changed, 20 insertions(+), 20 deletions(-) diff --git a/doc/api/api_changes_3.3/deprecations.rst b/doc/api/api_changes_3.3/deprecations.rst index 5635c5f73c86..71cd0cac61a4 100644 --- a/doc/api/api_changes_3.3/deprecations.rst +++ b/doc/api/api_changes_3.3/deprecations.rst @@ -137,9 +137,10 @@ This method is deprecated. Use ``SymmetricalScale.InvertedSymmetricalTransform`` are deprecated. Directly access the transform classes from the :mod:`.scale` module. -``TexManager.cachedir`` -~~~~~~~~~~~~~~~~~~~~~~~ -Use `matplotlib.get_cachedir()` instead. +``TexManager.cachedir``, ``TexManager.rgba_arrayd`` +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +Use `matplotlib.get_cachedir()` instead for the former; there is no replacement +for the latter. Setting `.Line2D`\'s pickradius via `.Line2D.set_picker` ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ diff --git a/lib/matplotlib/texmanager.py b/lib/matplotlib/texmanager.py index d51e73bcd16a..97230a2f64b6 100644 --- a/lib/matplotlib/texmanager.py +++ b/lib/matplotlib/texmanager.py @@ -53,7 +53,6 @@ class TexManager: # Caches. texcache = os.path.join(mpl.get_cachedir(), 'tex.cache') - rgba_arrayd = {} grey_arrayd = {} font_family = 'serif' @@ -85,6 +84,11 @@ class TexManager: def cachedir(self): return mpl.get_cachedir() + @cbook.deprecated("3.3") + @property + def rgba_arrayd(self): + return {} + @functools.lru_cache() # Always return the same instance. def __new__(cls): Path(cls.texcache).mkdir(parents=True, exist_ok=True) @@ -369,30 +373,25 @@ def make_png(self, tex, fontsize, dpi): def get_grey(self, tex, fontsize=None, dpi=None): """Return the alpha channel.""" + if not fontsize: + fontsize = rcParams['font.size'] + if not dpi: + dpi = rcParams['savefig.dpi'] key = tex, self.get_font_config(), fontsize, dpi alpha = self.grey_arrayd.get(key) if alpha is None: pngfile = self.make_png(tex, fontsize, dpi) - X = mpl.image.imread(os.path.join(self.texcache, pngfile)) - self.grey_arrayd[key] = alpha = X[:, :, -1] + rgba = mpl.image.imread(os.path.join(self.texcache, pngfile)) + self.grey_arrayd[key] = alpha = rgba[:, :, -1] return alpha def get_rgba(self, tex, fontsize=None, dpi=None, rgb=(0, 0, 0)): """Return latex's rendering of the tex string as an rgba array.""" - if not fontsize: - fontsize = rcParams['font.size'] - if not dpi: - dpi = rcParams['savefig.dpi'] - r, g, b = rgb - key = tex, self.get_font_config(), fontsize, dpi, tuple(rgb) - Z = self.rgba_arrayd.get(key) - - if Z is None: - alpha = self.get_grey(tex, fontsize, dpi) - Z = np.dstack([r, g, b, alpha]) - self.rgba_arrayd[key] = Z - - return Z + alpha = self.get_grey(tex, fontsize, dpi) + rgba = np.empty((*alpha.shape, 4)) + rgba[..., :3] = mpl.colors.to_rgb(rgb) + rgba[..., -1] = alpha + return rgba def get_text_width_height_descent(self, tex, fontsize, renderer=None): """Return width, height and descent of the text."""