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