简体   繁体   中英

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.

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.

I would like to replace that label with the actual values (so, not percentage), like: SO4: 164, Ca: 122, Na: 158 .

Adding the text option in the trace, like:

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.

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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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