FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Fix for scatter not showing points with valid x/y but invalid color by QiCuiHub · Pull Request #10809 · matplotlib/matplotlib · GitHub

Repository navigation

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .png  (1) .py  (2) All 2 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
24 changes: 17 additions & 7 deletions lib/matplotlib/axes/_axes.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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
Copy Markdown
Member

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

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.
Viewer requires iframe.
12 changes: 12 additions & 0 deletions lib/matplotlib/tests/test_axes.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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

Back | FazBrowse Home | New Git URL