I have a pandas DataFrame with a DateTimeIndex and a numeric data column, from which I've created a line graph using px.line
. I'd now like to highlight certain points on the line, and to do this I decided to overlay a scatter plot whose rows are a subset of the rows of the initial DataFrame.
I tried something like fig.add_trace(go.Scatter(x=df.index, y=df.col))
but this makes a new line on my line graph rather than a scatter plot. I also looked for a graph_objects
function that makes a line graph but could not find it.
I've also observed that while I can make a scatter plot (that is, I see a bunch of points rather than a line) with px.scatter
, even adding the above trace to an empty figure still creates a line graph. Maybe the solution here is as simple as passing the correct arguments to go.Scatter
to get points instead of a line, but I'm not sure what that would be.
The default behavior of go.Scatter
is to add markers and lines.
To plot only the points, you can instead pass the argument mode='markers'
to go.Scatter:
fig.add_trace(go.Scatter(x=df.index, y=df.col, mode='markers'))
For example:
df = px.data.stocks()
fig = px.line(df, x='date', y="GOOG")
df_highlighted = df.iloc[50:60]
fig.add_trace(go.Scatter(x=df_highlighted['date'], y=df_highlighted['GOOG'], mode='markers'))