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

Commitc5393d5

Browse files
committed
change Colorizer.to_rgba() to Colorizer.__call__()
1 parente7d53e0 commitc5393d5

File tree

4 files changed

+55
-52
lines changed

4 files changed

+55
-52
lines changed

‎lib/matplotlib/artist.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1446,7 +1446,7 @@ def set_array(self, A):
14461446
A : array-like or None
14471447
The values that are mapped to colors.
14481448
1449-
The base class `.VectorMappable` does not make any assumptions on
1449+
The base class `.ColorizingArtist` does not make any assumptions on
14501450
the dimensionality and shape of the value array *A*.
14511451
"""
14521452
ifAisNone:
@@ -1466,7 +1466,7 @@ def get_array(self):
14661466
"""
14671467
Return the array of values, that are mapped to colors.
14681468
1469-
The base class `.VectorMappable` does not make any assumptions on
1469+
The base class `.ColorizingArtist` does not make any assumptions on
14701470
the dimensionality and shape of the array.
14711471
"""
14721472
returnself._A

‎lib/matplotlib/cm.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ class ScalarMappable(colorizer.ColorizerShim):
279279
"""
280280
A mixin class to map one or multiple sets of scalar data to RGBA.
281281
282-
TheVectorMappable applies data normalization before returning RGBA colors
282+
TheScalarMappable applies data normalization before returning RGBA colors
283283
from the given `~matplotlib.colors.Colormap`, `~matplotlib.colors.BivarColormap`,
284284
or `~matplotlib.colors.MultivarColormap`.
285285
"""

‎lib/matplotlib/colorizer.py

Lines changed: 51 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@
2424
classColorizer():
2525
"""
2626
Class that holds the data to color pipeline
27-
accessible via `.to_rgba(A)` and executed via
28-
the `.norm` and `.cmap` attributes.
27+
accessible via `Colorizer.__call__(A)` and executed via
28+
the `Colorizer.norm` and `Colorizer.cmap` attributes.
2929
"""
3030
def__init__(self,cmap=None,norm=None):
3131

@@ -94,7 +94,7 @@ def norm(self, norm):
9494
ifnotin_init:
9595
self.changed()
9696

97-
defto_rgba(self,x,alpha=None,bytes=False,norm=True):
97+
def__call__(self,x,alpha=None,bytes=False,norm=True):
9898
"""
9999
Return a normalized RGBA array corresponding to *x*.
100100
@@ -125,56 +125,59 @@ def to_rgba(self, x, alpha=None, bytes=False, norm=True):
125125
126126
"""
127127
# First check for special case, image input:
128-
# First check for special case, image input:
129-
try:
130-
ifx.ndim==3:
131-
ifx.shape[2]==3:
132-
ifalphaisNone:
133-
alpha=1
134-
ifx.dtype==np.uint8:
135-
alpha=np.uint8(alpha*255)
136-
m,n=x.shape[:2]
137-
xx=np.empty(shape=(m,n,4),dtype=x.dtype)
138-
xx[:, :, :3]=x
139-
xx[:, :,3]=alpha
140-
elifx.shape[2]==4:
141-
xx=x
142-
else:
143-
raiseValueError("Third dimension must be 3 or 4")
144-
ifxx.dtype.kind=='f':
145-
# If any of R, G, B, or A is nan, set to 0
146-
ifnp.any(nans:=np.isnan(x)):
147-
ifx.shape[2]==4:
148-
xx=xx.copy()
149-
xx[np.any(nans,axis=2), :]=0
150-
151-
ifnormand (xx.max()>1orxx.min()<0):
152-
raiseValueError("Floating point image RGB values "
153-
"must be in the 0..1 range.")
154-
ifbytes:
155-
xx= (xx*255).astype(np.uint8)
156-
elifxx.dtype==np.uint8:
157-
ifnotbytes:
158-
xx=xx.astype(np.float32)/255
159-
else:
160-
raiseValueError("Image RGB array must be uint8 or "
161-
"floating point; found %s"%xx.dtype)
162-
# Account for any masked entries in the original array
163-
# If any of R, G, B, or A are masked for an entry, we set alpha to 0
164-
ifnp.ma.is_masked(x):
165-
xx[np.any(np.ma.getmaskarray(x),axis=2),3]=0
166-
returnxx
167-
exceptAttributeError:
168-
# e.g., x is not an ndarray; so try mapping it
169-
pass
170-
171-
# This is the normal case, mapping a scalar array:
128+
ifisinstance(x,np.ndarray)andx.ndim==3:
129+
returnself._pass_image_data(x,alpha,bytes,norm)
130+
131+
# Otherwise run norm -> colormap pipeline
172132
x=ma.asarray(x)
173133
ifnorm:
174134
x=self.norm(x)
175135
rgba=self.cmap(x,alpha=alpha,bytes=bytes)
176136
returnrgba
177137

138+
@staticmethod
139+
def_pass_image_data(x,alpha=None,bytes=False,norm=True):
140+
"""
141+
Helper function to pass ndarray of shape (...,3) or (..., 4)
142+
through `__call__()`, see `__call__()` for docstring.
143+
"""
144+
ifx.shape[2]==3:
145+
ifalphaisNone:
146+
alpha=1
147+
ifx.dtype==np.uint8:
148+
alpha=np.uint8(alpha*255)
149+
m,n=x.shape[:2]
150+
xx=np.empty(shape=(m,n,4),dtype=x.dtype)
151+
xx[:, :, :3]=x
152+
xx[:, :,3]=alpha
153+
elifx.shape[2]==4:
154+
xx=x
155+
else:
156+
raiseValueError("Third dimension must be 3 or 4")
157+
ifxx.dtype.kind=='f':
158+
# If any of R, G, B, or A is nan, set to 0
159+
ifnp.any(nans:=np.isnan(x)):
160+
ifx.shape[2]==4:
161+
xx=xx.copy()
162+
xx[np.any(nans,axis=2), :]=0
163+
164+
ifnormand (xx.max()>1orxx.min()<0):
165+
raiseValueError("Floating point image RGB values "
166+
"must be in the 0..1 range.")
167+
ifbytes:
168+
xx= (xx*255).astype(np.uint8)
169+
elifxx.dtype==np.uint8:
170+
ifnotbytes:
171+
xx=xx.astype(np.float32)/255
172+
else:
173+
raiseValueError("Image RGB array must be uint8 or "
174+
"floating point; found %s"%xx.dtype)
175+
# Account for any masked entries in the original array
176+
# If any of R, G, B, or A are masked for an entry, we set alpha to 0
177+
ifnp.ma.is_masked(x):
178+
xx[np.any(np.ma.getmaskarray(x),axis=2),3]=0
179+
returnxx
180+
178181
defnormalize(self,x):
179182
"""
180183
Normalize the data in x.
@@ -350,7 +353,7 @@ def to_rgba(self, x, alpha=None, bytes=False, norm=True):
350353
performed, and it is assumed to be in the range (0-1).
351354
352355
"""
353-
returnself.colorizer.to_rgba(x,alpha=alpha,bytes=bytes,norm=norm)
356+
returnself.colorizer(x,alpha=alpha,bytes=bytes,norm=norm)
354357

355358
defget_clim(self):
356359
"""

‎lib/matplotlib/image.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1583,7 +1583,7 @@ def imsave(fname, arr, vmin=None, vmax=None, cmap=None, format=None,
15831583
else:
15841584
sm=colorizer.Colorizer(cmap=cmap)
15851585
sm.set_clim(vmin,vmax)
1586-
rgba=sm.to_rgba(arr,bytes=True)
1586+
rgba=sm(arr,bytes=True)
15871587
ifpil_kwargsisNone:
15881588
pil_kwargs= {}
15891589
else:

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp