简体   繁体   中英

Plotly in Python: how to highlight a trace on hover?

I have the following figure definition:

import plotly.graph_objects as go
fig = go.Figure()
fig.add_trace(go.Scatter(x=[1,2,3], y=[1,2,3], name="A"))
fig.add_trace(go.Scatter(x=[1,2,3], y=[1,3,5], name="B"))
fig.show()

在此处输入图片说明

I would like to highlight a trace upon hover , which would reduce the opacity of all other traces and make the selected one bold preferably.

Is there a way to achieve this?

Thanks to vestland , I think I managed to get it working by following this link . I had to change go.Figure to go.FigureWidget and I also had to add a update_trace method that appears in the provided link. Then I changed on_click callback to on_hover and it worked. The solution isn't ideal, I don't really like the fact that we need to loop over each data point and I don't like that the update_trace function relies on go.FigureWidget object from outer scope, so if anyone has a cleaner way to solve this, please let us know.

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