Skip to content

Commit 84f2fb7

Browse files
committed
Merge pull request matplotlib#5148 from efiring/scatter2
FIX: scatter accepts 2-D x, y, c; closes matplotlib#5141
2 parents 39f0136 + 5179da9 commit 84f2fb7

File tree

3 files changed

+23
-6
lines changed

3 files changed

+23
-6
lines changed

lib/matplotlib/axes/_axes.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3788,6 +3788,12 @@ def scatter(self, x, y, s=20, c=None, marker='o', cmap=None, norm=None,
37883788
which case all masks will be combined and only unmasked points
37893789
will be plotted.
37903790
3791+
Fundamentally, scatter works with 1-D arrays; `x`, `y`, `s`,
3792+
and `c` may be input as 2-D arrays, but within scatter
3793+
they will be flattened. The exception is `c`, which
3794+
will be flattened only if its size matches the size of `x`
3795+
and `y`.
3796+
37913797
Examples
37923798
--------
37933799
.. plot:: mpl_examples/shapes_and_collections/scatter_demo.py
@@ -3839,13 +3845,13 @@ def scatter(self, x, y, s=20, c=None, marker='o', cmap=None, norm=None,
38393845
# After this block, c_array will be None unless
38403846
# c is an array for mapping. The potential ambiguity
38413847
# with a sequence of 3 or 4 numbers is resolved in
3842-
# favor mapping, not rgb or rgba.
3848+
# favor of mapping, not rgb or rgba.
38433849
try:
38443850
c_array = np.asanyarray(c, dtype=float)
3845-
if c_array.shape == x.shape:
3851+
if c_array.size == x.size:
38463852
c = np.ma.ravel(c_array)
38473853
else:
3848-
# Wrong shape; it must not be intended for mapping.
3854+
# Wrong size; it must not be intended for mapping.
38493855
c_array = None
38503856
except ValueError:
38513857
# Failed to make a floating-point array; c must be color specs.
7.33 KB
Loading

lib/matplotlib/tests/test_axes.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1241,6 +1241,17 @@ def test_scatter_marker():
12411241
marker=mmarkers.MarkerStyle('o', fillstyle='top'))
12421242

12431243

1244+
@image_comparison(baseline_images=['scatter_2D'], remove_text=True,
1245+
extensions=['png'])
1246+
def test_scatter_2D():
1247+
x = np.arange(3)
1248+
y = np.arange(2)
1249+
x, y = np.meshgrid(x, y)
1250+
z = x + y
1251+
fig, ax = plt.subplots()
1252+
ax.scatter(x, y, c=z, s=200, edgecolors='face')
1253+
1254+
12441255
@cleanup
12451256
def test_as_mpl_axes_api():
12461257
# tests the _as_mpl_axes api
@@ -2150,17 +2161,17 @@ def test_errorbar():
21502161
def test_errorbar_shape():
21512162
fig = plt.figure()
21522163
ax = fig.gca()
2153-
2164+
21542165
x = np.arange(0.1, 4, 0.5)
21552166
y = np.exp(-x)
21562167
yerr1 = 0.1 + 0.2*np.sqrt(x)
21572168
yerr = np.vstack((yerr1, 2*yerr1)).T
21582169
xerr = 0.1 + yerr
2159-
2170+
21602171
assert_raises(ValueError, ax.errorbar, x, y, yerr=yerr, fmt='o')
21612172
assert_raises(ValueError, ax.errorbar, x, y, xerr=xerr, fmt='o')
21622173
assert_raises(ValueError, ax.errorbar, x, y, yerr=yerr, xerr=xerr, fmt='o')
2163-
2174+
21642175

21652176
@image_comparison(baseline_images=['errorbar_limits'])
21662177
def test_errorbar_limits():

0 commit comments

Comments
 (0)