From f1b0a28574a06f94ae2369f3c8c017b081bf5de9 Mon Sep 17 00:00:00 2001 From: Tim Hoffmann <2836374+timhoffm@users.noreply.github.com> Date: Mon, 30 Sep 2024 22:25:06 +0200 Subject: [PATCH] MNT: Fix double evaluation of _LazyTickList Closes #28908. The final instance.majorTicks list must be assigned after running `_get_tick()` because that may call `reset_ticks()`, which invalidates a previous set list. We still temporarily assign an empty tick list to instance.majorTicks, because `_get_tick()` may rely on that attibute to exist. --- doc/users/next_whats_new/axes_creation_speedup.rst | 4 ++++ lib/matplotlib/axis.py | 10 ++++++---- 2 files changed, 10 insertions(+), 4 deletions(-) create mode 100644 doc/users/next_whats_new/axes_creation_speedup.rst diff --git a/doc/users/next_whats_new/axes_creation_speedup.rst b/doc/users/next_whats_new/axes_creation_speedup.rst new file mode 100644 index 000000000000..c9eaa48c0060 --- /dev/null +++ b/doc/users/next_whats_new/axes_creation_speedup.rst @@ -0,0 +1,4 @@ +Axes creation speedup +~~~~~~~~~~~~~~~~~~~~~ + +Creating an Axes is now 20-25% faster due to internal optimizations. diff --git a/lib/matplotlib/axis.py b/lib/matplotlib/axis.py index cbf7928ec44e..d7ba29e1d595 100644 --- a/lib/matplotlib/axis.py +++ b/lib/matplotlib/axis.py @@ -538,17 +538,19 @@ def __get__(self, instance, owner): # instance._get_tick() can itself try to access the majorTicks # attribute (e.g. in certain projection classes which override # e.g. get_xaxis_text1_transform). In order to avoid infinite - # recursion, first set the majorTicks on the instance to an empty - # list, then create the tick and append it. + # recursion, first set the majorTicks on the instance temporarily + # to an empty lis. Then create the tick; note that _get_tick() + # may call reset_ticks(). Therefore, the final tick list is + # created and assigned afterwards. if self._major: instance.majorTicks = [] tick = instance._get_tick(major=True) - instance.majorTicks.append(tick) + instance.majorTicks = [tick] return instance.majorTicks else: instance.minorTicks = [] tick = instance._get_tick(major=False) - instance.minorTicks.append(tick) + instance.minorTicks = [tick] return instance.minorTicks