6
6
7
7
import matplotlib .ticker as mticker
8
8
import matplotlib .transforms as mtransforms
9
+ import matplotlib .scale as mscale
9
10
10
11
from matplotlib .axes ._base import _AxesBase
11
12
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 )
12
50
13
51
def _make_inset_locator (rect , trans , parent ):
14
52
"""
@@ -120,13 +158,34 @@ def set_location(self, location):
120
158
self .set_axes_locator (secondary_locator )
121
159
self ._y = y
122
160
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
+
123
182
def set_conversion (self , conversion ):
124
183
"""
125
184
Set how the secondary axis converts limits from the parent axes.
126
185
127
186
Parameters
128
187
----------
129
- conversion : tuple of floats or function
188
+ conversion : tuple of floats, transform, or string
130
189
conversion between the parent xaxis values and the secondary xaxis
131
190
values. If a tuple of floats, the floats are polynomial
132
191
co-efficients, with the first entry the highest exponent's
@@ -137,8 +196,9 @@ def set_conversion(self, conversion):
137
196
"""
138
197
139
198
# make the _convert function...
140
- if callable (conversion ):
199
+ if isinstance (conversion , mtransforms . Transform ):
141
200
self ._convert = conversion
201
+ self .set_xscale ('arbitrary' , transform = conversion )
142
202
else :
143
203
if isinstance (conversion , numbers .Number ):
144
204
conversion = np .asanyarray ([conversion ])
@@ -158,7 +218,13 @@ def draw(self, renderer=None, inframe=False):
158
218
159
219
"""
160
220
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 )
162
228
super ().draw (renderer = renderer , inframe = inframe )
163
229
164
230
def set_xlabel (self , xlabel , fontdict = None , labelpad = None , ** kwargs ):
0 commit comments