Skip to content

Commit 5d3124d

Browse files
swaltektimhoffmQuLogic
authored
warning when scatter plot color settings discarded (#23516)
* Warning when scatter plot color settings discarded * Update lib/matplotlib/axes/_axes.py Co-authored-by: Tim Hoffmann <2836374+timhoffm@users.noreply.github.com> * Wrapped 23516-MS.rst lines at 80 characters * Fixed tests to look for proper warning message * Update doc/api/next_api_changes/behavior/23516-MS.rst Co-authored-by: Elliott Sales de Andrade <quantum.analyst@gmail.com> Co-authored-by: Tim Hoffmann <2836374+timhoffm@users.noreply.github.com> Co-authored-by: Elliott Sales de Andrade <quantum.analyst@gmail.com>
1 parent e4bab37 commit 5d3124d

File tree

4 files changed

+37
-2
lines changed

4 files changed

+37
-2
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
Warning when scatter plot color settings discarded
2+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
3+
When making an animation of a scatter plot, if you don't set *c* (the color
4+
value parameter) when initializing the artist, the color settings are ignored.
5+
`.Axes.scatter` now raises a warning if color-related settings are changed
6+
without setting *c*.

lib/matplotlib/axes/_axes.py

+10
Original file line numberDiff line numberDiff line change
@@ -4578,6 +4578,16 @@ def scatter(self, x, y, s=None, c=None, marker=None, cmap=None, norm=None,
45784578
collection.set_cmap(cmap)
45794579
collection.set_norm(norm)
45804580
collection._scale_norm(norm, vmin, vmax)
4581+
else:
4582+
extra_kwargs = {
4583+
'cmap': cmap, 'norm': norm, 'vmin': vmin, 'vmax': vmax
4584+
}
4585+
extra_keys = [k for k, v in extra_kwargs.items() if v is not None]
4586+
if any(extra_keys):
4587+
keys_str = ", ".join(f"'{k}'" for k in extra_keys)
4588+
_api.warn_external(
4589+
"No data for colormapping provided via 'c'. "
4590+
f"Parameters {keys_str} will be ignored")
45814591
collection._internal_update(kwargs)
45824592

45834593
# Classic mode only:

lib/matplotlib/tests/test_axes.py

+19
Original file line numberDiff line numberDiff line change
@@ -2383,6 +2383,25 @@ def test_scatter_color(self):
23832383
with pytest.raises(ValueError):
23842384
plt.scatter([1, 2, 3], [1, 2, 3], color=[1, 2, 3])
23852385

2386+
@pytest.mark.parametrize('kwargs',
2387+
[
2388+
{'cmap': 'gray'},
2389+
{'norm': mcolors.Normalize()},
2390+
{'vmin': 0},
2391+
{'vmax': 0}
2392+
])
2393+
def test_scatter_color_warning(self, kwargs):
2394+
warn_match = "No data for colormapping provided "
2395+
# Warn for cases where 'cmap', 'norm', 'vmin', 'vmax'
2396+
# kwargs are being overridden
2397+
with pytest.warns(Warning, match=warn_match):
2398+
plt.scatter([], [], **kwargs)
2399+
with pytest.warns(Warning, match=warn_match):
2400+
plt.scatter([1, 2], [3, 4], c=[], **kwargs)
2401+
# Do not warn for cases where 'c' matches 'x' and 'y'
2402+
plt.scatter([], [], c=[], **kwargs)
2403+
plt.scatter([1, 2], [3, 4], c=[4, 5], **kwargs)
2404+
23862405
def test_scatter_unfilled(self):
23872406
coll = plt.scatter([0, 1, 2], [1, 3, 2], c=['0.1', '0.3', '0.5'],
23882407
marker=mmarkers.MarkerStyle('o', fillstyle='none'),

lib/matplotlib/tests/test_colorbar.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -280,7 +280,7 @@ def test_remove_from_figure(use_gridspec):
280280
Test `remove` with the specified ``use_gridspec`` setting
281281
"""
282282
fig, ax = plt.subplots()
283-
sc = ax.scatter([1, 2], [3, 4], cmap="spring")
283+
sc = ax.scatter([1, 2], [3, 4])
284284
sc.set_array(np.array([5, 6]))
285285
pre_position = ax.get_position()
286286
cb = fig.colorbar(sc, use_gridspec=use_gridspec)
@@ -296,7 +296,7 @@ def test_remove_from_figure_cl():
296296
Test `remove` with constrained_layout
297297
"""
298298
fig, ax = plt.subplots(constrained_layout=True)
299-
sc = ax.scatter([1, 2], [3, 4], cmap="spring")
299+
sc = ax.scatter([1, 2], [3, 4])
300300
sc.set_array(np.array([5, 6]))
301301
fig.draw_without_rendering()
302302
pre_position = ax.get_position()

0 commit comments

Comments
 (0)