Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

New data → color pipeline#28658

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
tacaswell merged 18 commits intomatplotlib:mainfromtrygvrad:Colorizer-class
Oct 24, 2024
Merged
Show file tree
Hide file tree
Changes from1 commit
Commits
Show all changes
18 commits
Select commitHold shift + click to select a range
1e52ba8
Colorizer class
trygvradAug 2, 2024
9e5620d
Creation of colorizer.py
trygvradAug 6, 2024
b3f5260
updated ColorizingArtist.__init__
trygvradAug 7, 2024
edbe127
simplify to_rgba() by extracting the part relating to RGBA data
trygvradAug 9, 2024
fc9973e
updated class hierarchy for Colorizer
trygvradAug 13, 2024
596daec
colorizer keyword on plotting functions with typing
trygvradAug 13, 2024
21a5a64
changes to keyword parameter ordering
trygvradAug 16, 2024
5755d1b
adjustments based on code review
trygvradAug 21, 2024
a6fe9e8
MNT: adjust inheritance so isinstance(..., cm.ScalarMappable) works
tacaswellAug 21, 2024
dbe8cc3
updated docs with colorizer changes
trygvradAug 21, 2024
9ac2739
updates to colorizer pipeline based on feedback from @QuLogic
trygvradAug 27, 2024
7dd31ad
DOC: fix unrelated xref issues
tacaswellAug 29, 2024
1ecfe95
DOC: add multivariate colormaps to the docs
tacaswellAug 29, 2024
c685f36
DOC: fix colorizer related xrefs
tacaswellAug 29, 2024
676a31f
DOC: auto-generate ScalarMappable in conf.py
tacaswellAug 29, 2024
c3cad60
Corrections based on feedback from @QuLogic
trygvradSep 6, 2024
269ace9
MNT: Touch up rebase to use 'register' for docstring interpolations
ksundenOct 11, 2024
336a9ba
fix missing refererence linenos
ksundenOct 23, 2024
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
PrevPrevious commit
NextNext commit
Corrections based on feedback from@QuLogic
  • Loading branch information
@trygvrad@ksunden
trygvrad authored andksunden committedOct 18, 2024
commitc3cad605ef2ead73803a5e0dc4010436d28e38ef
68 changes: 39 additions & 29 deletionslib/matplotlib/colorizer.py
View file
Open in desktop
Original file line numberDiff line numberDiff 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

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 createdbase on *norm* and *cmap*.""",
object is createdfrom 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
"""
def __init__(self, cmap=None, norm=None):

Expand DownExpand 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 DownExpand 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 DownExpand 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 DownExpand 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 DownExpand Up@@ -582,7 +591,7 @@ def _check_exclusionary_keywords(colorizer, **kwargs):

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


class ColorizingArtist(_ScalarMappable, artist.Artist):
"""
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 deletionslib/matplotlib/contour.py
View file
Open in desktop
Original file line numberDiff line numberDiff 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 deletionslib/matplotlib/tests/test_colors.py
View file
Open in desktop
Original file line numberDiff line numberDiff 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 DownExpand 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

[8]ページ先頭

©2009-2025 Movatter.jp