Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork7.9k
Description
There seems to be a discrepancy in how matplotlib'simshow
displays rasters which contain NaN areas. The NaN areas of the raster appear larger than they really are.
I assume the problem might be that the raw raster gets resampled/interpolated during display (butbefore converting to RGBA) and the NaN values propagate during that interpolation (since the ordinary mean of a list containing at least one nonfinite value is also not finite). If so, a fix would be to ensure conversion to RGBA always precedes any image resampling/rescaling.
Here is an example. The first subplot shows a float32 raster with values of zero and one. The second subplot exactly replaces the nonzero pixels with NaN, however the resulting shapes look different (making narrow non-NaN features invisible). The third subplot combines both, showing that the non-NaN area (red) iseroded, resulting in a black outline.
The expected correct behaviour would be that the red layer perfectly covers the black layer, leaving no border visible.
importmatplotlib.pyplotasplt,numpyasnpraster=np.full(shape=[200]*2,fill_value=np.nan,dtype=np.float32)# example imageraster[100:150,100:150]=0foriinrange(10):raster[20*i:20*i+i, :]=0# lines of varying thicknessplt.figure(figsize=(8,8))# small figureplt.subplot(1,3,1).imshow(np.isnan(raster).astype(np.float32))# 0 vs 1plt.subplot(1,3,2).imshow(raster)# 0 vs NaNax=plt.subplot(1,3,3)ax.imshow(np.isnan(raster),cmap='gray')# black border/underlayerax.imshow(raster,cmap='autumn')# red interior
Tested on matplotlib 3.2.1, in a jupyter 1.0.0 sandbox usingipykernel.pylab.backend_inline
backend, ipykernel 5.3.4, python 3.6.9, Linux 4.14 amzn2 x86_64.