簡體   English   中英

更改三元圖上懸停時的標簽

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

在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軸的值相加。

感謝在密謀論壇上的回答

解決方案是創建一個與數據長度相同的列表,並在列表的索引上循環,並在繪圖設置中使用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,
)

希望這可以幫助其他人

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM