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.
]
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:
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()```
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()
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.
]
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:
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()```
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()