Hiding stacked scatter plot lines when value is zero

Hi,
I’m using plotly in python to produce graphs illustrating energy systems.
I am adding individual data series (e.g. one for solar power) using

fig.add_trace(go.Scatter(x=x_solar, y=y_solar, name='solar', line=dict(width=2.0, color='yellow'), stackgroup='generators')

My problem is that data is plotted even when the series y-value is 0, with the line then occluding lines from other (non-zero) data sets stacked below. In the example below, the yellow solar line is shown even at night (solar is zero), occluding the line for the dark green line for the wind power data set.
In some places this is only a cosmetic problem, in others it makes the plot hard to interpret.
Changing the order in which traces are added is not a solution.

I tried filtering x_solar and y_solar so they only contain values for times when y_solar > 0 but that didn’t do the trick.
Removing the lines altogether by setting line=dict(width=0.0, color=… works around the issue in cases where the data series can represented by the fill area alone but doesn’t work in cases where I want/need to show the line.

Is there a way to basically tell plotly: “If the y value is zero then just don’t plot anything for this x value”?

Thank you very much for any tips & insights!

HI @tobi_kellner I don’t think there is a builtin function to do this. If you are able to modify the data before plotting the chart, you could set up a filter for the y values and set the corresponding x- and y- values to nan. An example:

import plotly.graph_objs as go
import numpy as np

# generate continous data
x = np.arange(0, 8*np.pi, 0.1)
y = np.sin(x)

# set upper and lower bound for filter
upper = -0.1
lower = -0.8

# generate filtered data
u = []
v = []
for xx, yy in zip(x,y):
    if lower < yy < upper:
        u.append(float('nan'))
        v.append(float('nan'))
    else:
        u.append(xx)
        v.append(yy)


# create figure
fig = go.Figure()

# add continous trace
fig.add_trace(
    go.Scatter(
        x=x, 
        y=y, 
        mode='lines', 
        line={'color': 'blue', 'width': 12},
        name='continous',
        showlegend=True
    )
)

# add filtered trace
fig.add_trace(
    go.Scatter(
        x=u, 
        y=v, 
        mode='lines', 
        line={'color': 'darkgray', 'width': 3},
        name='filtered',
        showlegend=True,
        connectgaps=False
        
    )
)

fig.show()

creates:

@AIMPED aah, that’s a creative solution! Will try it. Thanks!

Hmm, sadly the solution proposed by @AIMPED above doesn’t seem to work for stacked graphs. Adding a simple stackgroup='a', to the code, so

import plotly.graph_objs as go
import numpy as np

# generate continous data
x = np.arange(0, 8*np.pi, 0.1)
y = np.sin(x)

# set upper and lower bound for filter
upper = -0.1
lower = -0.8

# generate filtered data
u = []
v = []
for xx, yy in zip(x,y):
    if lower < yy < upper:
        u.append(float('nan'))
        v.append(float('nan'))
    else:
        u.append(xx)
        v.append(yy)


# create figure
fig = go.Figure()

# add continous trace
fig.add_trace(
    go.Scatter(
        x=x, 
        y=y, 
        mode='lines', 
        line={'color': 'blue', 'width': 12},
        name='continous',
        stackgroup='a',
        showlegend=True,
        connectgaps=False
    )
)

# add filtered trace
fig.add_trace(
    go.Scatter(
        x=u, 
        y=v, 
        mode='lines', 
        line={'color': 'darkgray', 'width': 3},
        name='filtered',
        stackgroup='a',
        showlegend=True,
        connectgaps=False
        
    )
)

fig.show()

Leads to the filtered lines being drawn again:

Pity, it seemed like a nice solution!

Thanks,
Tobi