Plotly px.line – do not connect data points

Question:

I have the below set up for a line graph (yes I know this is not yet a best practice re. data visualization…).

I don’t want the line graph to connect the gaps. I tried many things yet it continues to fill the gaps.

Any thoughts on how to solve this? I’m using plotly.

enter image description here]

fig = px.line( x = x ,
              y = y,
              color = color,
              hover_name = tooltip,
              line_shape = 'spline',
              render_mode="SVG",
              #text=df2['primary_artist_name'],
              title = 'this is a graph')

fig.update_yaxes(autorange="reversed")

fig.update_traces(connectgaps=False)

fig.show()```

update

so I switched to a scatter plot and connected the dots – after sorting the dataset correctly but I still don’t get a proper connection of the dots:

enter image description here

y = df['rank']

df['year'] = df['year'].apply(str)

color = df['year']

df.sort_values(by=['year', 'week', 'rank'], inplace=True)

# View the first 5 rows
print(df)

fig = px.scatter( x = x ,
              y = y,
              color = color,
              #hover_name = tooltip,
              #line_shape = 'spline',
              #trendline = 'ols',
              #trendline_scope='trace',
              render_mode="SVG",
              #text=df2['primary_artist_name'],
              title = 'Churn on Dutch Hip Hop tracks that landed an #1 position')

fig.update_yaxes(autorange="reversed")

fig.update_traces(connectgaps=True)

fig.update_traces(mode="lines+markers")

fig.show()```

update
enter image description here

Asked By: jsb92

||

Answers:

If fig.update_traces(connectgaps=False) is not working to remove the lines connecting the gaps in the line graph, you can try setting line_shape parameter to ‘hv’ or ‘vh’ instead of ‘spline’.

Here’s an updated code snippet with line_shape set to ‘hv’:

fig = px.line( x = x ,
          y = y,
          color = color,
          hover_name = tooltip,
          line_shape = 'hv',
          render_mode="SVG",
          #text=df2['primary_artist_name'],
          title = 'this is a graph')

fig.update_yaxes(autorange="reversed")
fig.show()

And if you still see the gaps connected in the line graph, you can try setting the mode parameter to ‘lines’ in the update_traces() method. Here’s the updated code snippet with mode set to ‘lines’:

fig = px.line( x = x ,
          y = y,
          color = color,
          hover_name = tooltip,
          line_shape = 'spline',
          render_mode="SVG",
          #text=df2['primary_artist_name'],
          title = 'this is a graph')

fig.update_yaxes(autorange="reversed")
fig.update_traces(mode='lines')
fig.show()
Answered By: Sneha s kumar
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.