matplotlib / matplotlib/matplotlib

[Bug]: interpolation_stage="data" removes too many pixels in the vicinity of nans in upsampled, interpolated images

Open
#29,778 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

topic: images
Dominant language
Python
Stars
23.2k
Forks
8.5k
Avg merge
1d 6h
Merged PRs (30d)
66

Description

### Bug summary

Currently, when upsampling images with interpolation_stage="data", upsampled pixels are set to nan if*any* of the underlying data points is nan. This leads to much wider "nan-propagation" than interpolation_stage="rgba".

### Code for reproduction

```Python
from pylab import *

a = tril(arange(1., 26.).reshape(5, 5))
a[a == 0] = np.nan
axs = figure(layout="constrained").subplots(2, 2)
axs[0, 0].imshow(a, interpolation_stage="data", interpolation="none")
axs[0, 0].set_title("stage=data, interp=none")
axs[0, 1].imshow(a, interpolation_stage="data", interpolation="bilinear")
axs[0, 1].set_title("stage=data, interp=bilinear")
axs[1, 0].imshow(a, interpolation_stage="rgba", interpolation="none")
axs[1, 0].set_title("stage=rgba, interp=none")
axs[1, 1].imshow(a, interpolation_stage="rgba", interpolation="bilinear")
axs[1, 1].set_title("stage=rgba, interp=bilinear")

show()
```

### Actual outcome

![Image](https://github.com/user-attachments/assets/f9e38193-c198-4c3b-93fc-fa17eb6540a8)

Note how the blank area is much wider in the bilinear, data-stage interpolation case.

### Expected outcome

Although I'm not sure the choice is objective, I think a blurred boundary (similarly to the bottom right case) would make sense.

Implementation-wise, I suspect this arises from a similar issue as https://github.com/matplotlib/matplotlib/issues/29711#issuecomment-2729139906: it should indeed be possible to interpolate in data space even with nans if we interpret the data array as a single-channel image with an additional alpha channel (0-1, depending on whether the data is nan) and correctly weighting the data by the alpha channel (similarly to the premultiplied alpha filtering suggested in the comment). Without setting a zero weight on the nans, it becomes of course impossible to upsample pixels for which any underlying data points are nan (so setting the upsampled pixel to nan is the only reasonable choice).

### Additional information

_No response_

### Operating system

_No response_

### Matplotlib Version

3.11.0.dev525+g9f7b3dd205

### Matplotlib Backend

_No response_

### Python version

3.13

### Jupyter version

_No response_

### Installation

git checkout

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the imshow path using interpolation_stage="data" and bilinear interpolation, reproducing the supplied 5x5 NaN example. Trace how NaNs are handled during upsampling and compare it with the rgba-stage result; done means data-stage interpolation no longer removes the full vicinity around NaNs and produces the expected blurred boundary.

Written by the indexing model from the issue text.

Assessment

Tech stack
matplotlib, numpy, python
Domain
data-visualization
Issue type
Bug
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.