Skip to content

Fix for scatter not showing points with valid x/y but invalid color #10809

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 17 additions & 7 deletions lib/matplotlib/axes/_axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -4073,7 +4073,7 @@ def dopatch(xs, ys, **kwargs):
label_namer="y")
def scatter(self, x, y, s=None, c=None, marker=None, cmap=None, norm=None,
vmin=None, vmax=None, alpha=None, linewidths=None,
verts=None, edgecolors=None,
verts=None, edgecolors=None, plotinvalid=False,
**kwargs):
"""
A scatter plot of *y* vs *x* with varying marker size and/or color.
Expand Down Expand Up @@ -4145,6 +4145,10 @@ def scatter(self, x, y, s=None, c=None, marker=None, cmap=None, norm=None,
For non-filled markers, the *edgecolors* kwarg is ignored and
forced to 'face' internally.

plotinvalid : boolean, optional, default: False
Set to plot valid points with invalid color, in conjunction with
`~matplotlib.colors.Colormap.set_bad`.

Returns
-------
paths : `~matplotlib.collections.PathCollection`
Expand Down Expand Up @@ -4268,11 +4272,12 @@ def scatter(self, x, y, s=None, c=None, marker=None, cmap=None, norm=None,
else:
colors = None # use cmap, norm after collection is created

# `delete_masked_points` only modifies arguments of the same length as
# `x`.
x, y, s, c, colors, edgecolors, linewidths =\
cbook.delete_masked_points(
x, y, s, c, colors, edgecolors, linewidths)
if plotinvalid is False:
# `delete_masked_points` only modifies arguments of the same length
# as `x`.
x, y, s, c, colors, edgecolors, linewidths =\
cbook.delete_masked_points(
x, y, s, c, colors, edgecolors, linewidths)

scales = s # Renamed for readability below.

Expand Down Expand Up @@ -4314,7 +4319,12 @@ def scatter(self, x, y, s=None, c=None, marker=None, cmap=None, norm=None,
if norm is not None and not isinstance(norm, mcolors.Normalize):
raise ValueError(
"'norm' must be an instance of 'mcolors.Normalize'")
collection.set_array(np.asarray(c))

if plotinvalid is False:
collection.set_array(c)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you really get away w/o the np.asarray?

Otherwise this seems to preserve the old behaviour by default and adds a useful functionality.

This will need a whats-new entry...

else:
collection.set_array(ma.masked_invalid(c))

collection.set_cmap(cmap)
collection.set_norm(norm)

Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 12 additions & 0 deletions lib/matplotlib/tests/test_axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -1668,6 +1668,18 @@ def test_hist2d_transpose():
ax.hist2d(x, y, bins=10)


@image_comparison(baseline_images=['scatter_invalid_color'], remove_text=True,
extensions=['png'])
def test_scatter_invalid_color():
fig, ax = plt.subplots()
data = {"x": [0, 1, 2, 3], "c": [1, np.nan, 9, np.nan],
"s": 1e4}
cmap = plt.get_cmap('jet', 16)
cmap.set_bad("k", 1)
ax.scatter(data["x"], data["x"], c=data["c"], s=data["s"],
cmap=cmap, plotinvalid=True)


@image_comparison(baseline_images=['scatter', 'scatter'])
def test_scatter_plot():
fig, ax = plt.subplots()
Expand Down