8000 In SecondaryAxis.set_functions, reuse _set_scale's parent scale caching. · matplotlib/matplotlib@c7f6506 · GitHub
[go: up one dir, main page]

Skip to content

Commit c7f6506

Browse files
committed
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.
1 parent 9c4e686 commit c7f6506

File tree

2 files changed

+15
-20
lines changed

2 files changed

+15
-20
lines changed

lib/matplotlib/axes/_secondary_axes.py

Lines changed: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ def __init__(self, parent, orientation,
6262
self._axis = self.yaxis
6363
self._locstrings = ['right', 'left']
6464
self._otherstrings = ['top', 'bottom']
65-
self._parentscale = self._axis.get_scale()
65+
self._parentscale = None
6666
# this gets positioned w/o constrained_layout so exclude:
6767
self._layoutbox = None
6868
self._poslayoutbox = None
@@ -195,21 +195,6 @@ def set_functions(self, functions):
195195
If a transform is supplied, then the transform must have an
196196
inverse.
197197
"""
198-
199-
if self._orientation == 'x':
200-
set_scale = self.set_xscale
201-
parent_scale = self._parent.get_xscale()
202-
else:
203-
set_scale = self.set_yscale
204-
parent_scale = self._parent.get_yscale()
205-
# we need to use a modified scale so the scale can receive the
206-
# transform. Only types supported are linear and log10 for now.
207-
# Probably possible to add other transforms as a todo...
208-
if parent_scale == 'log':
209-
defscale = 'functionlog'
210-
else:
211-
defscale = 'function'
212-
213198
if (isinstance(functions, tuple) and len(functions) == 2 and
214199
callable(functions[0]) and callable(functions[1])):
215200
# make an arbitrary convert from a two-tuple of functions
@@ -222,8 +207,7 @@ def set_functions(self, functions):
222207
'must be a two-tuple of callable functions '
223208
'with the first function being the transform '
224209
'and the second being the inverse')
225-
# need to invert the roles here for the ticks to line up.
226-
set_scale(defscale, functions=self._functions[::-1])
210+
self._set_scale()
227211

228212
def draw(self, renderer=None, inframe=False):
229213
"""
@@ -252,8 +236,6 @@ def _set_scale(self):
252236
set_scale = self.set_yscale
253237
if pscale == self._parentscale:
254238
return
255-
else:
256-
self._parentscale = pscale
257239

258240
if pscale == 'log':
259241
defscale = 'functionlog'
@@ -271,6 +253,9 @@ def _set_scale(self):
271253
if self._ticks_set:
272254
self._axis.set_major_locator(mticker.FixedLocator(ticks))
273255

256+
# If the parent scale doesn't change, we can skip this next time.
257+
self._parentscale = pscale
258+
274259
def _set_lims(self):
275260
"""
276261
Set the limits based on parent limits and the convert method

lib/matplotlib/tests/test_axes.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6544,6 +6544,16 @@ def invert(x):
65446544
mticker.NullLocator)
65456545

65466546

6547+
def test_secondary_formatter():
6548+
fig, ax = plt.subplots()
6549+
ax.set_xscale("log")
6550+
secax = ax.secondary_xaxis("top")
6551+
secax.xaxis.set_major_formatter(mticker.ScalarFormatter())
6552+
fig.canvas.draw()
6553+
assert isinstance(
6554+
secax.xaxis.get_major_formatter(), mticker.ScalarFormatter)
6555+
6556+
65476557
def color_boxes(fig, axs):
65486558
"""
65496559
Helper for the tests below that test the extents of various axes elements

0 commit comments

Comments
 (0)
0