Skip to content

ENH: expose make_norm_from_scale #20405

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 1 commit into from
Jul 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions doc/api/colors_api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,4 @@ Functions
is_color_like
same_color
get_named_colors_mapping
make_norm_from_scale
8 changes: 8 additions & 0 deletions doc/api/next_api_changes/behavior/20405-JMK.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Make norm from scale now public
===============================

Downstream libraries can take advantage of `.colors.make_norm_from_scale`
to create a `~.colors.Normalize` subclass directly from an existing scale.
Usually norms have a scale, and the advantage of having a `~.scale.ScaleBase`
attached to a norm is to provide a scale, and associated tick locators and
formatters, for the colorbar.
17 changes: 9 additions & 8 deletions lib/matplotlib/colors.py
Original file line number Diff line number Diff line change
Expand Up @@ -1415,13 +1415,14 @@ def __call__(self, value, clip=None):
return super().__call__(value, clip=clip)


def _make_norm_from_scale(scale_cls, base_norm_cls=None, *, init=None):
def make_norm_from_scale(scale_cls, base_norm_cls=None, *, init=None):
"""
Decorator for building a `.Normalize` subclass from a `.Scale` subclass.
Decorator for building a `.Normalize` subclass from a `~.scale.ScaleBase`
subclass.

After ::

@_make_norm_from_scale(scale_cls)
@make_norm_from_scale(scale_cls)
class norm_cls(Normalize):
...

Expand All @@ -1436,7 +1437,7 @@ class norm_cls(Normalize):
a dummy axis).

If the *scale_cls* constructor takes additional parameters, then *init*
should be passed to `_make_norm_from_scale`. It is a callable which is
should be passed to `make_norm_from_scale`. It is a callable which is
*only* used for its signature. First, this signature will become the
signature of *norm_cls*. Second, the *norm_cls* constructor will bind the
parameters passed to it using this signature, extract the bound *vmin*,
Expand All @@ -1446,7 +1447,7 @@ class norm_cls(Normalize):
"""

if base_norm_cls is None:
return functools.partial(_make_norm_from_scale, scale_cls, init=init)
return functools.partial(make_norm_from_scale, scale_cls, init=init)

if init is None:
def init(vmin=None, vmax=None, clip=False): pass
Expand Down Expand Up @@ -1509,7 +1510,7 @@ def inverse(self, value):
return Norm


@_make_norm_from_scale(
@make_norm_from_scale(
scale.FuncScale,
init=lambda functions, vmin=None, vmax=None, clip=False: None)
class FuncNorm(Normalize):
Expand Down Expand Up @@ -1542,7 +1543,7 @@ def forward(values: array-like) -> array-like
"""


@_make_norm_from_scale(functools.partial(scale.LogScale, nonpositive="mask"))
@make_norm_from_scale(functools.partial(scale.LogScale, nonpositive="mask"))
class LogNorm(Normalize):
"""Normalize a given value to the 0-1 range on a log scale."""

Expand All @@ -1555,7 +1556,7 @@ def autoscale_None(self, A):
super().autoscale_None(np.ma.array(A, mask=(A <= 0)))


@_make_norm_from_scale(
@make_norm_from_scale(
scale.SymmetricalLogScale,
init=lambda linthresh, linscale=1., vmin=None, vmax=None, clip=False, *,
base=10: None)
Expand Down