Skip to content

Commit

Permalink
Merge pull request #9997 from timhoffm/fix-empty-plot-steps
Browse files Browse the repository at this point in the history
Fix empty plot with drawstyle="steps"
  • Loading branch information
anntzer committed Dec 23, 2017
2 parents d24aea7 + cd250ac commit f96fac0
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 8 deletions.
19 changes: 11 additions & 8 deletions lib/matplotlib/cbook/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2166,7 +2166,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``.
Expand All @@ -2176,13 +2176,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
Expand All @@ -2203,7 +2204,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``.
Expand All @@ -2213,13 +2214,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
Expand All @@ -2238,7 +2240,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``.
Expand All @@ -2257,7 +2259,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
Expand Down
15 changes: 15 additions & 0 deletions lib/matplotlib/tests/test_cbook.py
Original file line number Diff line number Diff line change
Expand Up @@ -391,6 +391,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)
Expand All @@ -411,6 +416,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)
Expand All @@ -431,6 +441,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'),
Expand Down

0 comments on commit f96fac0

Please sign in to comment.