如何以legent而不是悬停显示名称? [英] How to display name in legent but not on hover?

查看:78
本文介绍了如何以legent而不是悬停显示名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用了跟踪名称,它将名称显示为图例项和悬停.我应该用什么来仅将名称显示为图例而不是悬停,拜托?谢谢

I used the trace name, which shows the names as the legend item and on hover. What should I use to display the name only as the legend but NOT on hover, please? Thanks

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()

所需的结果是没有红色文本点.

The desired result is without the red text Points.

建议后:

如何将其与 hovertemplate 结合起来?

How to combine that with 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 属性:

You need to use the hoverinfo property :

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

如果您使用的是 hovertemplate,至覆盖 hoverinfo,然后你需要清空他们所谓的辅助框";可以用标签

If you are using hovertemplate, wich overrides hoverinfo, then you'll need to empty what they call the "secondary box" which can be referenced with the tag <extra>

标签 中包含的任何内容都显示在辅助框中,例如{fullData.name}".隐藏次要完全框,使用空标签.

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>"
)

这篇关于如何以legent而不是悬停显示名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆