diff --git a/lib/matplotlib/axes/_secondary_axes.py b/lib/matplotlib/axes/_secondary_axes.py index 4740fe6b23fb..b9ae2d57d6a3 100644 --- a/lib/matplotlib/axes/_secondary_axes.py +++ b/lib/matplotlib/axes/_secondary_axes.py @@ -82,6 +82,7 @@ def __init__(self, parent, orientation, self._axis = self.yaxis self._locstrings = ['right', 'left'] self._otherstrings = ['top', 'bottom'] + self._parentscale = self._axis.get_scale() # this gets positioned w/o constrained_layout so exclude: self._layoutbox = None self._poslayoutbox = None @@ -272,6 +273,11 @@ def _set_scale(self): if self._orientation == 'y': pscale = self._parent.yaxis.get_scale() set_scale = self.set_yscale + if pscale == self._parentscale: + return + else: + self._parentscale = pscale + if pscale == 'log': defscale = 'functionlog' else: diff --git a/lib/matplotlib/tests/test_axes.py b/lib/matplotlib/tests/test_axes.py index 9e1a8c8123d8..9c3570c2bc16 100644 --- a/lib/matplotlib/tests/test_axes.py +++ b/lib/matplotlib/tests/test_axes.py @@ -24,6 +24,7 @@ import matplotlib.markers as mmarkers import matplotlib.patches as mpatches import matplotlib.colors as mcolors +import matplotlib.ticker as mticker import matplotlib.transforms as mtransforms from numpy.testing import ( assert_allclose, assert_array_equal, assert_array_almost_equal) @@ -6079,6 +6080,29 @@ def invert(x): assert_allclose(ax.get_position().extents, [0.125, 0.1, 0.9, 0.9]) +def test_secondary_minorloc(): + fig, ax = plt.subplots(figsize=(10, 5)) + ax.plot(np.arange(2, 11), np.arange(2, 11)) + def invert(x): + with np.errstate(divide='ignore'): + return 1 / x + + secax = ax.secondary_xaxis('top', functions=(invert, invert)) + assert isinstance(secax._axis.get_minor_locator(), + mticker.NullLocator) + secax.minorticks_on() + assert isinstance(secax._axis.get_minor_locator(), + mticker.AutoMinorLocator) + ax.set_xscale('log') + plt.draw() + assert isinstance(secax._axis.get_minor_locator(), + mticker.LogLocator) + ax.set_xscale('linear') + plt.draw() + assert isinstance(secax._axis.get_minor_locator(), + mticker.NullLocator) + + def color_boxes(fig, axs): """ Helper for the tests below that test the extents of various axes elements