From a63787a92632d64c6ba4cf3236beb29b22eda76e Mon Sep 17 00:00:00 2001 From: Antony Lee Date: Wed, 4 Dec 2019 16:14:42 +0100 Subject: [PATCH] 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. --- lib/matplotlib/axes/_axes.py | 13 ++++--------- lib/matplotlib/colors.py | 11 +++++------ lib/matplotlib/tests/test_axes.py | 8 ++++++-- 3 files changed, 15 insertions(+), 17 deletions(-) diff --git a/lib/matplotlib/axes/_axes.py b/lib/matplotlib/axes/_axes.py index 152d00b0e4a3..379e42c02a63 100644 --- a/lib/matplotlib/axes/_axes.py +++ b/lib/matplotlib/axes/_axes.py @@ -6586,8 +6586,6 @@ def hist(self, x, bins=None, range=None, density=False, weights=None, if histtype == 'barstacked' and not stacked: stacked = True - # basic input validation - input_empty = np.size(x) == 0 # Massage 'x' for processing. x = cbook._reshape_2D(x, 'x') nx = len(x) # number of datasets @@ -6649,13 +6647,10 @@ def hist(self, x, bins=None, range=None, density=False, weights=None, # If bins are not specified either explicitly or via range, # we need to figure out the range required for all datasets, # and supply that to np.histogram. - if not input_empty and len(x) > 1: - if weights is not None: - _w = np.concatenate(w) - else: - _w = None - bins = np.histogram_bin_edges( - np.concatenate(x), bins, bin_range, _w) + all_xs = np.concatenate(x) + if len(all_xs): + all_ws = np.concatenate(w) if weights is not None else None + bins = np.histogram_bin_edges(all_xs, bins, bin_range, all_ws) else: hist_kwargs['range'] = bin_range diff --git a/lib/matplotlib/colors.py b/lib/matplotlib/colors.py index 300e33bc94f6..f4bca75d8870 100644 --- a/lib/matplotlib/colors.py +++ b/lib/matplotlib/colors.py @@ -68,6 +68,7 @@ from collections.abc import Sized import functools import itertools +from numbers import Number import re import numpy as np @@ -260,14 +261,12 @@ def _to_rgba_no_colorcycle(c, alpha=None): return c, c, c, alpha if alpha is not None else 1. raise ValueError(f"Invalid RGBA argument: {orig_c!r}") # tuple color. - c = np.array(c) - if not np.can_cast(c.dtype, float, "same_kind") or c.ndim != 1: - # Test the dtype explicitly as `map(float, ...)`, `np.array(..., - # float)` and `np.array(...).astype(float)` all convert "0.5" to 0.5. - # Test dimensionality to reject single floats. + if not np.iterable(c) or not all(isinstance(x, Number) for x in c): + # Checks that don't work: `map(float, ...)`, `np.array(..., float)` and + # `np.array(...).astype(float)` would all convert "0.5" to 0.5. raise ValueError(f"Invalid RGBA argument: {orig_c!r}") # Return a tuple to prevent the cached value from being modified. - c = tuple(c.astype(float)) + c = tuple(map(float, c)) if len(c) not in [3, 4]: raise ValueError("RGBA sequence should have length 3 or 4") if len(c) == 3 and alpha is None: diff --git a/lib/matplotlib/tests/test_axes.py b/lib/matplotlib/tests/test_axes.py index c63196056496..3a01f8a182e8 100644 --- a/lib/matplotlib/tests/test_axes.py +++ b/lib/matplotlib/tests/test_axes.py @@ -604,7 +604,9 @@ def test_shaped_data(): plt.plot(y2) plt.subplot(413) - with pytest.raises(ValueError): + # DeprecationWarning is raised by numpy 1.19 on object array creation; + # later it will be a ValueError again. + with pytest.raises((ValueError, DeprecationWarning)): plt.plot((y1, y2)) plt.subplot(414) @@ -2697,7 +2699,9 @@ def test_boxplot_bad_ci_2(): x = np.linspace(-7, 7, 140) x = np.hstack([-25, x, 25]) fig, ax = plt.subplots() - with pytest.raises(ValueError): + # DeprecationWarning is raised by numpy 1.19 on object array creation; + # later it will be a ValueError again. + with pytest.raises((ValueError, DeprecationWarning)): ax.boxplot([x, x], conf_intervals=[[1, 2], [1]])