BUG: fill_between with interpolate=True and NaN. #19534
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
PR Summary
closes #18986
Fixes a bug in
fill_between
when usinginterpolate=True
with NaNs and/or masked arrays (issue #18986).Here
matplotlib/lib/matplotlib/axes/_axes.py
Lines 5269 to 5271 in 140ed40
we correctly convert the input arrays
ind
,dep1
anddep1
to masked arrays, but a few lines latermatplotlib/lib/matplotlib/axes/_axes.py
Line 5288 in 140ed40
the
np.broadcast_arrays
call converts the masked arrays back to normalndarray
s. The subsequentnp.ma.is_masked
test fails and the fill is rendered incorrectly.The fix is simple, I've added the kwarg
subok=True
to thebroadcast_arrays
call which preserves the masked arrays.I've added a new test based on the OP's example, and the existing test images for
test_axes/fill_between_interpolate
are now correct. We are unlucky not to have spotted this before but it is not surprising, there are only a few erroneous pixels on the left-hand side corresponding to the first element being masked.OP's script now produces the correct result:

PR Checklist
pytest
passes).flake8
on changed files to check).