From 5ffd67e6a6d1cb87257e306e269a0aa2d7d81a2a Mon Sep 17 00:00:00 2001 From: Scott Shambaugh Date: Sun, 18 Jan 2026 22:42:48 -0700 Subject: [PATCH 1/3] Speed up ticks processing when not visible or using a NullLocator --- lib/matplotlib/axis.py | 55 +++++++++++++++++++++++++++++++----------- 1 file changed, 41 insertions(+), 14 deletions(-) diff --git a/lib/matplotlib/axis.py b/lib/matplotlib/axis.py index d814924463a9..f3e6ea34b371 100644 --- a/lib/matplotlib/axis.py +++ b/lib/matplotlib/axis.py @@ -1295,20 +1295,47 @@ def _update_ticks(self): Update ticks (position and labels) using the current data interval of the axes. Return the list of ticks that will be drawn. """ - major_locs = self.get_majorticklocs() - major_labels = self.major.formatter.format_ticks(major_locs) - major_ticks = self.get_major_ticks(len(major_locs)) - for tick, loc, label in zip(major_ticks, major_locs, major_labels): - tick.update_position(loc) - tick.label1.set_text(label) - tick.label2.set_text(label) - minor_locs = self.get_minorticklocs() - minor_labels = self.minor.formatter.format_ticks(minor_locs) - minor_ticks = self.get_minor_ticks(len(minor_locs)) - for tick, loc, label in zip(minor_ticks, minor_locs, minor_labels): - tick.update_position(loc) - tick.label1.set_text(label) - tick.label2.set_text(label) + # Check if major ticks should be computed. + # Skip if using NullLocator or if all visible components are off. + major_kw = self._major_tick_kw + major_visible = (major_kw.get('tick1On') is not False or + major_kw.get('tick2On') is not False or + major_kw.get('label1On') is not False or + major_kw.get('label2On') is not False or + major_kw.get('gridOn') is not False) + + if (major_visible + and not isinstance(self.get_major_locator(), NullLocator)): + major_locs = self.get_majorticklocs() + major_labels = self.major.formatter.format_ticks(major_locs) + major_ticks = self.get_major_ticks(len(major_locs)) + for tick, loc, label in zip(major_ticks, major_locs, major_labels): + tick.update_position(loc) + tick.label1.set_text(label) + tick.label2.set_text(label) + else: + major_ticks = [] + + # Check if minor ticks should be computed. + minor_kw = self._minor_tick_kw + minor_visible = (minor_kw.get('tick1On') is not False or + minor_kw.get('tick2On') is not False or + minor_kw.get('label1On') is not False or + minor_kw.get('label2On') is not False or + minor_kw.get('gridOn') is not False) + + if (minor_visible + and not isinstance(self.get_minor_locator(), NullLocator)): + minor_locs = self.get_minorticklocs() + minor_labels = self.minor.formatter.format_ticks(minor_locs) + minor_ticks = self.get_minor_ticks(len(minor_locs)) + for tick, loc, label in zip(minor_ticks, minor_locs, minor_labels): + tick.update_position(loc) + tick.label1.set_text(label) + tick.label2.set_text(label) + else: + minor_ticks = [] + ticks = [*major_ticks, *minor_ticks] view_low, view_high = self.get_view_interval() From decfbd190e0fccd7301f362d752c9fc1525d5ff4 Mon Sep 17 00:00:00 2001 From: Scott Shambaugh Date: Mon, 19 Jan 2026 09:07:45 -0700 Subject: [PATCH 2/3] Break out tick visibility check into static method --- lib/matplotlib/axis.py | 30 ++++++++++++++---------------- 1 file changed, 14 insertions(+), 16 deletions(-) diff --git a/lib/matplotlib/axis.py b/lib/matplotlib/axis.py index f3e6ea34b371..1cf25e561b8d 100644 --- a/lib/matplotlib/axis.py +++ b/lib/matplotlib/axis.py @@ -1290,6 +1290,18 @@ def _set_artist_props(self, a): return a.set_figure(self.get_figure(root=False)) + @staticmethod + def _tick_group_visible(kw): + """ + Check if any of the tick group components are visible. + Takes in self._major_tick_kw or self._minor_tick_kw. + """ + return (kw.get('tick1On') is not False or + kw.get('tick2On') is not False or + kw.get('label1On') is not False or + kw.get('label2On') is not False or + kw.get('gridOn') is not False) + def _update_ticks(self): """ Update ticks (position and labels) using the current data interval of @@ -1297,14 +1309,7 @@ def _update_ticks(self): """ # Check if major ticks should be computed. # Skip if using NullLocator or if all visible components are off. - major_kw = self._major_tick_kw - major_visible = (major_kw.get('tick1On') is not False or - major_kw.get('tick2On') is not False or - major_kw.get('label1On') is not False or - major_kw.get('label2On') is not False or - major_kw.get('gridOn') is not False) - - if (major_visible + if (self._tick_group_visible(self._major_tick_kw) and not isinstance(self.get_major_locator(), NullLocator)): major_locs = self.get_majorticklocs() major_labels = self.major.formatter.format_ticks(major_locs) @@ -1317,14 +1322,7 @@ def _update_ticks(self): major_ticks = [] # Check if minor ticks should be computed. - minor_kw = self._minor_tick_kw - minor_visible = (minor_kw.get('tick1On') is not False or - minor_kw.get('tick2On') is not False or - minor_kw.get('label1On') is not False or - minor_kw.get('label2On') is not False or - minor_kw.get('gridOn') is not False) - - if (minor_visible + if (self._tick_group_visible(self._minor_tick_kw) and not isinstance(self.get_minor_locator(), NullLocator)): minor_locs = self.get_minorticklocs() minor_labels = self.minor.formatter.format_ticks(minor_locs) From 85a77b67c2512892acda2f84499d9a0d321e8902 Mon Sep 17 00:00:00 2001 From: Scott Shambaugh Date: Mon, 19 Jan 2026 09:10:31 -0700 Subject: [PATCH 3/3] Early ticks return path --- lib/matplotlib/axis.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/matplotlib/axis.py b/lib/matplotlib/axis.py index 1cf25e561b8d..bcfccb17d74a 100644 --- a/lib/matplotlib/axis.py +++ b/lib/matplotlib/axis.py @@ -1335,6 +1335,8 @@ def _update_ticks(self): minor_ticks = [] ticks = [*major_ticks, *minor_ticks] + if not ticks: + return [] view_low, view_high = self.get_view_interval() if view_low > view_high: