Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix empty plot with drawstyle="steps" #9997

Merged
merged 2 commits into from
Dec 23, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 11 additions & 8 deletions lib/matplotlib/cbook/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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``.
Expand All @@ -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
Expand All @@ -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``.
Expand All @@ -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
Expand All @@ -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``.
Expand All @@ -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
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 @@ -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)
Expand All @@ -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)
Expand All @@ -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'),
Expand Down