如何在图例中显示名称而不是悬停?

How to display name in legent but not on hover?

我使用了跟踪名称,它将名称显示为图例项并在悬停时显示。请问我应该用什么来仅将名称显示为图例而不是悬停?谢谢

import pandas as pd
import plotly.express as px
import plotly.graph_objects as go

## sample DataFrames
df1=pd.DataFrame({'A':[1,2,3],'B':[4,5,6]})
df2=pd.DataFrame({'x':[1,2,3],'y':[7,8,9]})

fig = px.scatter(df1, x='A', y='B')
fig.add_scatter(x=df2['x'], y=df2['y'])

fig.update_traces(name='Points', showlegend = True)


fig.show()

期望的结果没有红色文本点。

咨询后:

如何将其与 hovertemplate 结合使用?

import pandas as pd
import plotly.express as px
import plotly.graph_objects as go

## sample DataFrames
df1=pd.DataFrame({'A':[1,2,3],'B':[4,5,6]})
df2=pd.DataFrame({'x':[1,2,3],'y':[7,8,9]})

fig = px.scatter(df1, x='A', y='B')
fig.add_scatter(x=df2['x'], y=df2['y'])

fig.update_traces(
    hovertemplate="<br>".join([
        "<b><i>T</i> (R):</b>            %{x:.3f} ± %{customdata[2]:.3f}",
     ])
    )

fig.update_traces(name='Points', showlegend = True, hoverinfo="x+y")


fig.show()

您需要使用 hoverinfo 属性 :

fig.update_traces(name='Points', showlegend = True, hoverinfo="x+y")

如果您使用 hovertemplate,它会覆盖 hoverinfo,那么您需要清空他们所谓的“辅助框”,它可以用标签 <extra>

引用

Anything contained in tag <extra> is displayed in the secondary box, for example "{fullData.name}". To hide the secondary box completely, use an empty tag <extra></extra>.

例如:

fig.update_traces(
  hovertemplate="[...] %{x:.3f} ± %{customdata[2]:.3f}<extra></extra>"
)