plotly.py icon indicating copy to clipboard operation
plotly.py copied to clipboard

Incorrect fill in graph objects when connectgaps is set to false

Open NoniosTheMad opened this issue 10 months ago • 3 comments

Whenever a graph object has the following parameters: First series -> fill='tonexty', Second series -> connectgaps=False

The fill connects the first point of this series with the first point of the last segment after "gaps" from the second series, instead of its respective first value.

Here is a basic example

import plotly.graph_objects as go

fig = go.Figure()

def remove_from_list(lst, idx):
    return lst[:idx] + [None] + lst[idx+1:]

x = [i for i in range(5)]

y = [1 for _ in x]
fig.add_trace(go.Scatter(
    x=x,
    y=remove_from_list(y, 2),
    connectgaps=False
))

y = [2 for _ in x]
fig.add_trace(go.Scatter(
    x=x,
    y=y,
    fill='tonexty',
))

fig.show()

Image

Here is another example with more than 1 gap in the data:

import plotly.graph_objects as go

fig = go.Figure()

def remove_from_list(lst, idx):
    return lst[:idx] + [None] + lst[idx+1:]

x = [i for i in range(8)]

y = [1 for _ in x]
fig.add_trace(go.Scatter(
    x=x,
    y=remove_from_list(remove_from_list(y, 2), 5),
    connectgaps=False
))

y = [2 for _ in x]
fig.add_trace(go.Scatter(
    x=x,
    y=y,
    fill='tonexty',
))

fig.show()

Image

I've also attached their respective figure outuputs.

Finally, I have an additional comment regarding how areas are filled. The following shows another basic example with gaps in both series, and both with connectgaps=False.

import plotly.graph_objects as go

fig = go.Figure()

def remove_from_list(lst, idx):
    return lst[:idx] + [None] + lst[idx+1:]

x = [i for i in range(5)]

y = [1 for _ in x]
fig.add_trace(go.Scatter(
    x=x,
    y=remove_from_list(y, 2),
    connectgaps=False
))

y = [2 for _ in x]
fig.add_trace(go.Scatter(
    x=x,
    y=remove_from_list(y, 2),
    fill='tonexty',
    connectgaps=False
))

fig.show()

The connected area spans even the places where the values are undefined (during a gap). My expectation would be the opposite: Image

NoniosTheMad avatar Mar 21 '25 12:03 NoniosTheMad

Thanks for the bug report @NoniosTheMad - can you please run pip list or the equivalent and give us the versions of Python and plotly.py you're using? thanks - @gvwilson

gvwilson avatar Mar 21 '25 17:03 gvwilson

Of course:

Package Version


asttokens 3.0.0 attrs 25.3.0 colorama 0.4.6 comm 0.2.2 debugpy 1.8.13 decorator 5.2.1 executing 2.2.0 fastjsonschema 2.21.1 ipykernel 6.29.5 ipython 9.0.2 ipython_pygments_lexers 1.1.1 jedi 0.19.2 jsonschema 4.23.0 jsonschema-specifications 2024.10.1 jupyter_client 8.6.3 jupyter_core 5.7.2 matplotlib-inline 0.1.7 narwhals 1.31.0 nbformat 5.10.4 nest-asyncio 1.6.0 packaging 24.2 parso 0.8.4 pip 24.3.1 platformdirs 4.3.7 plotly 6.0.1 prompt_toolkit 3.0.50 psutil 7.0.0 pure_eval 0.2.3 Pygments 2.19.1 python-dateutil 2.9.0.post0 pywin32 310 pyzmq 26.3.0 referencing 0.36.2 rpds-py 0.23.1 six 1.17.0 stack-data 0.6.3 tornado 6.4.2 traitlets 5.14.3 typing_extensions 4.12.2 wcwidth 0.2.13

NoniosTheMad avatar Mar 21 '25 17:03 NoniosTheMad

Hey, just a follow up, since I've noticed something else happening on the same conditions:

import plotly.graph_objects as go
import math

fig = go.Figure()

def remove_from_list(lst, idx):
    return lst[:idx] + [None] + lst[idx+1:]

x = [i for i in range(10)]

y = [math.sin(v / 2) for v in x]
fig.add_trace(go.Scatter(
    x=x,
    y=remove_from_list(y, 6),
    connectgaps=False
))

y = [math.sin(v/4) + 4 for v in x]
fig.add_trace(go.Scatter(
    x=x,
    y=remove_from_list(y, 6),
    fill='tonexty',
    connectgaps=False
))

fig.show()

Image

It seems the lower graph is joined with itself. This makes me suspect that it is just choosing the wrong points to join: Instead of joining start1-start2 and end2-next_start2, it's matching the points incorrectly to start1-next_start2 and start2-end2 where 1 and 2 represents which series I'm referring to.

Regardless of the cause of the bug, I'd like to question whether this is the desired behavior. To me, it seems very unintuitive to choose connectgaps=False but see that the filled area is connecting them regardless. Of course, I realize that when the gaps between series don't match, it's not trivial how the area should behave...

NoniosTheMad avatar Mar 31 '25 13:03 NoniosTheMad