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
updated class hierarchy for Colorizer
  • Loading branch information
@trygvrad@ksunden
trygvrad authored andksunden committedOct 18, 2024
commitfc9973e6bb1aeec28cd4080bc32e4040f6195552
62 changes: 0 additions & 62 deletionslib/matplotlib/artist.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -13,7 +13,6 @@

import matplotlib as mpl
from . import _api, cbook
from .colorizer import Colorizer
from .path import Path
from .transforms import (BboxBase, Bbox, IdentityTransform, Transform, TransformedBbox,
TransformedPatchPath, TransformedPath)
Expand DownExpand Up@@ -1393,67 +1392,6 @@ def set_mouseover(self, mouseover):
mouseover = property(get_mouseover, set_mouseover) # backcompat.


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

Artist.__init__(self)

self._A = None

self.colorizer = colorizer
self._id_colorizer = self.colorizer.callbacks.connect('changed', self.changed)
self.callbacks = cbook.CallbackRegistry(signals=["changed"])

def set_array(self, A):
"""
Set the value array from array-like *A*.

Parameters
----------
A : array-like or None
The values that are mapped to colors.

The base class `.ColorizingArtist` does not make any assumptions on
the dimensionality and shape of the value array *A*.
"""
if A is None:
self._A = None
return

A = cbook.safe_masked_invalid(A, copy=True)
if not np.can_cast(A.dtype, float, "same_kind"):
raise TypeError(f"Image data of dtype {A.dtype} cannot be "
"converted to float")

self._A = A
if not self.norm.scaled():
self.colorizer.autoscale_None(A)

def get_array(self):
"""
Return the array of values, that are mapped to colors.

The base class `.ColorizingArtist` does not make any assumptions on
the dimensionality and shape of the array.
"""
return self._A

def changed(self):
"""
Call this whenever the mappable is changed to notify all the
callbackSM listeners to the 'changed' signal.
"""
self.callbacks.process('changed')
self.stale = True


def _get_tightbbox_for_layout_only(obj, *args, **kwargs):
"""
Matplotlib's `.Axes.get_tightbbox` and `.Axis.get_tightbbox` support a
Expand Down
2 changes: 1 addition & 1 deletionlib/matplotlib/cm.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -281,7 +281,7 @@ def get_cmap(name=None, lut=None):
return _colormaps[name].resampled(lut)


class ScalarMappable(colorizer.ColorizerShim):
class ScalarMappable(colorizer._ColorizerInterface):
"""
A mixin class to map one or multiple sets of scalar data to RGBA.

Expand Down
4 changes: 2 additions & 2 deletionslib/matplotlib/collections.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -33,7 +33,7 @@
"linewidth": ["linewidths", "lw"],
"offset_transform": ["transOffset"],
})
class Collection(artist.ColorizingArtist,colorizer.ColorizerShim):
class Collection(colorizer.ColorizingArtist):
r"""
Base class for Collections. Must be subclassed to be usable.

Expand DownExpand Up@@ -157,7 +157,7 @@ def __init__(self, *,
``Collection.set_{key}(val)`` for each key-value pair in *kwargs*.
"""

artist.ColorizingArtist.__init__(self, colorizer._get_colorizer(cmap, norm))
colorizer.ColorizingArtist.__init__(self, colorizer._get_colorizer(cmap, norm))
# list of un-scaled dash patterns
# this is needed scaling the dash pattern by linewidth
self._us_linestyles = [(0, None)]
Expand Down
76 changes: 72 additions & 4 deletionslib/matplotlib/colorizer.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -18,7 +18,7 @@
import numpy as np
from numpy import ma
import functools
from matplotlib import _api, colors, cbook, scale, cm
from matplotlib import _api, colors, cbook, scale, cm, artist


class Colorizer():
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Suggested change
classColorizer():
classColorizer:

pretty sure you don't need the ()

trygvrad reacted with thumbs up emoji
Expand DownExpand Up@@ -318,8 +318,15 @@ def _get_colorizer(cmap, norm):
return Colorizer(cmap, norm)


class ColorizerShim:
class _ColorizerInterface:
"""
Base class that contains the interface to `Colorizer` objects from
a `ColorizingArtist` or `cm.ScalarMappable`.

Note: This class only contain functions that interface the .colorizer
attribute. Other functions that as shared between `ColorizingArtist`
and `cm.ScalarMappable` are not included.
"""
def _scale_norm(self, norm, vmin, vmax):
self.colorizer._scale_norm(norm, vmin, vmax, self._A)

Expand DownExpand Up@@ -464,8 +471,8 @@ def _format_cursor_data_override(self, data):
# cm.ScalarMappable second, so Artist.format_cursor_data would always
# have precedence over cm.ScalarMappable.format_cursor_data.

# Note if cm.ScalarMappable is depreciated, this functionality should be
# implemented as format_cursor_data() on ColorizingArtist.
# Note if cm.ScalarMappable is depreciated, this functionality should be
# implemented as format_cursor_data() on ColorizingArtist.
n = self.cmap.N
if np.ma.getmask(data):
return "[]"
Expand DownExpand Up@@ -493,6 +500,67 @@ def _format_cursor_data_override(self, data):
return f"[{data:-#.{g_sig_digits}g}]"


class ColorizingArtist(artist.Artist, _ColorizerInterface):
def __init__(self, colorizer):
"""
Parameters
----------
colorizer : `colorizer.Colorizer`
"""
if not isinstance(colorizer, Colorizer):
raise ValueError("A `mpl.colorizer.Colorizer` object must be provided")

artist.Artist.__init__(self)

self._A = None

self.colorizer = colorizer
self._id_colorizer = self.colorizer.callbacks.connect('changed', self.changed)
self.callbacks = cbook.CallbackRegistry(signals=["changed"])

def set_array(self, A):
"""
Set the value array from array-like *A*.

Parameters
----------
A : array-like or None
The values that are mapped to colors.

The base class `.ColorizingArtist` does not make any assumptions on
the dimensionality and shape of the value array *A*.
"""
if A is None:
self._A = None
return

A = cbook.safe_masked_invalid(A, copy=True)
if not np.can_cast(A.dtype, float, "same_kind"):
raise TypeError(f"Image data of dtype {A.dtype} cannot be "
"converted to float")

self._A = A
if not self.norm.scaled():
self.colorizer.autoscale_None(A)

def get_array(self):
"""
Return the array of values, that are mapped to colors.

The base class `.ColorizingArtist` does not make any assumptions on
the dimensionality and shape of the array.
"""
return self._A

def changed(self):
"""
Call this whenever the mappable is changed to notify all the
callbackSM listeners to the 'changed' signal.
"""
self.callbacks.process('changed')
self.stale = True


def _auto_norm_from_scale(scale_cls):
"""
Automatically generate a norm class from *scale_cls*.
Expand Down
8 changes: 4 additions & 4 deletionslib/matplotlib/image.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -229,7 +229,7 @@ def _rgb_to_rgba(A):
return rgba


class _ImageBase(martist.ColorizingArtist,colorizer.ColorizerShim):
class _ImageBase(colorizer.ColorizingArtist):
"""
Base class for images.

Expand DownExpand Up@@ -258,7 +258,7 @@ def __init__(self, ax,
interpolation_stage=None,
**kwargs
):
martist.ColorizingArtist.__init__(self, colorizer._get_colorizer(cmap, norm))
colorizer.ColorizingArtist.__init__(self, colorizer._get_colorizer(cmap, norm))
if origin is None:
origin = mpl.rcParams['image.origin']
_api.check_in_list(["upper", "lower"], origin=origin)
Expand DownExpand Up@@ -330,7 +330,7 @@ def changed(self):
Call this whenever the mappable is changed so observers can update.
"""
self._imcache = None
martist.ColorizingArtist.changed(self)
colorizer.ColorizingArtist.changed(self)

def _make_image(self, A, in_bbox, out_bbox, clip_bbox, magnification=1.0,
unsampled=False, round_to_pixel_border=True):
Expand DownExpand Up@@ -1349,7 +1349,7 @@ def make_image(self, renderer, magnification=1.0, unsampled=False):

def set_data(self, A):
"""Set the image array."""
martist.ColorizingArtist.set_array(self, A)
colorizer.ColorizingArtist.set_array(self, A)
self.stale = True


Expand Down

[8]ページ先頭

©2009-2025 Movatter.jp