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

Skip to content

Commit 4b22def

Browse files
committed
ENH: add arbitrary-scale functionality
API: add ability to set formatter and locators from scale init
1 parent ac0525e commit 4b22def

File tree

1 file changed

+89
-0
lines changed

1 file changed

+89
-0
lines changed

lib/matplotlib/scale.py

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,94 @@ def get_transform(self):
8282
return IdentityTransform()
8383

8484

85+
class ArbitraryTransform(Transform):
86+
"""
87+
A simple transform that takes and arbitrary function for the
88+
forward and inverse transform.
89+
"""
90+
91+
input_dims = 1
92+
output_dims = 1
93+
is_separable = True
94+
has_inverse = True
95+
96+
def __init__(self, forward, inverse):
97+
"""
98+
Parameters
99+
----------
100+
101+
forward: The forward function for the transform
102+
103+
inverse: The inverse of the forward function.
104+
"""
105+
super().__init__()
106+
if callable(forward) and callable(inverse):
107+
self._forward = forward
108+
self._inverse = inverse
109+
else:
110+
raise ValueError('arguments to ArbitraryTransform must '
111+
'be functions')
112+
113+
def transform_non_affine(self, values):
114+
return self._forward(values)
115+
116+
def inverted(self):
117+
return ArbitraryTransform(self._inverse, self._forward)
118+
119+
120+
class ArbitraryScale(ScaleBase):
121+
"""
122+
Provide an arbitrary scale for the axis.
123+
"""
124+
125+
name = 'arbitrary'
126+
127+
def __init__(self, axis, functions, *,
128+
major_locator_class=AutoLocator,
129+
minor_locator_class=NullLocator,
130+
major_formatter_class=ScalarFormatter,
131+
minor_formatter_class=NullFormatter):
132+
"""
133+
Parameters
134+
----------
135+
136+
axis: the axis for the scale
137+
138+
functions: (forward, inverse)
139+
two-tuple of the forward and inverse functions for the scale.
140+
141+
Examples
142+
--------
143+
144+
TODO
145+
"""
146+
147+
forward, inverse = functions
148+
transform = ArbitraryTransform(forward, inverse)
149+
self._transform = transform
150+
self._major_locator_class = major_locator_class
151+
self._minor_locator_class = minor_locator_class
152+
self._major_formatter_class = major_formatter_class
153+
self._minor_formatter_class = minor_formatter_class
154+
155+
def get_transform(self):
156+
"""
157+
The transform for linear scaling is just the
158+
:class:`~matplotlib.transforms.IdentityTransform`.
159+
"""
160+
return self._transform
161+
162+
def set_default_locators_and_formatters(self, axis):
163+
"""
164+
Set the locators and formatters to reasonable defaults for
165+
linear scaling.
166+
"""
167+
axis.set_major_locator(self._major_locator_class())
168+
axis.set_major_formatter(self._major_formatter_class())
169+
axis.set_minor_locator(self._minor_locator_class())
170+
axis.set_minor_formatter(self._minor_formatter_class())
171+
172+
85173
class LogTransformBase(Transform):
86174
input_dims = 1
87175
output_dims = 1
@@ -545,6 +633,7 @@ def limit_range_for_scale(self, vmin, vmax, minpos):
545633
'log': LogScale,
546634
'symlog': SymmetricalLogScale,
547635
'logit': LogitScale,
636+
'arbitrary': ArbitraryScale,
548637
}
549638

550639

0 commit comments

Comments
 (0)