Prevent plotly from adding a segment connecting the last value in a series to the x-axis

Viewed 29

I am trying to create a stacked line chart where the stacked part terminates but the plot continues. I am doing this with masking the part I don't want plotted with NAs.

In the example below, the blue portion is using the stackgroup parameter while the red portion is not. The blue part is supposed to start/terminate at x=6, but plotly creates a line segment connecting it to the x-axis at x=5.

The red portion shows the desired behavior where the series simply truncates at 4 where it is supposed to.

Below is a reproducable example showing the behavior with and without stackgroup.

fig = go.Figure()

# without stackgroup
fig.add_trace(go.Scatter(
    x=[1,2,3,4,5,6,7], y=[1,2,3,2,np.nan, np.nan, np.nan],
    mode='lines',
    line=dict(width=0.5, color='red'),
    fill="tozeroy",
    name="without stackgroup",
    hovertemplate="%{y}"
))

# with stackgroup
fig.add_trace(go.Scatter(
    x=[1,2,3,4,5,6,7], y=[np.nan, np.nan, np.nan, np.nan, np.nan, 2, 2],
    mode='lines',
    line=dict(width=0.5, color='blue'),
    #fill="tozeroy",
    stackgroup="two",
    name="with stackgroup",
    hovertemplate="%{y}"
))

fig.show()

enter image description here

0 Answers
Related