python - 更改三元图中悬停时的标签 Plotly python

标签 python plotly

在 python 中使用plotly悬停三元图时,我无法更改标签。

这是创建绘图的代码:

import plotly
import plotly.graph_objs as go

# sample data
f1 = [31.83, 39.93, 29.15, 42.36, 432.88, 43.05, 31.32, 0.57, 424.19, 320.1, 48.16, 123.67, 176.99, 342.0, 174.84, 271.27, 115.15]
f2 = [110.8, 124.34, 132.07, 82.14, 213.04, 91.47, 133.31, 211.26, 224.33, 201.46, 59.5, 154.9, 163.59, 231.25, 123.57, 119.6, 186.35]
f3 = [59.92, 87.86, 114.43, 57.99, 364.05, 1910.65, 1196.43, 931.8, 134.58, 233.37, 80.92, 194.15, 121.22, 166.59, 142.02, 340.56, 357.92]

# trace creation
trace1 = go.Scatterternary(
a = f1,
b = f2,
c = f3,
mode='markers',
)
# plot plotting
data = [trace1]
fig = go.Figure(data=data) 
plotly.offline.plot(fig)

效果很好。但是,当悬停数据时,悬停时在每个点上绘制带有 A: 0.54、B: 0.28、C: 0.17 的标签。

我想用实际值(所以不是百分比)替换该标签,例如:SO4: 164, Ca: 122, Na: 158

在跟踪中添加text选项,例如:

trace1 = go.Scatterternary(
a = f1,
b = f2,
c = f3,
mode='markers',
text=f1
)

我得到了 SO4 的值(对于 a 轴也是如此),但我无法添加 b 轴和 c 轴的值。

最佳答案

感谢this answer in the plotly forum

解决方案是创建一个与数据长度相同的列表,并在列表的索引上循环,并使用 texthoverinfo=text 选项的组合在绘图设置中:

text = ['SO4: '+'{:d}'.format(f1[k])+'<br>Ca: '+'{:d}'.format(f2[k])+'<br>Na: '+'{:d}'.format(f3[k]) for k in range(len(f1)]

trace1 = go.Scatterternary(
a = f1,
b = f2,
c = f3,
mode='markers',
hoverinfo='text'
text=text,
)

希望这对其他人也有帮助

关于python - 更改三元图中悬停时的标签 Plotly python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44802477/

相关文章:

python - 使用plotly.express 的固定比例轴facetrow/facet_col

python - 如果存在列表元素,则搜索 CSV,然后删除

Python:四阶龙格-库塔法

r - 通过绘图下拉菜单切换显示的轨迹

python - 如何使用时间 slider 绘制 map 并使用 python 中的plotly缩放城市

python - 在 plotly 中自定义图例的顺序

python - 在pygame中创建两个运动图像

python - Pandas 截断不完整的时间序列

Python:用于绘制甘特图的模块

python - plotly 属性错误: 'Figure' object has no attribute 'show'