1
votes

When two points have same [x,y] values, the scatter plot only show one point. Is there a way to display all points and their hover info even when there are overlap?

I can not use fig.update_layout (hovermode = 'x') because I have so many points on the same X axis

import plotly.graph_objects as go
fig = go.Figure()
fig.add_trace(go.Scatter(
x=[0, 1, 2,1,2],
y=[3, 3, 3,3,3],
mode="markers",
text = ['m1', 'm2', 'm3','m4','m5']
)
)
fig.show()
1

1 Answers

0
votes

One solution might be to remove overlapping points and concatenate their labels.

import plotly.graph_objects as go
fig = go.Figure()

x = [0, 1, 2, 1, 2]
y = [3, 3, 3, 3, 3]
text = ['m1', 'm2', 'm3','m4','m5']

i = 0
while i < len(x):
    j = i + 1
    while j < len(x):
        if x[i] == x[j] and y[i] == y[j]:
            text[i] += ', ' + text[j]
            x.pop(j)
            y.pop(j)
            text.pop(j)
        else:
            j += 1
    i += 1

fig.add_trace(go.Scatter(
x=x,
y=y,
text=text,
mode="markers",
)
)
fig.show()

Result: result