Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Should imshow() recognise 0-255 images? #1880

Closed
fmaussion opened this issue Feb 2, 2018 · 1 comment · Fixed by #1893
Closed

Should imshow() recognise 0-255 images? #1880

fmaussion opened this issue Feb 2, 2018 · 1 comment · Fixed by #1893

Comments

@fmaussion
Copy link
Member

Code Sample, a copy-pastable example if possible

import os
import urllib.request
import xarray as xr
import matplotlib.pyplot as plt

# Download the file from rasterio's repository
url = 'https://github.com/mapbox/rasterio/raw/master/tests/data/RGB.byte.tif'
urllib.request.urlretrieve(url, 'RGB.byte.tif')

# Read the data
da = xr.open_rasterio('RGB.byte.tif')

f, (ax1, ax2) = plt.subplots(1, 2, figsize=(9, 4))
da.plot.imshow(ax=ax1)
(da / 255).plot.imshow(ax=ax2)
plt.tight_layout()
plt.show()

# Delete the file
os.remove('RGB.byte.tif')

figure_1

Problem description

In #1796, @Zac-HD added support for RGBA images. If an alpha channel is not found, it is added (code)

The problem is that adding this alpha channel requires the images to be normalized to 0-1, while plotting an image in 0-255 range without alpha channel works fine in matplotlib. Removing https://github.com/pydata/xarray/blob/master/xarray/plot/plot.py#L708-L715 would solve the problem, but I guess it was added for a reason.

@Zac-HD , thoughts?

@Zac-HD
Copy link
Contributor

Zac-HD commented Feb 4, 2018

Ouch, this is definitely a bug in the code I wrote - that's terrible 😭

Probably related to matplotlib/matplotlib/pull/10220, where I'm looking at clipping instead of wrapping colours from the other side. (not as in code overlap or causing a bug, but dealing with similar logic)

Fixing this issue may be as simple as setting alpha to 255 instead of 1 for integer dtypes, or it might be more involved. Either way I'll try to open a pull within the next few days.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants