8000 Fix build with numpy master. · matplotlib/matplotlib@82252d9 · GitHub
[go: up one dir, main page]

Skip to content
8000

Commit 82252d9

Browse files
committed
Fix build with numpy master.
Specifically, fix places where we build object arrays implicitly, as that's deprecated on numpy master. The change to colors.py may possibly(?) make conversion to rgba actually slightly *faster* as we avoid the conversion to array in some cases.
1 parent 582ba33 commit 82252d9

File tree

2 files changed

+8
-7
lines changed

2 files changed

+8
-7
lines changed

lib/matplotlib/colors.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@
6868
from collections.abc import Sized
6969
import functools
7070
import itertools
71+
from numbers import Number
7172
import re
7273

7374
import numpy as np
@@ -260,14 +261,12 @@ def _to_rgba_no_colorcycle(c, alpha=None):
260261
return c, c, c, alpha if alpha is not None else 1.
261262
raise ValueError(f"Invalid RGBA argument: {orig_c!r}")
262263
# tuple color.
263-
c = np.array(c)
264-
if not np.can_cast(c.dtype, float, "same_kind") or c.ndim != 1:
265-
# Test the dtype explicitly as `map(float, ...)`, `np.array(...,
266-
# float)` and `np.array(...).astype(float)` all convert "0.5" to 0.5.
267-
# Test dimensionality to reject single floats.
264+
if not np.iterable(c) or not all(isinstance(x, Number) for x in c):
265+
# Checks that don't work: `map(float, ...)`, `np.array(..., float)` and
266+
# `np.array(...).astype(float)` would all convert "0.5" to 0.5.
268267
raise ValueError(f"Invalid RGBA argument: {orig_c!r}")
269268
# Return a tuple to prevent the cached value from being modified.
270-
c = tuple(c.astype(float))
269+
c = tuple(map(float, c))
271270
if len(c) not in [3, 4]:
272271
raise ValueError("RGBA sequence should have length 3 or 4")
273272
if len(c) == 3 and alpha is None:

lib/matplotlib/tests/test_axes.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -604,7 +604,9 @@ def test_shaped_data():
604604
plt.plot(y2)
605605

606606
plt.subplot(413)
607-
with pytest.raises(ValueError):
607+
# DeprecationWarning is raised by numpy 1.19 on object array creation;
608+
# later it will be a ValueError again.
609+
with pytest.raises((ValueError, DeprecationWarning)):
608610
plt.plot((y1, y2))
609611

610612
plt.subplot(414)

0 commit comments

Comments
 (0)
0