diff --git a/lib/matplotlib/tests/test_text.py b/lib/matplotlib/tests/test_text.py index cfa2566173aa..3546532b9725 100644 --- a/lib/matplotlib/tests/test_text.py +++ b/lib/matplotlib/tests/test_text.py @@ -5,7 +5,7 @@ import numpy as np from numpy.testing import assert_almost_equal import pytest - +import time import matplotlib as mpl from matplotlib.backend_bases import MouseEvent from matplotlib.font_manager import FontProperties @@ -756,3 +756,18 @@ def test_pdf_chars_beyond_bmp(): plt.rcParams['mathtext.fontset'] = 'stixsans' plt.figure() plt.figtext(0.1, 0.5, "Mass $m$ \U00010308", size=30) + + +def test_cache_large_labels(): + """Test to verify cache helps when ticks are too large""" + times = [] + fig, _ = plt.subplots() + for pow in range(1, 5): + labels = [i for i in range(10**pow)] + t0 = time.perf_counter() + plt.xticks(labels) + plt.yticks(labels) + fig.draw_without_rendering() + times.append(time.perf_counter()-t0) + assert times[-1] > times[0] + assert times[-1] > times[-2] diff --git a/lib/matplotlib/text.py b/lib/matplotlib/text.py index c4b3a9184a3a..f3acd20f253c 100644 --- a/lib/matplotlib/text.py +++ b/lib/matplotlib/text.py @@ -107,7 +107,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)) @@ -158,6 +157,7 @@ def __init__(self, self._linespacing = linespacing self.set_rotation_mode(rotation_mode) self.update(kwargs) + self._cached = dict() def update(self, kwargs): # docstring inherited @@ -177,6 +177,7 @@ def __getstate__(self): d = super().__getstate__() # remove the cached _renderer (if it exists) d['_renderer'] = None + d['_cached'] = {} return d def contains(self, mouseevent):