简体   繁体   English

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

[英]How to show overlap points in scatter plot?

When two points have same [x,y] values, the scatter plot only show one point.当两点具有相同的 [x,y] 值时,散点图 plot 仅显示一个点。 Is there a way to display all points and their hover info even when there are overlap?即使有重叠,有没有办法显示所有点及其 hover 信息?

I can not use fig.update_layout (hovermode = 'x') because I have so many points on the same X axis我不能使用 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()

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:结果: 结果

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM