Don't squeeze 2-D arrays when plotting (#3008)

* Don't squeeze 2-D arrays when plotting.

Resolves #3007

* Update change log
This commit is contained in:
Sean Gillies 2024-01-16 19:02:52 -07:00 committed by GitHub
parent a5e8fd02f1
commit 34b0d1b34d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 14 additions and 2 deletions

View File

@ -31,6 +31,7 @@ New Features:
Bug fixes:
- Avoid squeezing narrow 2-D arrays to 1-D (#3008).
- Operations on closed MemoryFile and ZipMemoryFile objects now raise
ValueError as with other Python file objects (#2870, #).
- Delay clamping of I/O windows until just before GDAL methods calls to improve

View File

@ -111,9 +111,10 @@ def show(source, with_bounds=True, contour=False, contour_label_kws=None,
arr = source.read(1, masked=True)
else:
# The source is a numpy array reshape it to image if it has 3+ bands
source = np.ma.squeeze(source)
if source.ndim >= 3:
source = np.ma.squeeze(source)
if len(source.shape) >= 3:
if source.ndim >= 3:
arr = reshape_as_image(source)
else:
arr = source

View File

@ -301,3 +301,13 @@ def test_plot_normalize():
a = np.linspace(1, 6, 10)
b = adjust_band(a, 'linear')
np.testing.assert_array_almost_equal(np.linspace(0, 1, 10), b)
def test_issue3007():
"""Don't squeeze further than 2D."""
with rasterio.open('tests/data/RGB.byte.tif') as src:
arr = src.read(1)[0:1, :]
assert arr.shape == (1, 791)
show(arr)
fig = plt.gcf()
plt.close(fig)