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

Skip to content

Commit 2058467

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 2058467

File tree

2 files changed

+15
-11
lines changed

2 files changed

+15
-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: 14 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,17 @@ def get_next_color():
26382639
"conversion": "^'c' argument must be a color", # bad vals
26392640
}
26402641

2641-
if re_key is None:
2642+
assert_context = (
2643+
pytest.raises(ValueError, match=REGEXP[re_key])
2644+
if re_key is not None
2645+
else pytest.warns(match="argument looks like a single numeric RGB")
2646+
if isinstance(c_case, list) and len(c_case) == 3
2647+
else contextlib.nullcontext()
2648+
)
2649+
with assert_context:
26422650
mpl.axes.Axes._parse_scatter_color_args(
26432651
c=c_case, edgecolors="black", kwargs={}, xsize=xsize,
26442652
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)
26502653

26512654
@mpl.style.context('default')
26522655
@check_figures_equal(extensions=["png"])
@@ -6803,9 +6806,9 @@ def test_color_length_mismatch():
68036806
fig, ax = plt.subplots()
68046807
with pytest.raises(ValueError):
68056808
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)
6809+
with pytest.warns(match="argument looks like a single numeric RGB"):
6810+
ax.scatter(x, y, c=(0.5, 0.5, 0.5))
6811+
ax.scatter(x, y, c=[(0.5, 0.5, 0.5)] * N)
68096812

68106813

68116814
def test_eventplot_legend():
@@ -7688,7 +7691,8 @@ def test_2dcolor_plot(fig_test, fig_ref):
76887691
# plot with 1D-color:
76897692
axs = fig_test.subplots(5)
76907693
axs[0].plot([1, 2], [1, 2], c=color.reshape(-1))
7691-
axs[1].scatter([1, 2], [1, 2], c=color.reshape(-1))
7694+
with pytest.warns(match="argument looks like a single numeric RGB"):
7695+
axs[1].scatter([1, 2], [1, 2], c=color.reshape(-1))
76927696
axs[2].step([1, 2], [1, 2], c=color.reshape(-1))
76937697
axs[3].hist(np.arange(10), color=color.reshape(-1))
76947698
axs[4].bar(np.arange(10), np.arange(10), color=color.reshape(-1))

0 commit comments

Comments
 (0)
0