8000 _make_norm_from_scale fixes. by anntzer · Pull Request #18702 · matplotlib/matplotlib · GitHub
[go: up one dir, main page]

Skip to content

_make_norm_from_scale fixes. #18702

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
Oct 14, 2020
Merged
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
34 changes: 24 additions & 10 deletions lib/matplotlib/colors.py
Original file line number Diff line number Diff line change
Expand Up @@ -1262,29 +1262,40 @@ def _make_norm_from_scale(scale_cls, base_norm_cls=None, *, init=None):
After ::

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

*base_norm_cls* is filled with methods so that normalization computations
are forwarded to *scale_cls* (i.e., *scale_cls* is the scale that would be
used for the colorbar of a mappable normalized with *base_norm_cls*).

The constructor signature of *base_norm_cls* is derived from the
constructor signature of *scale_cls*, but can be overridden using *init*
(a callable which is *only* used for its signature).
*norm_cls* is filled with methods so that normalization computations are
forwarded to *scale_cls* (i.e., *scale_cls* is the scale that would be used
for the colorbar of a mappable normalized with *norm_cls*).

If *init* is not passed, then the constructor signature of *norm_cls*
will be ``norm_cls(vmin=None, vmax=None, clip=False)``; these three
parameters will be forwarded to the base class (``Normalize.__init__``),
and a *scale_cls* object will be initialized with no arguments (other than
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
*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*,
*vmax*, and *clip* values, pass those to ``Normalize.__init__``, and
forward the remaining bound values (including any defaults defined by the
signature) to the *scale_cls* constructor.
"""

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

if init is None:
def init(vmin=None, vmax=None, clip=False): pass
init_signature = inspect.signature(init)
bound_init_signature = inspect.signature(init)

class Norm(base_norm_cls):

def __init__(self, *args, **kwargs):
ba = init_signature.bind(*args, **kwargs)
ba = bound_init_signature.bind(*args, **kwargs)
ba.apply_defaults()
super().__init__(
**{k: ba.arguments.pop(k) for k in ["vmin", "vmax", "clip"]})
Expand Down Expand Up @@ -1329,6 +1340,9 @@ def inverse(self, value):
Norm.__name__ = base_norm_cls.__name__
Norm.__qualname__ = base_norm_cls.__qualname__
Norm.__module__ = base_norm_cls.__module__
Norm.__init__.__signature__ = bound_init_signature.replace(parameters=[
inspect.Parameter("self", inspect.Parameter.POSITIONAL_OR_KEYWORD),
*bound_init_signature.parameters.values()])
return Norm


Expand Down
0