diff --git a/lib/matplotlib/cbook/__init__.py b/lib/matplotlib/cbook/__init__.py index 0b6a4968b113..fbeb3defd4db 100644 --- a/lib/matplotlib/cbook/__init__.py +++ b/lib/matplotlib/cbook/__init__.py @@ -2167,7 +2167,7 @@ def pts_to_prestep(x, *args): Parameters ---------- x : array - The x location of the steps. + The x location of the steps. May be empty. y1, ..., yp : array y arrays to be turned into steps; all must be the same length as ``x``. @@ -2177,13 +2177,14 @@ def pts_to_prestep(x, *args): out : array The x and y values converted to steps in the same order as the input; can be unpacked as ``x_out, y1_out, ..., yp_out``. If the input is - length ``N``, each of these arrays will be length ``2N + 1``. + length ``N``, each of these arrays will be length ``2N + 1``. For + ``N=0``, the length will be 0. Examples -------- >> x_s, y1_s, y2_s = pts_to_prestep(x, y1, y2) """ - steps = np.zeros((1 + len(args), 2 * len(x) - 1)) + steps = np.zeros((1 + len(args), max(2 * len(x) - 1, 0))) # In all `pts_to_*step` functions, only assign *once* using `x` and `args`, # as converting to an array may be expensive. steps[0, 0::2] = x @@ -2204,7 +2205,7 @@ def pts_to_poststep(x, *args): Parameters ---------- x : array - The x location of the steps. + The x location of the steps. May be empty. y1, ..., yp : array y arrays to be turned into steps; all must be the same length as ``x``. @@ -2214,13 +2215,14 @@ def pts_to_poststep(x, *args): out : array The x and y values converted to steps in the same order as the input; can be unpacked as ``x_out, y1_out, ..., yp_out``. If the input is - length ``N``, each of these arrays will be length ``2N + 1``. + length ``N``, each of these arrays will be length ``2N + 1``. For + ``N=0``, the length will be 0. Examples -------- >> x_s, y1_s, y2_s = pts_to_poststep(x, y1, y2) """ - steps = np.zeros((1 + len(args), 2 * len(x) - 1)) + steps = np.zeros((1 + len(args), max(2 * len(x) - 1, 0))) steps[0, 0::2] = x steps[0, 1::2] = steps[0, 2::2] steps[1:, 0::2] = args @@ -2239,7 +2241,7 @@ def pts_to_midstep(x, *args): Parameters ---------- x : array - The x location of the steps. + The x location of the steps. May be empty. y1, ..., yp : array y arrays to be turned into steps; all must be the same length as ``x``. @@ -2258,7 +2260,8 @@ def pts_to_midstep(x, *args): steps = np.zeros((1 + len(args), 2 * len(x))) x = np.asanyarray(x) steps[0, 1:-1:2] = steps[0, 2::2] = (x[:-1] + x[1:]) / 2 - steps[0, 0], steps[0, -1] = x[0], x[-1] + steps[0, :1] = x[:1] # Also works for zero-sized input. + steps[0, -1:] = x[-1:] steps[1:, 0::2] = args steps[1:, 1::2] = steps[1:, 0::2] return steps diff --git a/lib/matplotlib/tests/test_cbook.py b/lib/matplotlib/tests/test_cbook.py index 4ff2cc52abb3..50ac01a42106 100644 --- a/lib/matplotlib/tests/test_cbook.py +++ b/lib/matplotlib/tests/test_cbook.py @@ -392,6 +392,11 @@ def test_to_prestep(): assert_array_equal(y1_target, y1s) +def test_to_prestep_empty(): + steps = cbook.pts_to_prestep([], []) + assert steps.shape == (2, 0) + + def test_to_poststep(): x = np.arange(4) y1 = np.arange(4) @@ -412,6 +417,11 @@ def test_to_poststep(): assert_array_equal(y1_target, y1s) +def test_to_poststep_empty(): + steps = cbook.pts_to_poststep([], []) + assert steps.shape == (2, 0) + + def test_to_midstep(): x = np.arange(4) y1 = np.arange(4) @@ -432,6 +442,11 @@ def test_to_midstep(): assert_array_equal(y1_target, y1s) +def test_to_midstep_empty(): + steps = cbook.pts_to_midstep([], []) + assert steps.shape == (2, 0) + + @pytest.mark.parametrize( "args", [(np.arange(12).reshape(3, 4), 'a'),