如何在散点图中显示重叠点?

问题描述 投票:0回答:1

[当两个点具有相同的[x,y]值时,散点图仅显示一个点。即使重叠,有没有办法显示所有点及其悬停信息?

我不能使用fig.update_layout(hovermode ='x'),因为我在同一X轴上有很多点

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()
python plotly
1个回答
0
投票

一种解决方案可能是删除重叠点并连接其标签。

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

© www.soinside.com 2019 - 2024. All rights reserved.