Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
updates based on feedback from review, @oscargus, @anntzer
  • Loading branch information
trygvrad committed Apr 10, 2025
commit 6985111180e48aa98fb02bc7e0ede110fac18128
44 changes: 37 additions & 7 deletions lib/matplotlib/colors.py
Original file line number Diff line number Diff line change
Expand Up @@ -1595,14 +1595,14 @@ def with_extremes(self, *, bad=None, under=None, over=None):

Parameters
----------
bad: :mpltype:`color`, default: None
bad : :mpltype:`color`, default: None
If Matplotlib color, the bad value is set accordingly in the copy

under: tuple of :mpltype:`color`, default: None
under : tuple of :mpltype:`color`, default: None
If tuple, the ``under`` value of each component is set with the values
from the tuple.

over: tuple of :mpltype:`color`, default: None
over : tuple of :mpltype:`color`, default: None
If tuple, the ``over`` value of each component is set with the values
from the tuple.

Expand Down Expand Up @@ -3255,12 +3255,13 @@ def __init__(self, norms, vmin=None, vmax=None, clip=False):

if isinstance(norms, str) or not np.iterable(norms):
raise ValueError("A MultiNorm must be assigned multiple norms")
norms = [n for n in norms]

norms = [*norms]
for i, n in enumerate(norms):
if n is None:
norms[i] = Normalize()
elif isinstance(n, str):
scale_cls = scale._get_scale_cls_from_str(n)
scale_cls = _get_scale_cls_from_str(n)
norms[i] = mpl.colorizer._auto_norm_from_scale(scale_cls)()

# Convert the list of norms to a tuple to make it immutable.
Expand Down Expand Up @@ -3354,7 +3355,7 @@ def __call__(self, value, clip=None):
value
Data to normalize. Must be of length `n_input` or have a data type with
`n_input` fields.
clip : List of bools or bool, optional
clip : list of bools or bool, optional
See the description of the parameter *clip* in Normalize.
If ``None``, defaults to ``self.clip`` (which defaults to
``False``).
Expand Down Expand Up @@ -3424,7 +3425,7 @@ def autoscale_None(self, A):
self._changed()

def scaled(self):
"""Return whether both *vmin* and *vmax* are set on all constitient norms"""
"""Return whether both *vmin* and *vmax* are set on all constituent norms"""
return all([(n.vmin is not None and n.vmax is not None) for n in self.norms])

@staticmethod
Expand Down Expand Up @@ -4092,3 +4093,32 @@ def from_levels_and_colors(levels, colors, extend='neither'):

norm = BoundaryNorm(levels, ncolors=n_data_colors)
return cmap, norm


def _get_scale_cls_from_str(scale_as_str):
"""
Returns the scale class from a string.

Used in the creation of norms from a string to ensure a reasonable error
in the case where an invalid string is used. This cannot use
`_api.check_getitem()`, because the norm keyword accepts arguments
other than strings.

Parameters
----------
scale_as_str : string
A string corresponding to a scale

Returns
-------
A subclass of ScaleBase.

"""
try:
scale_cls = scale._scale_mapping[scale_as_str]
except KeyError:
raise ValueError(
"Invalid norm str name; the following values are "
f"supported: {', '.join(scale._scale_mapping)}"
) from None
return scale_cls
29 changes: 0 additions & 29 deletions lib/matplotlib/scale.py
Original file line number Diff line number Diff line change
Expand Up @@ -715,35 +715,6 @@ def get_scale_names():
return sorted(_scale_mapping)


def _get_scale_cls_from_str(scale_as_str):
"""
Returns the scale class from a string.

Used in the creation of norms from a string to ensure a reasonable error
in the case where an invalid string is used. This cannot use
`_api.check_getitem()`, because the norm keyword accepts arguments
other than strings.

Parameters
----------
scale_as_str : string
A string corresponding to a scale

Returns
-------
A subclass of ScaleBase.

"""
try:
scale_cls = _scale_mapping[scale_as_str]
except KeyError:
raise ValueError(
"Invalid norm str name; the following values are "
f"supported: {', '.join(_scale_mapping)}"
) from None
return scale_cls


def scale_factory(scale, axis, **kwargs):
"""
Return a scale class by name.
Expand Down