8000 MNT: Issue a warning instead of logging if RGB(A) passed to scatter(..., · matplotlib/matplotlib@ae1496a · GitHub
[go: up one dir, main page]

Skip to content

Commit ae1496a

Browse files
committed
MNT: Issue a warning instead of logging if RGB(A) passed to scatter(...,
c) When to use what: - warnings.warn() in library code if the issue is avoidable and the client application should be modified to eliminate the warning - logging.warning() if there is nothing the client application can do about the situation, but the event should still be noted There are unambiguous ways for the user to specify the color (see the message). Here, the user should take action and switch from using *c* to using *color*. Thus warnings.warn() is the way to go. Closes half of #23422.
1 parent cd185ab commit ae1496a

File tree

2 files changed

+15
-8
lines changed

2 files changed

+15
-8
lines changed

lib/matplotlib/axes/_axes.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4394,7 +4394,7 @@ def invalid_shape_exception(csize, xsize):
43944394
c_is_mapped = True
43954395
else: # Wrong size; it must not be intended for mapping.
43964396
if c.shape in ((3,), (4,)):
4397-
_log.warning(
4397+
_api.warn_external(
43984398
"*c* argument looks like a single numeric RGB or "
43994399
"RGBA sequence, which should be avoided as value-"
44004400
"mapping will have precedence in case its length "

lib/matplotlib/tests/test_axes.py

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import contextlib
12
from collections import namedtuple
23
import datetime
34
from decimal import Decimal
@@ -2639,9 +2640,14 @@ def get_next_color():
26392640
}
26402641

26412642
if re_key is None:
2642-
mpl.axes.Axes._parse_scatter_color_args(
2643-
c=c_case, edgecolors="black", kwargs={}, xsize=xsize,
2644-
get_next_color_func=get_next_color)
2643+
may_warn_context = (
2644+
pytest.warns(match="argument looks like a single numeric RGB")
2645+
if isinstance(c_case, list) and len(c_case) == 3
2646+
else contextlib.nullcontext())
2647+
with may_warn_context:
2648+
mpl.axes.Axes._parse_scatter_color_args(
2649+
c=c_case, edgecolors="black", kwargs={}, xsize=xsize,
2650+
get_next_color_func=get_next_color)
26452651
else:
26462652
with pytest.raises(ValueError, match=REGEXP[re_key]):
26472653
mpl.axes.Axes._parse_scatter_color_args(
@@ -6803,9 +6809,9 @@ def test_color_length_mismatch():
68036809
fig, ax = plt.subplots()
68046810
with pytest.raises(ValueError):
68056811
ax.scatter(x, y, c=colors)
6806-
c_rgb = (0.5, 0.5, 0.5)
6807-
ax.scatter(x, y, c=c_rgb)
6808-
ax.scatter(x, y, c=[c_rgb] * N)
6812+
with pytest.warns(match="argument looks like a single numeric RGB"):
6813+
ax.scatter(x, y, c=(0.5, 0.5, 0.5))
6814+
ax.scatter(x, y, c=[(0.5, 0.5, 0.5)] * N)
68096815

68106816

68116817
def test_eventplot_legend():
@@ -7688,7 +7694,8 @@ def test_2dcolor_plot(fig_test, fig_ref):
76887694
# plot with 1D-color:
76897695
axs = fig_test.subplots(5)
76907696
axs[0].plot([1, 2], [1, 2], c=color.reshape(-1))
7691-
axs[1].scatter([1, 2], [1, 2], c=color.reshape(-1))
7697+
with pytest.warns(match="argument looks like a single numeric RGB"):
7698+
axs[1].scatter([1, 2], [1, 2], c=color.reshape(-1))
76927699
axs[2].step([1, 2], [1, 2], c=color.reshape(-1))
76937700
axs[3].hist(np.arange(10), color=color.reshape(-1))
76947701
axs[4].bar(np.arange(10), np.arange(10), color=color.reshape(-1))

0 commit comments

Comments
 (0)
0