gpt4 book ai didi

python - 绘图对象 : Show Hover information does not work

转载 作者:行者123 更新时间:2023-12-04 09:02:52 24 4
gpt4 key购买 nike

如何将“值”列中的悬停信息添加到该图中?

import plotly-graph_objects as go

fig = go.Figure()

fig.add_trace(go.Scatter(x = df.category,
y = df.type,
mode='markers',
marker={"color": df.value,
"colorscale": 'Sunsetdark',
"size": df["size"],
"showscale": True}
),
)
我的数据框如下所示:
  category          value       type          size
8 B 95890.0 A 19.171122
35 G 95890.0 B 22.312869
67 V 4113.75 C 20.188301
.
.
.
我试图传递参数 hoverinfo = df.valuego.Scatter()但这不起作用。它可以与 plotly express 一起使用,但我想使用 plotly 图形对象。错误说(无效元素是我的 df 的前 10 个):
ValueError: 
Invalid element(s) received for the 'hoverinfo' property of scatter
Invalid elements include: [95890.0, 69910.0, 4113.75, 40450.0, 77530.0, 67470.0, 97660.03, 644340.03, 79488.89, 45591.7399999998]

The 'hoverinfo' property is a flaglist and may be specified
as a string containing:
- Any combination of ['x', 'y', 'z', 'text', 'name'] joined with '+' characters
(e.g. 'x+y')
OR exactly one of ['all', 'none', 'skip'] (e.g. 'skip')
- A list or array of the above

最佳答案

您可以通过 df.valuetext ,然后设置 hoverinfo='text' .请注意,由于您设置了 mode='markers' , 绘图本身不会显示任何文本。我根据您的代码在下面包含了一个示例。

import plotly.graph_objects as go
import pandas as pd

df = pd.DataFrame({'category': ['B', 'G', 'V'],
'value': [95890.0, 95890.0 , 4113.75],
'type': ['A', 'B', 'C'],
'size': [19.171122, 22.312869, 20.188301]})

fig = go.Figure()

fig.add_trace(go.Scatter(x=df.category,
y=df.type,
text=df.value,
hoverinfo='text',
mode='markers',
marker={'color': df.value,
'colorscale': 'Sunsetdark',
'size': df.size,
'showscale': True}))

fig.show()

关于python - 绘图对象 : Show Hover information does not work,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63525452/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com