🌐 AI搜索 & 代理 主页
Skip to content

Commit 2fc3ed6

Browse files
committed
Improve error messages for mismatched s arg to scatter().
- Display array sizes in case of size mismatch. - Display a clearer, distinct error message when `s` is not real.
1 parent fdf8995 commit 2fc3ed6

File tree

2 files changed

+9
-9
lines changed

2 files changed

+9
-9
lines changed

lib/matplotlib/axes/_axes.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5309,12 +5309,12 @@ def scatter(self, x, y, s=None, c=None, marker=None, cmap=None, norm=None,
53095309
s = (20 if mpl.rcParams['_internal.classic_mode'] else
53105310
mpl.rcParams['lines.markersize'] ** 2.0)
53115311
s = np.ma.ravel(s)
5312-
if (len(s) not in (1, x.size) or
5313-
(not np.issubdtype(s.dtype, np.floating) and
5314-
not np.issubdtype(s.dtype, np.integer))):
5315-
raise ValueError(
5316-
"s must be a scalar, "
5317-
"or float array-like with the same size as x and y")
5312+
if not (np.issubdtype(s.dtype, np.floating)
5313+
or np.issubdtype(s.dtype, np.integer)):
5314+
raise ValueError(f"s must be float, but has type {s.dtype}")
5315+
if len(s) not in (1, x.size):
5316+
raise ValueError(f"s (size {len(s)}) cannot be broadcast "
5317+
f"to match x and y (size {len(x)})")
53185318

53195319
# get the original edgecolor the user passed before we normalize
53205320
orig_edgecolor = edgecolors

lib/matplotlib/tests/test_axes.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2958,11 +2958,11 @@ def test_scatter_unfillable(self):
29582958

29592959
def test_scatter_size_arg_size(self):
29602960
x = np.arange(4)
2961-
with pytest.raises(ValueError, match='same size as x and y'):
2961+
with pytest.raises(ValueError, match='cannot be broadcast to match x and y'):
29622962
plt.scatter(x, x, x[1:])
2963-
with pytest.raises(ValueError, match='same size as x and y'):
2963+
with pytest.raises(ValueError, match='cannot be broadcast to match x and y'):
29642964
plt.scatter(x[1:], x[1:], x)
2965-
with pytest.raises(ValueError, match='float array-like'):
2965+
with pytest.raises(ValueError, match='must be float'):
29662966
plt.scatter(x, x, 'foo')
29672967

29682968
def test_scatter_edgecolor_RGB(self):

0 commit comments

Comments
 (0)