简体   繁体   English

更改三元图上悬停时的标签

[英]Change label on hover in ternary plots Plotly python

I'm not able to change the labels when hovering a ternary plot with plotly in python. 在python中使用plotly悬停三元图时,我无法更改标签。

This the code that creates the plot: 这是创建绘图的代码:

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)

it works nice. 它很好用。 But when hovering the data, on each point plotted the label with A: 0.54, B: 0.28, C: 0.17 when hovering. 但是,将数据悬停时,在每个点上悬停时在标签上标出的标签分别为A: 0.54, B: 0.28, C: 0.17

I would like to replace that label with the actual values (so, not percentage), like: SO4: 164, Ca: 122, Na: 158 . 我想用实际值(而不是百分比)替换该标签,例如: SO4: 164, Ca: 122, Na: 158

Adding the text option in the trace, like: 在跟踪中添加text选项,例如:

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

I get get the value for SO4 (so for a axis) but I cannot add the values of b and c axis. 我得到了SO4的值(因此对于一个轴),但是我无法将b和c轴的值相加。

Thanks to this answer in the plotly forum . 感谢在密谋论坛上的回答

The solution is to create a list with the same length as the data and looping on the index of the lists and use the combination of text and hoverinfo=text options in the plot settings: 解决方案是创建一个与数据长度相同的列表,并在列表的索引上循环,并在绘图设置中使用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,
)

Hope this can help also somebody else 希望这可以帮助其他人

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

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