Show hoverinfo over fill area of polygon in scatter trace

Similar to this question, I would like to have hoverinfo over polygons with scatter traces.

My use case is that I want to build a comet chart which I have previously done in bokeh.

The graphing library documentation says it should be possible to add hoverinfo. However, I can’t get it to work. See the simple example below.

fig = go.Figure(
    go.Scatter(
        x=[0, 1, 2, 0, None, 0, 0.5, 0, 0],
        y=[0, 2, 0, 0, None, 1, 2, 2, 1],
        fill="toself",
        hoverinfo="all",
        hovertext=["triangle 1", "triangle 2"],
        mode='line'
    )
)
fig.show()

Is this at all possible?

@dkapitan
There is a scatter attribute hoveron. Unfortunately if you are setting hoveron="fills", the hover over each triangle displays only the trace name.
Hence this option is not a solution. You can define a dummy trace of type scatter, mode=“none”, that associates a string (text) to each triangle center of mass:

import plotly.graph_objects as go
import numpy as np
#define data fo your triangles:
x = np.array([0, 1, 2, None, 0, 0.5, 0, None, 0.5, 1.5, 1])
y = np.array([0, 2., 0,  None, 1, 2, 2, None, 2.5, 2.5, 4])
#get the indices for None
I = np.where(x==None)[0]
J = np.where(y==None)[0]
X = []
Y = []
#calculate the centers of mass
for i, j in zip(I,J):
    X.append(np.mean(x[i-3:i]))
    Y.append(np.mean(y[i-3:i]))
X.append(np.mean(x[i+1:]))    
Y.append(np.mean(y[i+1:]))   

fig = go.Figure(
    go.Scatter(
        x=x,
        y=y,
        fill="toself",
        mode='none',
        hoverinfo='skip'
    )
)
fig.add_scatter(x=X, y=Y, 
                mode='none',
                showlegend=False,
                text=[f'triangle {k}' for k in range(1, len(I)+2)],
                hoverinfo='text')
1 Like

Thanks @empet, will give this a try!