Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f96fac0

Browse files
authored
Merge pull request #9997 from timhoffm/fix-empty-plot-steps
Fix empty plot with drawstyle="steps"
2 parents d24aea7 + cd250ac commit f96fac0

File tree

2 files changed

+26
-8
lines changed

2 files changed

+26
-8
lines changed

lib/matplotlib/cbook/__init__.py

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2166,7 +2166,7 @@ def pts_to_prestep(x, *args):
21662166
Parameters
21672167
----------
21682168
x : array
2169-
The x location of the steps.
2169+
The x location of the steps. May be empty.
21702170
21712171
y1, ..., yp : array
21722172
y arrays to be turned into steps; all must be the same length as ``x``.
@@ -2176,13 +2176,14 @@ def pts_to_prestep(x, *args):
21762176
out : array
21772177
The x and y values converted to steps in the same order as the input;
21782178
can be unpacked as ``x_out, y1_out, ..., yp_out``. If the input is
2179-
length ``N``, each of these arrays will be length ``2N + 1``.
2179+
length ``N``, each of these arrays will be length ``2N + 1``. For
2180+
``N=0``, the length will be 0.
21802181
21812182
Examples
21822183
--------
21832184
>> x_s, y1_s, y2_s = pts_to_prestep(x, y1, y2)
21842185
"""
2185-
steps = np.zeros((1 + len(args), 2 * len(x) - 1))
2186+
steps = np.zeros((1 + len(args), max(2 * len(x) - 1, 0)))
21862187
# In all `pts_to_*step` functions, only assign *once* using `x` and `args`,
21872188
# as converting to an array may be expensive.
21882189
steps[0, 0::2] = x
@@ -2203,7 +2204,7 @@ def pts_to_poststep(x, *args):
22032204
Parameters
22042205
----------
22052206
x : array
2206-
The x location of the steps.
2207+
The x location of the steps. May be empty.
22072208
22082209
y1, ..., yp : array
22092210
y arrays to be turned into steps; all must be the same length as ``x``.
@@ -2213,13 +2214,14 @@ def pts_to_poststep(x, *args):
22132214
out : array
22142215
The x and y values converted to steps in the same order as the input;
22152216
can be unpacked as ``x_out, y1_out, ..., yp_out``. If the input is
2216-
length ``N``, each of these arrays will be length ``2N + 1``.
2217+
length ``N``, each of these arrays will be length ``2N + 1``. For
2218+
``N=0``, the length will be 0.
22172219
22182220
Examples
22192221
--------
22202222
>> x_s, y1_s, y2_s = pts_to_poststep(x, y1, y2)
22212223
"""
2222-
steps = np.zeros((1 + len(args), 2 * len(x) - 1))
2224+
steps = np.zeros((1 + len(args), max(2 * len(x) - 1, 0)))
22232225
steps[0, 0::2] = x
22242226
steps[0, 1::2] = steps[0, 2::2]
22252227
steps[1:, 0::2] = args
@@ -2238,7 +2240,7 @@ def pts_to_midstep(x, *args):
22382240
Parameters
22392241
----------
22402242
x : array
2241-
The x location of the steps.
2243+
The x location of the steps. May be empty.
22422244
22432245
y1, ..., yp : array
22442246
y arrays to be turned into steps; all must be the same length as ``x``.
@@ -2257,7 +2259,8 @@ def pts_to_midstep(x, *args):
22572259
steps = np.zeros((1 + len(args), 2 * len(x)))
22582260
x = np.asanyarray(x)
22592261
steps[0, 1:-1:2] = steps[0, 2::2] = (x[:-1] + x[1:]) / 2
2260-
steps[0, 0], steps[0, -1] = x[0], x[-1]
2262+
steps[0, :1] = x[:1] # Also works for zero-sized input.
2263+
steps[0, -1:] = x[-1:]
22612264
steps[1:, 0::2] = args
22622265
steps[1:, 1::2] = steps[1:, 0::2]
22632266
return steps

lib/matplotlib/tests/test_cbook.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,11 @@ def test_to_prestep():
391391
assert_array_equal(y1_target, y1s)
392392

393393

394+
def test_to_prestep_empty():
395+
steps = cbook.pts_to_prestep([], [])
396+
assert steps.shape == (2, 0)
397+
398+
394399
def test_to_poststep():
395400
x = np.arange(4)
396401
y1 = np.arange(4)
@@ -411,6 +416,11 @@ def test_to_poststep():
411416
assert_array_equal(y1_target, y1s)
412417

413418

419+
def test_to_poststep_empty():
420+
steps = cbook.pts_to_poststep([], [])
421+
assert steps.shape == (2, 0)
422+
423+
414424
def test_to_midstep():
415425
x = np.arange(4)
416426
y1 = np.arange(4)
@@ -431,6 +441,11 @@ def test_to_midstep():
431441
assert_array_equal(y1_target, y1s)
432442

433443

444+
def test_to_midstep_empty():
445+
steps = cbook.pts_to_midstep([], [])
446+
assert steps.shape == (2, 0)
447+
448+
434449
@pytest.mark.parametrize(
435450
"args",
436451
[(np.arange(12).reshape(3, 4), 'a'),

0 commit comments

Comments
 (0)