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

Commit4a0d9c5

Browse files
authored
Merge pull request#14313 from efiring/masked_to_rgba
Support masked array inputs for to_rgba and to_rgba_array.
2 parents9ed01e9 +4d62e65 commit4a0d9c5

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

‎lib/matplotlib/colors.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ def to_rgba(c, alpha=None):
152152
153153
Parameters
154154
----------
155-
c : Matplotlib color
155+
c : Matplotlib color or ``np.ma.masked``
156156
157157
alpha : scalar, optional
158158
If *alpha* is not ``None``, it forces the alpha value, except if *c* is
@@ -189,6 +189,8 @@ def _to_rgba_no_colorcycle(c, alpha=None):
189189
``"none"`` (case-insensitive), which always maps to ``(0, 0, 0, 0)``.
190190
"""
191191
orig_c=c
192+
ifcisnp.ma.masked:
193+
return (0.,0.,0.,0.)
192194
ifisinstance(c,str):
193195
ifc.lower()=="none":
194196
return (0.,0.,0.,0.)
@@ -260,19 +262,25 @@ def to_rgba_array(c, alpha=None):
260262
261263
If *alpha* is not ``None``, it forces the alpha value. If *c* is
262264
``"none"`` (case-insensitive) or an empty list, an empty array is returned.
265+
If *c* is a masked array, an ndarray is returned with a (0, 0, 0, 0)
266+
row for each masked value or row in *c*.
263267
"""
264268
# Special-case inputs that are already arrays, for performance. (If the
265269
# array has the wrong kind or shape, raise the error during one-at-a-time
266270
# conversion.)
267271
if (isinstance(c,np.ndarray)andc.dtype.kindin"if"
268272
andc.ndim==2andc.shape[1]in [3,4]):
273+
mask=c.mask.any(axis=1)ifnp.ma.is_masked(c)elseNone
274+
c=np.ma.getdata(c)
269275
ifc.shape[1]==3:
270276
result=np.column_stack([c,np.zeros(len(c))])
271277
result[:,-1]=alphaifalphaisnotNoneelse1.
272278
elifc.shape[1]==4:
273279
result=c.copy()
274280
ifalphaisnotNone:
275281
result[:,-1]=alpha
282+
ifmaskisnotNone:
283+
result[mask]=0
276284
ifnp.any((result<0)| (result>1)):
277285
raiseValueError("RGBA values should be within 0-1 range")
278286
returnresult

‎lib/matplotlib/tests/test_colors.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -821,6 +821,16 @@ def test_conversions():
821821
hex_color
822822

823823

824+
deftest_conversions_masked():
825+
x1=np.ma.array(['k','b'],mask=[True,False])
826+
x2=np.ma.array([[0,0,0,1], [0,0,1,1]])
827+
x2[0]=np.ma.masked
828+
assertmcolors.to_rgba(x1[0])== (0,0,0,0)
829+
assert_array_equal(mcolors.to_rgba_array(x1),
830+
[[0,0,0,0], [0,0,1,1]])
831+
assert_array_equal(mcolors.to_rgba_array(x2),mcolors.to_rgba_array(x1))
832+
833+
824834
deftest_to_rgba_array_single_str():
825835
# single color name is valid
826836
assert_array_equal(mcolors.to_rgba_array("red"), [(1,0,0,1)])

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp