Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 9509b4e

Browse files
committed
FIX: move towards preoper non-linear axis
1 parent 170e2ed commit 9509b4e

File tree

1 file changed

+69
-3
lines changed

1 file changed

+69
-3
lines changed

lib/matplotlib/axes/_secondary_axes.py

Lines changed: 69 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,47 @@
66

77
import matplotlib.ticker as mticker
88
import matplotlib.transforms as mtransforms
9+
import matplotlib.scale as mscale
910

1011
from matplotlib.axes._base import _AxesBase
1112

13+
from matplotlib.ticker import (
14+
AutoLocator,
15+
FixedLocator,
16+
NullLocator,
17+
NullFormatter,
18+
FuncFormatter,
19+
ScalarFormatter,
20+
AutoMinorLocator,
21+
)
22+
23+
class ArbitraryScale(mscale.ScaleBase):
24+
25+
name = 'arbitrary'
26+
27+
def __init__(self, axis, transform=mtransforms.IdentityTransform()):
28+
"""
29+
TODO
30+
"""
31+
self._transform = transform
32+
33+
def get_transform(self):
34+
"""
35+
The transform for linear scaling is just the
36+
:class:`~matplotlib.transforms.IdentityTransform`.
37+
"""
38+
return self._transform
39+
40+
def set_default_locators_and_formatters(self, axis):
41+
"""
42+
Set the locators and formatters to reasonable defaults for
43+
linear scaling.
44+
"""
45+
axis.set_major_locator(AutoLocator())
46+
axis.set_major_formatter(ScalarFormatter())
47+
axis.set_minor_formatter(NullFormatter())
48+
49+
mscale.register_scale(ArbitraryScale)
1250

1351
def _make_inset_locator(rect, trans, parent):
1452
"""
@@ -120,13 +158,34 @@ def set_location(self, location):
120158
self.set_axes_locator(secondary_locator)
121159
self._y = y
122160

161+
def set_xticks(self, ticks, minor=False):
162+
"""
163+
Set the x ticks with list of *ticks*
164+
165+
Parameters
166+
----------
167+
ticks : list
168+
List of x-axis tick locations.
169+
170+
minor : bool, optional
171+
If ``False`` sets major ticks, if ``True`` sets minor ticks.
172+
Default is ``False``.
173+
"""
174+
ret = self.xaxis.set_ticks(ticks, minor=minor)
175+
self.stale = True
176+
177+
lims = self._parent.get_xlim()
178+
self.set_xlim(self._convert.transform(lims))
179+
return ret
180+
181+
123182
def set_conversion(self, conversion):
124183
"""
125184
Set how the secondary axis converts limits from the parent axes.
126185
127186
Parameters
128187
----------
129-
conversion : tuple of floats or function
188+
conversion : tuple of floats, transform, or string
130189
conversion between the parent xaxis values and the secondary xaxis
131190
values. If a tuple of floats, the floats are polynomial
132191
co-efficients, with the first entry the highest exponent's
@@ -137,8 +196,9 @@ def set_conversion(self, conversion):
137196
"""
138197

139198
# make the _convert function...
140-
if callable(conversion):
199+
if isinstance(conversion, mtransforms.Transform):
141200
self._convert = conversion
201+
self.set_xscale('arbitrary', transform=conversion)
142202
else:
143203
if isinstance(conversion, numbers.Number):
144204
conversion = np.asanyarray([conversion])
@@ -158,7 +218,13 @@ def draw(self, renderer=None, inframe=False):
158218
159219
"""
160220
lims = self._parent.get_xlim()
161-
self.set_xlim(self._convert(lims))
221+
order = lims[0] < lims[1]
222+
lims = self._convert.transform(lims)
223+
neworder = lims[0] < lims[1]
224+
if neworder != order:
225+
# flip because the transform will take care of the flipping..
226+
lims = lims[::-1]
227+
self.set_xlim(lims)
162228
super().draw(renderer=renderer, inframe=inframe)
163229

164230
def set_xlabel(self, xlabel, fontdict=None, labelpad=None, **kwargs):

0 commit comments

Comments
 (0)