Skip to content

BUG: fill_between with interpolate=True and NaN. #19534

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

Merged
merged 2 commits into from
Feb 20, 2021

Conversation

ianthomas23
Copy link
Member

@ianthomas23 ianthomas23 commented Feb 17, 2021

PR Summary

closes #18986

Fixes a bug in fill_between when using interpolate=True with NaNs and/or masked arrays (issue #18986).

Here

ind, dep1, dep2 = map(
ma.masked_invalid, self._process_unit_info(
[(ind_dir, ind), (dep_dir, dep1), (dep_dir, dep2)], kwargs))

we correctly convert the input arrays ind, dep1 and dep1 to masked arrays, but a few lines later
ind, dep1, dep2 = np.broadcast_arrays(np.atleast_1d(ind), dep1, dep2)

the np.broadcast_arrays call converts the masked arrays back to normal ndarrays. The subsequent np.ma.is_masked test fails and the fill is rendered incorrectly.

The fix is simple, I've added the kwarg subok=True to the broadcast_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:
Figure_1

PR Checklist

  • Has pytest style unit tests (and pytest passes).
  • Is Flake 8 compliant (run flake8 on changed files to check).

Copy link
Member

@QuLogic QuLogic left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

However, your tests are not passing.

@ianthomas23
Copy link
Member Author

Test was failing on np 1.16 as comparisons like y2 >= y1 which include a np.nan cause an invalid value encountered in greater_equal warning to be issued, which causes the test to fail. Can't even get around it by converting to masked arrays.

I've dealt with this by wrapping the comparisons with np.errstate(invalid='ignore'). I don't particularly like it, but I can't think of a better approach.

@jklymak jklymak merged commit a510e32 into matplotlib:master Feb 20, 2021
@tacaswell tacaswell added this to the v3.5.0 milestone Feb 22, 2021
@ianthomas23 ianthomas23 deleted the 18986_fill_between_nan branch July 8, 2021 18:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

fill_between issue with interpolation & NaN
4 participants