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

时间:2017-06-28 12:29:49

标签: 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的值(所以对于一个轴),但我不能添加b和c轴的值。

1 个答案:

答案 0 :(得分:2)

感谢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,
)

希望这也有助于其他人