Skip to content

Commit 5059e7e

Browse files
committed
Warn via logging, add test
1 parent 1290594 commit 5059e7e

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

lib/matplotlib/image.py

+4-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
from math import ceil
1515
import os
16-
import warnings
16+
import logging
1717

1818
import numpy as np
1919

@@ -35,6 +35,8 @@
3535
from matplotlib.transforms import (Affine2D, BboxBase, Bbox, BboxTransform,
3636
IdentityTransform, TransformedBbox)
3737

38+
_log = logging.getLogger(__name__)
39+
3840
# map interpolation strings to module constants
3941
_interpd_ = {
4042
'none': _image.NEAREST, # fall back to nearest when not supported
@@ -618,7 +620,7 @@ def set_data(self, A):
618620
# making reliable interpretation impossible.
619621
high = 255 if np.issubdtype(self._A.dtype, np.integer) else 1
620622
if self._A.min() < 0 or high < self._A.max():
621-
warnings.warn(
623+
_log.warn(
622624
'Clipping input data to the valid range for imshow with '
623625
'RGB data ([0..1] for floats or [0..255] for integers).'
624626
)

lib/matplotlib/tests/test_image.py

+12-1
Original file line numberDiff line numberDiff line change
@@ -602,7 +602,7 @@ def test_minimized_rasterized():
602602
def test_load_from_url():
603603
req = six.moves.urllib.request.urlopen(
604604
"http://matplotlib.org/_static/logo_sidebar_horiz.png")
605-
Z = plt.imread(req)
605+
plt.imread(req)
606606

607607

608608
@image_comparison(baseline_images=['log_scale_image'],
@@ -795,6 +795,17 @@ def test_imshow_no_warn_invalid():
795795
assert len(warns) == 0
796796

797797

798+
@pytest.mark.parametrize('as_float', [True, False])
799+
def test_imshow_logs_when_clipping_invalid_rgb(as_float, caplog):
800+
arr = np.arange(300).reshape((10, 10, 3))
801+
if as_float:
802+
arr = arr / 255
803+
fig = plt.figure()
804+
ax = fig.add_subplot(111)
805+
ax.imshow(arr)
806+
assert 'Clipping input data to the valid range for imshow' in caplog.text
807+
808+
798809
@image_comparison(baseline_images=['imshow_flatfield'],
799810
remove_text=True, style='mpl20',
800811
extensions=['png'])

0 commit comments

Comments
 (0)