Skip to content

Commit 40b5664

Browse files
committed
Raise warnings and resmpale on large data to _image.resample
1 parent 3361895 commit 40b5664

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

lib/matplotlib/image.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import os
88
import logging
99
from pathlib import Path
10+
import warnings
1011

1112
import numpy as np
1213
import PIL.PngImagePlugin
@@ -166,7 +167,22 @@ def _resample(
166167
allocating the output array and fetching the relevant properties from the
167168
Image object *image_obj*.
168169
"""
169-
170+
# AGG can only handle coordinates larger than 24-bit signed integers,
171+
# so raise errors if the input data is larger than _image.resample can
172+
# handle.
173+
msg = ('Data with more than {n} cannot be accurately displayed. '
174+
'Downsampling to less than {n} before displaying. '
175+
'To remove this warning, manually downsample your data.')
176+
if data.shape[1] > 2**23:
177+
warnings.warn(msg.format(n='2**23 rows'))
178+
step = int(np.ceil(data.shape[1] / 2**23))
179+
data = data[:, ::step]
180+
transform += Affine2D().scale(step, 1)
181+
if data.shape[0] > 2**24:
182+
warnings.warn(msg.format(n='2**24 columns'))
183+
step = int(np.ceil(data.shape[0] / 2**24))
184+
data = data[::step, :]
185+
transform += Affine2D().scale(1, step)
170186
# decide if we need to apply anti-aliasing if the data is upsampled:
171187
# compare the number of displayed pixels to the number of
172188
# the data pixels.

lib/matplotlib/tests/test_image.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1383,3 +1383,22 @@ def test_rgba_antialias():
13831383
# alternating red and blue stripes become purple
13841384
axs[3].imshow(aa, interpolation='antialiased', interpolation_stage='rgba',
13851385
cmap=cmap, vmin=-1.2, vmax=1.2)
1386+
1387+
1388+
@pytest.mark.filterwarnings(r'ignore:Data with more than 2\*\*23 rows cannot be accurately displayed')
1389+
@check_figures_equal()
1390+
def test_large_image(fig_test, fig_ref):
1391+
ax_test = fig_test.subplots()
1392+
ax_ref = fig_ref.subplots()
1393+
1394+
array = np.zeros((1, 2**23 + 2))
1395+
array[:, array.size // 2:] = 1
1396+
im = ax_test.imshow(array, vmin=0, vmax=1,
1397+
aspect='auto', extent=(0, 1, 0, 1),
1398+
interpolation='none')
1399+
1400+
array = np.zeros((1, 2))
1401+
array[:, 1] = 1
1402+
im = ax_ref.imshow(array, vmin=0, vmax=1, aspect='auto',
1403+
extent=(0, 1, 0, 1),
1404+
interpolation='none')

0 commit comments

Comments
 (0)