Skip to content
Merged
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
Corrections based on feedback from @QuLogic
  • Loading branch information
trygvrad authored and ksunden committed Oct 18, 2024
commit c3cad605ef2ead73803a5e0dc4010436d28e38ef
68 changes: 39 additions & 29 deletions lib/matplotlib/colorizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,35 +11,44 @@

:doc:`/gallery/color/colormap_reference` for a list of builtin colormaps.

:ref:`colormap-manipulation` for examples of how to make
colormaps.
:ref:`colormap-manipulation` for examples of how to make colormaps.

:ref:`colormaps` an in-depth discussion of choosing
colormaps.
:ref:`colormaps` for an in-depth discussion of choosing colormaps.

:ref:`colormapnorms` for more details about data normalization.

"""

import functools

import numpy as np
from numpy import ma
import functools

from matplotlib import _api, colors, cbook, scale, artist
import matplotlib as mpl
Comment thread
trygvrad marked this conversation as resolved.

mpl._docstring.interpd.update(
colorizer_doc="""\
colorizer : `~matplotlib.colorizer.Colorizer` or None, default: None
The Colorizer object used to map color to data. If None, a Colorizer
object is created base on *norm* and *cmap*.""",
object is created from a *norm* and *cmap*.""",
)


class Colorizer:
"""
Class that holds the data to color pipeline
accessible via `.Colorizer.to_rgba` and executed via
Data to color pipeline.

This pipeline is accessible via `.Colorizer.to_rgba` and executed via
the `.Colorizer.norm` and `.Colorizer.cmap` attributes.

Parameters
----------
cmap: colorbar.Colorbar or str or None, default: None
The colormap used to color data.

norm: colors.Normalize or str or None, default: None
The normalization used to normalize the data
"""
Comment thread
trygvrad marked this conversation as resolved.
def __init__(self, cmap=None, norm=None):
Comment thread
QuLogic marked this conversation as resolved.

Expand Down Expand Up @@ -225,8 +234,7 @@ def _set_cmap(self, cmap):
# bury import to avoid circular imports
from matplotlib import cm
in_init = self._cmap is None
cmap = cm._ensure_cmap(cmap)
self._cmap = cmap
self._cmap = cm._ensure_cmap(cmap)
if not in_init:
self.changed() # Things are not set up properly yet.

Expand Down Expand Up @@ -280,15 +288,15 @@ def changed(self):

@property
def vmin(self):
return self.get_clim[0]
return self.get_clim()[0]

@vmin.setter
def vmin(self, vmin):
self.set_clim(vmin=vmin)

@property
def vmax(self):
return self.get_clim[1]
return self.get_clim()[1]

@vmax.setter
def vmax(self, vmax):
Expand Down Expand Up @@ -439,6 +447,9 @@ def autoscale_None(self):

@property
def colorbar(self):
"""
The last colorbar associated with this object. May be None
"""
return self._colorizer.colorbar

@colorbar.setter
Expand Down Expand Up @@ -485,10 +496,8 @@ class _ScalarMappable(_ColorizerInterface):
"""
A mixin class to map one or multiple sets of scalar data to RGBA.

The ScalarMappable applies data normalization before returning RGBA colors
from the given `~matplotlib.colors.Colormap`, or
`~matplotlib.colors.BivarColormap`.

The ScalarMappable applies data normalization before returning RGBA colors from
the given `~matplotlib.colors.Colormap`.
"""

# _ScalarMappable exists for compatibility with
Expand Down Expand Up @@ -582,7 +591,7 @@ def _check_exclusionary_keywords(colorizer, **kwargs):

@staticmethod
def _get_colorizer(cmap, norm, colorizer):
if colorizer and isinstance(colorizer, Colorizer):
if isinstance(colorizer, Colorizer):
_ScalarMappable._check_exclusionary_keywords(
Colorizer, cmap=cmap, norm=norm
)
Expand Down Expand Up @@ -620,15 +629,20 @@ def _get_colorizer(cmap, norm, colorizer):


class ColorizingArtist(_ScalarMappable, artist.Artist):
Comment thread
QuLogic marked this conversation as resolved.
"""
Base class for artists that make map data to color using a `.colorizer.Colorizer`.

The `.colorizer.Colorizer` applies data normalization before
returning RGBA colors from a `~matplotlib.colors.Colormap`.

"""
def __init__(self, colorizer, **kwargs):
"""
Parameters
----------
colorizer : `.colorizer.Colorizer`
"""
if not isinstance(colorizer, Colorizer):
raise ValueError("A `mpl.colorizer.Colorizer` object must be provided")

_api.check_isinstance(Colorizer, colorizer=colorizer)
super().__init__(colorizer=colorizer, **kwargs)

@property
Expand All @@ -637,18 +651,14 @@ def colorizer(self):

@colorizer.setter
def colorizer(self, cl):
if isinstance(cl, Colorizer):
self._colorizer.callbacks.disconnect(self._id_colorizer)
self._colorizer = cl
self._id_colorizer = cl.callbacks.connect('changed', self.changed)
else:
raise ValueError("colorizer must be a `Colorizer` object, not "
f" {type(cl)}.")
_api.check_isinstance(Colorizer, colorizer=cl)
self._colorizer.callbacks.disconnect(self._id_colorizer)
self._colorizer = cl
self._id_colorizer = cl.callbacks.connect('changed', self.changed)

def _set_colorizer_check_keywords(self, colorizer, **kwargs):
"""
Raises a ValueError if any kwarg is not None while colorizer is not None
Passes or creates a Colorizer object.
Raises a ValueError if any kwarg is not None while colorizer is not None.
"""
self._check_exclusionary_keywords(colorizer, **kwargs)
self.colorizer = colorizer
Expand Down
4 changes: 2 additions & 2 deletions lib/matplotlib/contour.py
Original file line number Diff line number Diff line change
Expand Up @@ -676,8 +676,8 @@ def __init__(self, ax, *args,

if colorizer:
self._set_colorizer_check_keywords(colorizer, cmap=cmap,
norm=norm, vmin=vmin,
vmax=vmax, colors=colors)
norm=norm, vmin=vmin,
vmax=vmax, colors=colors)
norm = colorizer.norm
cmap = colorizer.cmap
if (isinstance(norm, mcolors.LogNorm)
Expand Down
13 changes: 13 additions & 0 deletions lib/matplotlib/tests/test_colors.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import matplotlib as mpl
import matplotlib.colors as mcolors
import matplotlib.colorbar as mcolorbar
import matplotlib.colorizer as mcolorizer
import matplotlib.pyplot as plt
import matplotlib.scale as mscale
from matplotlib.rcsetup import cycler
Expand Down Expand Up @@ -1715,3 +1716,15 @@ def test_to_rgba_array_none_color_with_alpha_param():
(('C3', 0.5), True)])
def test_is_color_like(input, expected):
assert is_color_like(input) is expected


def test_colorizer_vmin_vmax():
ca = mcolorizer.Colorizer()
assert ca.vmin is None
assert ca.vmax is None
ca.vmin = 1
ca.vmax = 3
assert ca.vmin == 1.0
assert ca.vmax == 3.0
assert ca.norm.vmin == 1.0
assert ca.norm.vmax == 3.0