8000 Merge pull request #14592 from anntzer/secondaryformatter · matplotlib/matplotlib@ad877a0 · GitHub
[go: up one dir, main page]

Skip to content

Commit ad877a0

Browse files
authored
Merge pull request #14592 from anntzer/secondaryformatter
In SecondaryAxis.set_functions, reuse _set_scale's parent scale caching.
2 parents 9895288 + c7f6506 commit ad877a0

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
@@ -189,21 +189,6 @@ def set_functions(self, functions):
189189
If a transform is supplied, then the transform must have an
190190
inverse.
191191
"""
192-
193-
if self._orientation == 'x':
194-
set_scale = self.set_xscale
195-
parent_scale = self._parent.get_xscale()
196-
else:
197-
set_scale = self.set_yscale
198-
parent_scale = self._parent.get_yscale()
199-
# we need to use a modified scale so the scale can receive the
200-
# transform. Only types supported are linear and log10 for now.
201-
# Probably possible to add other transforms as a todo...
202-
if parent_scale == 'log':
203-
defscale = 'functionlog'
204-
else:
205-
defscale = 'function'
206-
207192
if (isinstance(functions, tuple) and len(functions) == 2 and
208193
callable(functions[0]) and callable(functions[1])):
209194
# make an arbitrary convert from a two-tuple of functions
@@ -216,8 +201,7 @@ def set_functions(self, functions):
216201
'must be a two-tuple of callable functions '
217202
'with the first function being the transform '
218203
'and the second being the inverse')
219-
# need to invert the roles here for the ticks to line up.
220-
set_scale(defscale, functions=self._functions[::-1])
204+
self._set_scale()
221205

222206
def draw(self, renderer=None, inframe=False):
223207
"""
@@ -246,8 +230,6 @@ def _set_scale(self):
246230
set_scale = self.set_yscale
247231
if pscale == self._parentscale:
248232
return
249-
else:
250-
self._parentscale = pscale
251233

252234
if pscale == 'log':
253235
defscale = 'functionlog'
@@ -265,6 +247,9 @@ def _set_scale(self):
265247
if self._ticks_set:
266248
self._axis.set_major_locator(mticker.FixedLocator(ticks))
267249

250+
# If the parent scale doesn't change, we can skip this next time.
251+
self._parentscale = pscale
252+
268253
def _set_lims(self):
269254
"""
270255
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
@@ -5921,6 +5921,16 @@ def invert(x):
59215921
mticker.NullLocator)
59225922

59235923

5924+
def test_secondary_formatter():
5925+
fig, ax = plt.subplots()
5926+
ax.set_xscale("log")
5927+
secax = ax.secondary_xaxis("top")
5928+
secax.xaxis.set_major_formatter(mticker.ScalarFormatter())
5929+
fig.canvas.draw()
5930+
assert isinstance(
5931+
secax.xaxis.get_major_formatter(), mticker.ScalarFormatter)
5932+
5933+
59245934
def color_boxes(fig, axs):
59255935
"""
59265936
Helper for the tests below that test the extents of various axes elements

0 commit comments

Comments
 (0)
0