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

Skip to content

Commit 0fb837a

Browse files
committed
Merge branch 'colorfix-#4354' of https://github.com/QiCuiHub/matplotlib into scatter_color
2 parents a047d38 + b976201 commit 0fb837a

File tree

3 files changed

+29
-7
lines changed

3 files changed

+29
-7
lines changed

lib/matplotlib/axes/_axes.py

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4010,7 +4010,7 @@ def dopatch(xs, ys, **kwargs):
40104010
label_namer="y")
40114011
def scatter(self, x, y, s=None, c=None, marker=None, cmap=None, norm=None,
40124012
vmin=None, vmax=None, alpha=None, linewidths=None,
4013-
verts=None, edgecolors=None,
4013+
verts=None, edgecolors=None, plotinvalid=False,
40144014
**kwargs):
40154015
"""
40164016
A scatter plot of *y* vs *x* with varying marker size and/or color.
@@ -4087,6 +4087,10 @@ def scatter(self, x, y, s=None, c=None, marker=None, cmap=None, norm=None,
40874087
For non-filled markers, the *edgecolors* kwarg is ignored and
40884088
forced to 'face' internally.
40894089
4090+
plotinvalid : boolean, optional, default: False
4091+
Set to plot valid points with invalid color, in conjunction with
4092+
`~matplotlib.colors.Colormap.set_bad`.
4093+
40904094
Returns
40914095
-------
40924096
paths : `~matplotlib.collections.PathCollection`
@@ -4236,11 +4240,12 @@ def scatter(self, x, y, s=None, c=None, marker=None, cmap=None, norm=None,
42364240
else:
42374241
colors = None # use cmap, norm after collection is created
42384242

4239-
# `delete_masked_points` only modifies arguments of the same length as
4240-
# `x`.
4241-
x, y, s, c, colors, edgecolors, linewidths =\
4242-
cbook.delete_masked_points(
4243-
x, y, s, c, colors, edgecolors, linewidths)
4243+
if plotinvalid is False:
4244+
# `delete_masked_points` only modifies arguments of the same length
4245+
# as `x`.
4246+
x, y, s, c, colors, edgecolors, linewidths =\
4247+
cbook.delete_masked_points(
4248+
x, y, s, c, colors, edgecolors, linewidths)
42444249

42454250
scales = s # Renamed for readability below.
42464251

@@ -4284,7 +4289,12 @@ def scatter(self, x, y, s=None, c=None, marker=None, cmap=None, norm=None,
42844289
if norm is not None and not isinstance(norm, mcolors.Normalize):
42854290
raise ValueError(
42864291
"'norm' must be an instance of 'mcolors.Normalize'")
4287-
collection.set_array(np.asarray(c))
4292+
4293+
if plotinvalid is False:
4294+
collection.set_array(c)
4295+
else:
4296+
collection.set_array(ma.masked_invalid(c))
4297+
42884298
collection.set_cmap(cmap)
42894299
collection.set_norm(norm)
42904300

lib/matplotlib/tests/test_axes.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1685,6 +1685,18 @@ def test_hist2d_transpose():
16851685
ax.hist2d(x, y, bins=10, rasterized=True)
16861686

16871687

1688+
@image_comparison(baseline_images=['scatter_invalid_color'], remove_text=True,
1689+
extensions=['png'])
1690+
def test_scatter_invalid_color():
1691+
fig, ax = plt.subplots()
1692+
data = {"x": [0, 1, 2, 3], "c": [1, np.nan, 9, np.nan],
1693+
"s": 1e4}
1694+
cmap = plt.get_cmap('jet', 16)
1695+
cmap.set_bad("k", 1)
1696+
ax.scatter(data["x"], data["x"], c=data["c"], s=data["s"],
1697+
cmap=cmap, plotinvalid=True)
1698+
1699+
16881700
class TestScatter(object):
16891701
@image_comparison(baseline_images=['scatter', 'scatter'])
16901702
def test_scatter_plot(self):

0 commit comments

Comments
 (0)