From a242de0822ab25cc77989a2f6e1b3e9a4c8f9919 Mon Sep 17 00:00:00 2001 From: Antony Lee Date: Sun, 7 Aug 2022 12:33:43 +0200 Subject: [PATCH 1/2] Call apply_aspect just once per axes in _update_title_position. ... not once per title. --- lib/matplotlib/axes/_base.py | 24 +++++++++++------------- 1 file changed, 11 insertions(+), 13 deletions(-) diff --git a/lib/matplotlib/axes/_base.py b/lib/matplotlib/axes/_base.py index 90942d7f782f..6fa5904e0ed1 100644 --- a/lib/matplotlib/axes/_base.py +++ b/lib/matplotlib/axes/_base.py @@ -2943,23 +2943,21 @@ def _update_title_position(self, renderer): titles = (self.title, self._left_title, self._right_title) + # Need to check all our twins too, and all the children as well. + axs = self._twinned_axes.get_siblings(self) + self.child_axes + for ax in self.child_axes: # Child positions must be updated first. + locator = ax.get_axes_locator() + if locator: + pos = locator(self, renderer) + ax.apply_aspect(pos) + else: + ax.apply_aspect() + for title in titles: x, _ = title.get_position() # need to start again in case of window resizing title.set_position((x, 1.0)) - # need to check all our twins too... - axs = self._twinned_axes.get_siblings(self) - # and all the children - for ax in self.child_axes: - if ax is not None: - locator = ax.get_axes_locator() - if locator: - pos = locator(self, renderer) - ax.apply_aspect(pos) - else: - ax.apply_aspect() - axs = axs + [ax] - top = -np.Inf + top = -np.inf for ax in axs: bb = None if (ax.xaxis.get_ticks_position() in ['top', 'unknown'] From 5c1f9b011cc7cc4ce1507b455682c61234b46063 Mon Sep 17 00:00:00 2001 From: Antony Lee Date: Sun, 7 Aug 2022 12:38:53 +0200 Subject: [PATCH 2/2] Shorten calls to apply_aspect using ternaries. ... and relying on the fact that `apply_aspect()` and `apply_aspect(None)` are equivalent. --- lib/matplotlib/axes/_base.py | 19 ++++--------------- lib/matplotlib/figure.py | 13 +++---------- lib/mpl_toolkits/mplot3d/axes3d.py | 6 +----- 3 files changed, 8 insertions(+), 30 deletions(-) diff --git a/lib/matplotlib/axes/_base.py b/lib/matplotlib/axes/_base.py index 6fa5904e0ed1..04e34080c186 100644 --- a/lib/matplotlib/axes/_base.py +++ b/lib/matplotlib/axes/_base.py @@ -2947,11 +2947,7 @@ def _update_title_position(self, renderer): axs = self._twinned_axes.get_siblings(self) + self.child_axes for ax in self.child_axes: # Child positions must be updated first. locator = ax.get_axes_locator() - if locator: - pos = locator(self, renderer) - ax.apply_aspect(pos) - else: - ax.apply_aspect() + ax.apply_aspect(locator(self, renderer) if locator else None) for title in titles: x, _ = title.get_position() @@ -3014,11 +3010,7 @@ def draw(self, renderer): # loop over self and child Axes... locator = self.get_axes_locator() - if locator: - pos = locator(self, renderer) - self.apply_aspect(pos) - else: - self.apply_aspect() + self.apply_aspect(locator(self, renderer) if locator else None) artists = self.get_children() artists.remove(self.patch) @@ -4394,11 +4386,8 @@ def get_tightbbox(self, renderer=None, call_axes_locator=True, return None locator = self.get_axes_locator() - if locator and call_axes_locator: - pos = locator(self, renderer) - self.apply_aspect(pos) - else: - self.apply_aspect() + self.apply_aspect( + locator(self, renderer) if locator and call_axes_locator else None) for axis in self._axis_map.values(): if self.axison and axis.get_visible(): diff --git a/lib/matplotlib/figure.py b/lib/matplotlib/figure.py index d46c1b81cd4b..b2b02a2a2c96 100644 --- a/lib/matplotlib/figure.py +++ b/lib/matplotlib/figure.py @@ -206,20 +206,13 @@ def _get_draw_artists(self, renderer): key=lambda artist: artist.get_zorder()) for ax in self._localaxes: locator = ax.get_axes_locator() - if locator: - pos = locator(ax, renderer) - ax.apply_aspect(pos) - else: - ax.apply_aspect() + ax.apply_aspect(locator(ax, renderer) if locator else None) for child in ax.get_children(): if hasattr(child, 'apply_aspect'): locator = child.get_axes_locator() - if locator: - pos = locator(child, renderer) - child.apply_aspect(pos) - else: - child.apply_aspect() + child.apply_aspect( + locator(child, renderer) if locator else None) return artists def autofmt_xdate( diff --git a/lib/mpl_toolkits/mplot3d/axes3d.py b/lib/mpl_toolkits/mplot3d/axes3d.py index 05df0c16f220..e47029539352 100644 --- a/lib/mpl_toolkits/mplot3d/axes3d.py +++ b/lib/mpl_toolkits/mplot3d/axes3d.py @@ -420,11 +420,7 @@ def draw(self, renderer): # it adjusts the view limits and the size of the bounding box # of the Axes locator = self.get_axes_locator() - if locator: - pos = locator(self, renderer) - self.apply_aspect(pos) - else: - self.apply_aspect() + self.apply_aspect(locator(self, renderer) if locator else None) # add the projection matrix to the renderer self.M = self.get_proj()