From c7f65061eef352fead155a99ff06f0dae708b725 Mon Sep 17 00:00:00 2001 From: Antony Lee Date: Thu, 20 Jun 2019 20:25:44 +0200 Subject: [PATCH] In SecondaryAxis.set_functions, reuse _set_scale's parent scale caching. _set_scale checks whether the parent scale changed before modifying the secondary scale. set_functions should do the same. See added test for previously failing case. --- lib/matplotlib/axes/_secondary_axes.py | 25 +++++-------------------- lib/matplotlib/tests/test_axes.py | 10 ++++++++++ 2 files changed, 15 insertions(+), 20 deletions(-) diff --git a/lib/matplotlib/axes/_secondary_axes.py b/lib/matplotlib/axes/_secondary_axes.py index e21bfd62bdd1..2d2ae7d4338a 100644 --- a/lib/matplotlib/axes/_secondary_axes.py +++ b/lib/matplotlib/axes/_secondary_axes.py @@ -62,7 +62,7 @@ def __init__(self, parent, orientation, self._axis = self.yaxis self._locstrings = ['right', 'left'] self._otherstrings = ['top', 'bottom'] - self._parentscale = self._axis.get_scale() + self._parentscale = None # this gets positioned w/o constrained_layout so exclude: self._layoutbox = None self._poslayoutbox = None @@ -195,21 +195,6 @@ def set_functions(self, functions): If a transform is supplied, then the transform must have an inverse. """ - - if self._orientation == 'x': - set_scale = self.set_xscale - parent_scale = self._parent.get_xscale() - else: - set_scale = self.set_yscale - parent_scale = self._parent.get_yscale() - # we need to use a modified scale so the scale can receive the - # transform. Only types supported are linear and log10 for now. - # Probably possible to add other transforms as a todo... - if parent_scale == 'log': - defscale = 'functionlog' - else: - defscale = 'function' - if (isinstance(functions, tuple) and len(functions) == 2 and callable(functions[0]) and callable(functions[1])): # make an arbitrary convert from a two-tuple of functions @@ -222,8 +207,7 @@ def set_functions(self, functions): 'must be a two-tuple of callable functions ' 'with the first function being the transform ' 'and the second being the inverse') - # need to invert the roles here for the ticks to line up. - set_scale(defscale, functions=self._functions[::-1]) + self._set_scale() def draw(self, renderer=None, inframe=False): """ @@ -252,8 +236,6 @@ def _set_scale(self): set_scale = self.set_yscale if pscale == self._parentscale: return - else: - self._parentscale = pscale if pscale == 'log': defscale = 'functionlog' @@ -271,6 +253,9 @@ def _set_scale(self): if self._ticks_set: self._axis.set_major_locator(mticker.FixedLocator(ticks)) + # If the parent scale doesn't change, we can skip this next time. + self._parentscale = pscale + def _set_lims(self): """ Set the limits based on parent limits and the convert method diff --git a/lib/matplotlib/tests/test_axes.py b/lib/matplotlib/tests/test_axes.py index b650180884b1..38603d93ae07 100644 --- a/lib/matplotlib/tests/test_axes.py +++ b/lib/matplotlib/tests/test_axes.py @@ -6544,6 +6544,16 @@ def invert(x): mticker.NullLocator) +def test_secondary_formatter(): + fig, ax = plt.subplots() + ax.set_xscale("log") + secax = ax.secondary_xaxis("top") + secax.xaxis.set_major_formatter(mticker.ScalarFormatter()) + fig.canvas.draw() + assert isinstance( + secax.xaxis.get_major_formatter(), mticker.ScalarFormatter) + + def color_boxes(fig, axs): """ Helper for the tests below that test the extents of various axes elements pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy