Home > Enterprise >  How to add datapoints to a time series line plot to highlight events?
How to add datapoints to a time series line plot to highlight events?

Time:11-10

I am trying to add tweets as dot/scatter plots onto an existing plot via plotly.

Currently, my code is as follows:

fig = px.line(total_data, 
              x = 'date', y = ['doge_close','btc_close','eth_close','shib_close'], 
              color = 'has_tweet', hover_data = ['content', 'of_interest']
              )
fig.show()

Which produces the attached image here: Plotly output

What I'm looking to do is plot tweets on the line charts themselves, but as scatter plot points rather than the has_tweet = 1 line. (added dots via photoshop) enter image description here

I can't find anything within the plotly python documentation, but have found some with R unfortunately, and fig.add_trace is fairly limited on what it can handle from inputs, so adding multiple axes doesn't help the cause.

Thank you for any help!

EDIT:

Thank you to a very kind commentor, I am almost there.

I have updated the code as such:

fig = px.line(total_data, x = total_data.date,
              y = total_data.doge_close)
fig.add_trace(
             go.Scatter(
                        x=total_data[total_data.has_tweet==1].date,
                        y=total_data[total_data.has_tweet == 1['doge_close'],
                         mode = 'markers',
                         name = 'Tweet',
                         hovertext = ['content', 'of_interest']))
fig.show()

However, hovertext is not displaying anything aside from doge_price and the date, when I'm trying to get it to display tweet content. Image with hover provided.

enter image description here

CodePudding user response:

I may be missing something here, but this should be as easy as adding a trace with fig.add_trace(go.Scatter()) with values that correspond to your original values where some criteria is satisfied like this:

fig.add_trace(go.Scatter(x=df[df.has_tweet==1].index,
                         y = df[df.has_tweet==1]['values'],
                         mode = 'markers',
                        name = 'has_tweets'))

Plot 1 - Highlighted events

enter image description here

If you'd like to add more information to your highlghted events, you can do so through the hovertemplate and text attributes of go.Scatter

Plot 2 - Highlighted events with extra info in hovertemplate

enter image description here

Complete code for Plot 1:

import plotly.express as px
import plotly.graph_objects as go
import random

df = px.data.stocks().set_index('date')[['GOOG']].rename(columns={'GOOG':'values'})
df['has_tweet'] = random.choices([0,1], weights=(90,10), k=len(df))

fig = px.line(df, x=df.index, y = 'values')
fig.add_trace(go.Scatter(x=df[df.has_tweet==1].index,
                         y = df[df.has_tweet==1]['values'],
                         mode = 'markers',
                        name = 'has_tweets'))
fig.show()

Complete code for Plot 2:

import plotly.express as px
import plotly.graph_objects as go
import random

df = px.data.stocks().set_index('date')[['GOOG']].rename(columns={'GOOG':'values'})
df['has_tweet'] = random.choices([0,1], weights=(90,10), k=len(df))
df['tweet'] = random.choices(['A','B', 'C'], weights=(50,25,25), k=len(df))
df['tweet'].iloc[4] = "All the other tweets are short, <br> but this one is long"

fig = px.line(df, x=df.index, y = 'values')
fig.add_trace(go.Scatter(x=df[df.has_tweet==1].index,
                         y = df[df.has_tweet==1]['values'],
                         mode = 'markers',
                         hovertemplate =
                                        '<i>tweet:</i>'  '<br>'  
                                        
                                        '<i>%{text}</i>',
                         text = [t for t in df['tweet']],
                         name = 'has_tweets'))
fig.show()
  • Related