-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Enh arbitrary scale #12818
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Enh arbitrary scale #12818
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
:orphan: | ||
|
||
New `~.scale.FuncScale` added for arbitrary axes scales | ||
```````````````````````````````````````````````````````` | ||
|
||
A new `~.scale.FuncScale` class was added (and `~.scale.FuncTransform`) | ||
to allow the user to have arbitrary scale transformations without having to | ||
write a new subclass of `~.scale.ScaleBase`. This can be accessed by | ||
``ax.set_yscale('function', functions=(forward, inverse))``, where | ||
``forward`` and ``inverse`` are callables that return the scale transform and | ||
its inverse. See the last example in :doc:`/gallery/scales/scales`. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -94,6 +94,96 @@ def get_transform(self): | |
return IdentityTransform() | ||
|
||
|
||
class FuncTransform(Transform): | ||
""" | ||
A simple transform that takes and arbitrary function for the | ||
forward and inverse transform. | ||
""" | ||
|
||
input_dims = 1 | ||
output_dims = 1 | ||
is_separable = True | ||
has_inverse = True | ||
|
||
def __init__(self, forward, inverse): | ||
""" | ||
Parameters | ||
---------- | ||
|
||
forward : callable | ||
The forward function for the transform. This function must have | ||
an inverse and, for best behavior, be monotonic. | ||
It must have the signature:: | ||
|
||
def forward(values: array-like) -> array-like | ||
|
||
inverse : callable | ||
The inverse of the forward function. Signature as ``forward``. | ||
""" | ||
super().__init__() | ||
if callable(forward) and callable(inverse): | ||
self._forward = forward | ||
self._inverse = inverse | ||
else: | ||
raise ValueError('arguments to FuncTransform must ' | ||
'be functions') | ||
|
||
def transform_non_affine(self, values): | ||
return self._forward(values) | ||
|
||
def inverted(self): | ||
return FuncTransform(self._inverse, self._forward) | ||
|
||
|
||
class FuncScale(ScaleBase): | ||
""" | ||
Provide an arbitrary scale with user-supplied function for the axis. | ||
""" | ||
|
||
name = 'function' | ||
|
||
def __init__(self, axis, functions): | ||
""" | ||
Parameters | ||
---------- | ||
|
||
axis: the axis for the scale | ||
|
||
functions : (callable, callable) | ||
two-tuple of the forward and inverse functions for the scale. | ||
The forward function must have an inverse and, for best behavior, | ||
be monotonic. | ||
|
||
Both functions must have the signature:: | ||
|
||
def forward(values: array-like) -> array-like | ||
""" | ||
forward, inverse = functions | ||
transform = FuncTransform(forward, inverse) | ||
self._transform = transform | ||
|
||
def get_transform(self): | ||
""" | ||
The transform for arbitrary scaling | ||
""" | ||
return self._transform | ||
|
||
def set_default_locators_and_formatters(self, axis): | ||
""" | ||
Set the locators and formatters to the same defaults as the | ||
linear scale. | ||
""" | ||
axis.set_major_locator(AutoLocator()) | ||
axis.set_major_formatter(ScalarFormatter()) | ||
axis.set_minor_formatter(NullFormatter()) | ||
# update the minor locator for x and y axis based on rcParams | ||
if (axis.axis_name == 'x' and rcParams['xtick.minor.visible'] | ||
or axis.axis_name == 'y' and rcParams['ytick.minor.visible']): | ||
axis.set_minor_locator(AutoMinorLocator()) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you incoorporate the fix from #12938 here as well? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorry for the delay - Done! |
||
else: | ||
axis.set_minor_locator(NullLocator()) | ||
|
||
|
||
class LogTransformBase(Transform): | ||
input_dims = 1 | ||
output_dims = 1 | ||
|
@@ -557,6 +647,7 @@ def limit_range_for_scale(self, vmin, vmax, minpos): | |
'log': LogScale, | ||
'symlog': SymmetricalLogScale, | ||
'logit': LogitScale, | ||
'function': FuncScale, | ||
} | ||
|
||
|
||
|
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a reason why FuncTransform takes the two functions as separate args but FuncScale takes the pair as single arg? (Perhaps there is, didn't think more than that about it.)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just because the kwarg gets passed in as a tuple, whereas to me it makes more sense for the transform to take them separately. But I don't feel very strongly about it.