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

Skip to content

Commit 62c3ad4

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 62c3ad4

File tree

2 files changed

+16
-11
lines changed

2 files changed

+16
-11
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: 15 additions & 10 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
@@ -2638,15 +2639,18 @@ def get_next_color():
26382639
"conversion": "^'c' argument must be a color", # bad vals
26392640
}
26402641

2641-
if re_key is None:
2642+
2643+
assert_context = (
2644+
pytest.raises(ValueError, match=REGEXP[re_key])
2645+
if re_key is not None
2646+
else pytest.warns(match="argument looks like a single numeric RGB")
2647+
if isinstance(c_case, list) and len(c_case) == 3
2648+
else contextlib.nullcontext()
2649+
)
2650+
with assert_context:
26422651
mpl.axes.Axes._parse_scatter_color_args(
26432652
c=c_case, edgecolors="black", kwargs={}, xsize=xsize,
26442653
get_next_color_func=get_next_color)
2645-
else:
2646-
with pytest.raises(ValueError, match=REGEXP[re_key]):
2647-
mpl.axes.Axes._parse_scatter_color_args(
2648-
c=c_case, edgecolors="black", kwargs={}, xsize=xsize,
2649-
get_next_color_func=get_next_color)
26502654

26512655
@mpl.style.context('default')
26522656
@check_figures_equal(extensions=["png"])
@@ -6803,9 +6807,9 @@ def test_color_length_mismatch():
68036807
fig, ax = plt.subplots()
68046808
with pytest.raises(ValueError):
68056809
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)
6810+
with pytest.warns(match="argument looks like a single numeric RGB"):
6811+
ax.scatter(x, y, c=(0.5, 0.5, 0.5))
6812+
ax.scatter(x, y, c=[(0.5, 0.5, 0.5)] * N)
68096813

68106814

68116815
def test_eventplot_legend():
@@ -7688,7 +7692,8 @@ def test_2dcolor_plot(fig_test, fig_ref):
76887692
# plot with 1D-color:
76897693
axs = fig_test.subplots(5)
76907694
axs[0].plot([1, 2], [1, 2], c=color.reshape(-1))
7691-
axs[1].scatter([1, 2], [1, 2], c=color.reshape(-1))
7695+
with pytest.warns(match="argument looks like a single numeric RGB"):
7696+
axs[1].scatter([1, 2], [1, 2], c=color.reshape(-1))
76927697
axs[2].step([1, 2], [1, 2], c=color.reshape(-1))
76937698
axs[3].hist(np.arange(10), color=color.reshape(-1))
76947699
axs[4].bar(np.arange(10), np.arange(10), color=color.reshape(-1))

0 commit comments

Comments
 (0)
0