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

Skip to content

Commit 0e1f13b

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 b53418e commit 0e1f13b

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
@@ -82,7 +82,7 @@ def __init__(self, parent, orientation,
8282
self._axis = self.yaxis
8383
self._locstrings = ['right', 'left']
8484
self._otherstrings = ['top', 'bottom']
85-
self._parentscale = self._axis.get_scale()
85+
self._parentscale = None
8686
# this gets positioned w/o constrained_layout so exclude:
8787
self._layoutbox = None
8888
self._poslayoutbox = None
@@ -215,23 +215,7 @@ def set_functions(self, functions):
215215
216216
If a transform is supplied, then the transform must have an
217217
inverse.
218-
219218
"""
220-
221-
if self._orientation == 'x':
222-
set_scale = self.set_xscale
223-
parent_scale = self._parent.get_xscale()
224-
else:
225-
set_scale = self.set_yscale
226-
parent_scale = self._parent.get_yscale()
227-
# we need to use a modified scale so the scale can receive the
228-
# transform. Only types supported are linear and log10 for now.
229-
# Probably possible to add other transforms as a todo...
230-
if parent_scale == 'log':
231-
defscale = 'functionlog'
232-
else:
233-
defscale = 'function'
234-
235219
if (isinstance(functions, tuple) and len(functions) == 2 and
236220
callable(functions[0]) and callable(functions[1])):
237221
# make an arbitrary convert from a two-tuple of functions
@@ -244,7 +228,7 @@ def set_functions(self, functions):
244228
'must be a two-tuple of callable functions '
245229
'with the first function being the transform '
246230
'and the second being the inverse')
247-
set_scale(defscale, functions=self._functions)
231+
self._set_scale()
248232

249233
def draw(self, renderer=None, inframe=False):
250234
"""
@@ -275,8 +259,6 @@ def _set_scale(self):
275259
set_scale = self.set_yscale
276260
if pscale == self._parentscale:
277261
return
278-
else:
279-
self._parentscale = pscale
280262

281263
if pscale == 'log':
282264
defscale = 'functionlog'
@@ -293,6 +275,9 @@ def _set_scale(self):
293275
if self._ticks_set:
294276
self._axis.set_major_locator(FixedLocator(ticks))
295277

278+
# If the parent scale doesn't change, we can skip this next time.
279+
self._parentscale = pscale
280+
296281
def _set_lims(self):
297282
"""
298283
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
@@ -6135,6 +6135,16 @@ def invert(x):
61356135
mticker.NullLocator)
61366136

61376137

6138+
def test_secondary_formatter():
6139+
fig, ax = plt.subplots()
6140+
ax.set_xscale("log")
6141+
secax = ax.secondary_xaxis("top")
6142+
secax.xaxis.set_major_formatter(mticker.ScalarFormatter())
6143+
fig.canvas.draw()
6144+
assert isinstance(
6145+
secax.xaxis.get_major_formatter(), mticker.ScalarFormatter)
6146+
6147+
61386148
def color_boxes(fig, axs):
61396149
"""
61406150
Helper for the tests below that test the extents of various axes elements

0 commit comments

Comments
 (0)
0