Skip to content

warning when scatter plot color settings discarded #23516

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

Merged
merged 5 commits into from
Aug 8, 2022
Merged
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
6 changes: 6 additions & 0 deletions doc/api/next_api_changes/behavior/23516-MS.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Warning when scatter plot color settings discarded
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
When making an animation of a scatter plot, if you don't set *c* (the color
value parameter) when initializing the artist, the color settings are ignored.
`.Axes.scatter` now raises a warning if color-related settings are changed
without setting *c*.
10 changes: 10 additions & 0 deletions lib/matplotlib/axes/_axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -4559,6 +4559,16 @@ def scatter(self, x, y, s=None, c=None, marker=None, cmap=None, norm=None,
collection.set_cmap(cmap)
collection.set_norm(norm)
collection._scale_norm(norm, vmin, vmax)
else:
extra_kwargs = {
'cmap': cmap, 'norm': norm, 'vmin': vmin, 'vmax': vmax
}
extra_keys = [k for k, v in extra_kwargs.items() if v is not None]
if any(extra_keys):
keys_str = ", ".join(f"'{k}'" for k in extra_keys)
_api.warn_external(
"No data for colormapping provided via 'c'. "
f"Parameters {keys_str} will be ignored")
collection._internal_update(kwargs)

# Classic mode only:
Expand Down
19 changes: 19 additions & 0 deletions lib/matplotlib/tests/test_axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -2383,6 +2383,25 @@ def test_scatter_color(self):
with pytest.raises(ValueError):
plt.scatter([1, 2, 3], [1, 2, 3], color=[1, 2, 3])

@pytest.mark.parametrize('kwargs',
[
{'cmap': 'gray'},
{'norm': mcolors.Normalize()},
{'vmin': 0},
{'vmax': 0}
])
def test_scatter_color_warning(self, kwargs):
warn_match = "No data for colormapping provided "
# Warn for cases where 'cmap', 'norm', 'vmin', 'vmax'
# kwargs are being overridden
with pytest.warns(Warning, match=warn_match):
plt.scatter([], [], **kwargs)
with pytest.warns(Warning, match=warn_match):
plt.scatter([1, 2], [3, 4], c=[], **kwargs)
# Do not warn for cases where 'c' matches 'x' and 'y'
plt.scatter([], [], c=[], **kwargs)
plt.scatter([1, 2], [3, 4], c=[4, 5], **kwargs)

def test_scatter_unfilled(self):
coll = plt.scatter([0, 1, 2], [1, 3, 2], c=['0.1', '0.3', '0.5'],
marker=mmarkers.MarkerStyle('o', fillstyle='none'),
Expand Down
4 changes: 2 additions & 2 deletions lib/matplotlib/tests/test_colorbar.py
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,7 @@ def test_remove_from_figure(use_gridspec):
Test `remove` with the specified ``use_gridspec`` setting
"""
fig, ax = plt.subplots()
sc = ax.scatter([1, 2], [3, 4], cmap="spring")
sc = ax.scatter([1, 2], [3, 4])
sc.set_array(np.array([5, 6]))
pre_position = ax.get_position()
cb = fig.colorbar(sc, use_gridspec=use_gridspec)
Expand All @@ -296,7 +296,7 @@ def test_remove_from_figure_cl():
Test `remove` with constrained_layout
"""
fig, ax = plt.subplots(constrained_layout=True)
sc = ax.scatter([1, 2], [3, 4], cmap="spring")
sc = ax.scatter([1, 2], [3, 4])
sc.set_array(np.array([5, 6]))
fig.draw_without_rendering()
pre_position = ax.get_position()
Expand Down