在Python中密谋:如何在悬停时突出显示跟踪? [英] Plotly in Python: how to highlight a trace on hover?
问题描述
我有以下图形定义:
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?
推荐答案
感谢 vestland ,我想通过遵循此链接。我必须将 go.Figure
更改为 go.FigureWidget
,还必须添加显示在提供的链接中的update_trace
方法。然后,我将 on_click
回调更改为 on_hover
并成功。该解决方案并不理想,我真的不喜欢我们需要遍历每个数据点的事实,而且我不喜欢 update_trace
函数依赖 go.FigureWidget
来自外部作用域的对象,所以如果有人有更干净的方法来解决此问题,请告诉我们。
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.
这篇关于在Python中密谋:如何在悬停时突出显示跟踪?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!