diff --git a/doc/api/next_api_changes/deprecations/22299-GL.rst b/doc/api/next_api_changes/deprecations/22299-GL.rst new file mode 100644 index 000000000000..b755c7804731 --- /dev/null +++ b/doc/api/next_api_changes/deprecations/22299-GL.rst @@ -0,0 +1,4 @@ +``matplotlib.cbook.maxdict`` is deprecated +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +This class has been deprecated in favor of the standard library +``functools.lru_cache``. diff --git a/lib/matplotlib/cbook/__init__.py b/lib/matplotlib/cbook/__init__.py index efaabd1e09d4..2005f04c1ab6 100644 --- a/lib/matplotlib/cbook/__init__.py +++ b/lib/matplotlib/cbook/__init__.py @@ -540,6 +540,7 @@ def flatten(seq, scalarp=is_scalar_or_string): yield from flatten(item, scalarp) +@_api.deprecated("3.6", alternative="functools.lru_cache") class maxdict(dict): """ A dictionary with a maximum size. diff --git a/lib/matplotlib/text.py b/lib/matplotlib/text.py index 4dcf40648d35..4ff0fc2377a1 100644 --- a/lib/matplotlib/text.py +++ b/lib/matplotlib/text.py @@ -2,6 +2,7 @@ Classes for including text in a figure. """ +import functools import logging import math import numbers @@ -108,7 +109,6 @@ class Text(Artist): """Handle storing and drawing of text in window or data coordinates.""" zorder = 3 - _cached = cbook.maxdict(50) def __repr__(self): return "Text(%s, %s, %s)" % (self._x, self._y, repr(self._text)) @@ -294,9 +294,13 @@ def _get_layout(self, renderer): of a rotated text when necessary. """ key = self._get_layout_cache_key(renderer=renderer) - if key in self._cached: - return self._cached[key] + return self._get_layout_cache(key, renderer) + @functools.lru_cache(maxsize=50) + def _get_layout_cache(self, key, renderer): + """ + Return the layout using a lru_cache decorator + """ thisx, thisy = 0.0, 0.0 lines = self.get_text().split("\n") # Ensures lines is not empty. @@ -440,7 +444,6 @@ def _get_layout(self, renderer): xys = M.transform(offset_layout) - (offsetx, offsety) ret = bbox, list(zip(lines, zip(ws, hs), *xys.T)), descent - self._cached[key] = ret return ret def set_bbox(self, rectprops):