Skip to content

fill_between issue with interpolation & NaN #18986

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

Closed
lcnittl opened this issue Nov 20, 2020 · 0 comments · Fixed by #19534
Closed

fill_between issue with interpolation & NaN #18986

lcnittl opened this issue Nov 20, 2020 · 0 comments · Fixed by #19534

Comments

@lcnittl
Copy link

lcnittl commented Nov 20, 2020

Bug report

Bug summary

When fill_between encounters NaN in the y values (x valued not tested), with interpolate=True the produces output does not look like it should.

This behavior was already mentioned in #11781, which was closed as "resolved" when using a (back then) more up to date NumPy version, however, the issue is still there (or there again).

One more note: It seems that not all NaN containing fill_between produce a faulty output.

Code for reproduction

from io import StringIO

import jupyterlab as jpl
import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd

csv_file = StringIO("""\
x,y1,y2
0,8,18
1,18,11
2,24,8
3,18,11
4,8,18
5,18,26
6,24,42
7,18,
8,8,
9,18,
""")

df = pd.read_csv(csv_file)


fig, axes = plt.subplots(2, 2, sharex="all", sharey="all", figsize=(12, 6))
fig.patch.set_facecolor("#ffffff")  

for row_idx, row in enumerate(axes):
    for col_idx, axis in enumerate(row):
        for y in ["y1", "y2"]:
            axis.plot(
                df["x"],
                df[y],
                label=r"$ " + y + r" $",
            )
            
        if row_idx == 0:
            axis.fill_between(
                df["x"],
                df["y1"],
                df["y2"],
                where=df["y1"] <= df["y2"],
                facecolor="#94C154",
                interpolate=True,
            )
            axis.set_title((axis.get_title() + " & " if axis.get_title() else "") + "'where <='")
        if col_idx == 0:
            axis.fill_between(
                df["x"],
                df["y1"],
                df["y2"],
                where=df["y1"] >= df["y2"],
                facecolor="#DD4814",
                interpolate=True,
            )
            axis.set_title((axis.get_title() + " & " if axis.get_title() else "") + "'where >='")
        if row_idx == 1 and col_idx == 1:
            axis.fill_between(
                df["x"],
                df["y1"],
                df["y2"],
                facecolor="#F6A800",
                interpolate=True,
            )
            axis.set_title("without 'where'")

        if any(axis.get_legend_handles_labels()):
            axis.legend()

fig.suptitle(
    "matplotlib: v" + mpl.__version__ + " (" + mpl.get_backend() + ")" + "\n"
    + "jupyterlab: v" + jpl.__version__ + "\n"
    + "numpy: v" + np.__version__ + "\n"
    + "pandas: v" + pd.__version__,
    #y=1.15
)

fig.tight_layout()
plt.show(fig)

Actual outcome

image

Expected outcome

No intersecting areas, with correctly fillings.

Matplotlib version

  • Operating system: Windows 10
  • Matplotlib version: 3.3.3
  • Matplotlib backend (print(matplotlib.get_backend())): module://ipykernel.pylab.backend_inline
  • Python version: 3.8.5
  • Jupyter version (if applicable): 2.2.8 (Equal output when executed from plain python script)
  • Other libraries:
    • Pandas version: 1.1.4
    • NumPy version: 1.19.3 (Not actively used in MWE)

Everything installed via pip (20.2.4).

ianthomas23 added a commit to ianthomas23/matplotlib that referenced this issue Feb 17, 2021
ianthomas23 added a commit to ianthomas23/matplotlib that referenced this issue Feb 17, 2021
ianthomas23 added a commit to ianthomas23/matplotlib that referenced this issue Feb 17, 2021
@QuLogic QuLogic added this to the v3.5.0 milestone Mar 1, 2021
MihaiAnton pushed a commit to MihaiAnton/matplotlib that referenced this issue Mar 8, 2021
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