Skip to content

Commit b0bc62e

Browse files
authored
Merge pull request matplotlib#18947 from aitikgupta/streamplot-grid
MNT: Strictly increasing check with test coverage for streamplot grid
2 parents 702cdbc + 7af5e2c commit b0bc62e

File tree

2 files changed

+54
-2
lines changed

2 files changed

+54
-2
lines changed

lib/matplotlib/streamplot.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ def streamplot(axes, x, y, u, v, density=1, linewidth=None, color=None,
2525
2626
Parameters
2727
----------
28-
x, y : 1D arrays
29-
An evenly spaced grid.
28+
x, y : 1D/2D arrays
29+
Evenly spaced strictly increasing arrays to make a grid.
3030
u, v : 2D arrays
3131
*x* and *y*-velocities. The number of rows and columns must match
3232
the length of *y* and *x*, respectively.
@@ -333,6 +333,11 @@ def __init__(self, x, y):
333333
else:
334334
raise ValueError("'y' can have at maximum 2 dimensions")
335335

336+
if not (np.diff(x) > 0).all():
337+
raise ValueError("'x' must be strictly increasing")
338+
if not (np.diff(y) > 0).all():
339+
raise ValueError("'y' must be strictly increasing")
340+
336341
self.nx = len(x)
337342
self.ny = len(y)
338343

lib/matplotlib/tests/test_streamplot.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import numpy as np
44
from numpy.testing import assert_array_almost_equal
5+
import pytest
56
import matplotlib.pyplot as plt
67
from matplotlib.testing.decorators import image_comparison
78
import matplotlib.transforms as mtransforms
@@ -114,3 +115,49 @@ def test_streamplot_limits():
114115
# datalim.
115116
assert_array_almost_equal(ax.dataLim.bounds, (20, 30, 15, 6),
116117
decimal=1)
118+
119+
120+
def test_streamplot_grid():
121+
u = np.ones((2, 2))
122+
v = np.zeros((2, 2))
123+
124+
# Test for same rows and columns
125+
x = np.array([[10, 20], [10, 30]])
126+
y = np.array([[10, 10], [20, 20]])
127+
128+
with pytest.raises(ValueError, match="The rows of 'x' must be equal"):
129+
plt.streamplot(x, y, u, v)
130+
131+
x = np.array([[10, 20], [10, 20]])
132+
y = np.array([[10, 10], [20, 30]])
133+
134+
with pytest.raises(ValueError, match="The columns of 'y' must be equal"):
135+
plt.streamplot(x, y, u, v)
136+
137+
x = np.array([[10, 20], [10, 20]])
138+
y = np.array([[10, 10], [20, 20]])
139+
plt.streamplot(x, y, u, v)
140+
141+
# Test for maximum dimensions
142+
x = np.array([0, 10])
143+
y = np.array([[[0, 10]]])
144+
145+
with pytest.raises(ValueError, match="'y' can have at maximum "
146+
"2 dimensions"):
147+
plt.streamplot(x, y, u, v)
148+
149+
# Test for equal spacing
150+
u = np.ones((3, 3))
151+
v = np.zeros((3, 3))
152+
x = np.array([0, 10, 20])
153+
y = np.array([0, 10, 30])
154+
155+
with pytest.raises(ValueError, match="'y' values must be equally spaced"):
156+
plt.streamplot(x, y, u, v)
157+
158+
# Test for strictly increasing
159+
x = np.array([0, 20, 40])
160+
y = np.array([0, 20, 10])
161+
162+
with pytest.raises(ValueError, match="'y' must be strictly increasing"):
163+
plt.streamplot(x, y, u, v)

0 commit comments

Comments
 (0)