如何在Plotly for Python中悬停时突出显示整个跟踪?

如何在Plotly for Python中悬停时突出显示整个跟踪?,python,plotly,Python,Plotly,我希望在鼠标悬停选择时高亮显示轨迹(颜色或不透明度更改)。我已经研究了restyle功能,但它可能不适合我的用例 看起来像,但我不确定是否已解决/实施 下面是我想在Plotly Python中实现的Bokeh示例: from bokeh.plotting import figure, show, output_notebook from bokeh.models import HoverTool from bokeh.models import ColumnDataSource output_

我希望在鼠标悬停选择时高亮显示轨迹(颜色或不透明度更改)。我已经研究了
restyle
功能,但它可能不适合我的用例

看起来像,但我不确定是否已解决/实施

下面是我想在Plotly Python中实现的Bokeh示例:

from bokeh.plotting import figure, show, output_notebook
from bokeh.models import HoverTool
from bokeh.models import ColumnDataSource
output_notebook()

p = figure(plot_width=400, plot_height=400,y_range=(0.2,0.5))


y_vals = [0.22,0.22,0.25,0.25,0.26,0.26,0.27,0.27]
y_vals2 = [y*1.4 for y in y_vals]
x_vals = [0,1,1,2,2,2,2,3]
data_dict = {'x':[x_vals,x_vals],
             'y':[y_vals,y_vals2],
             'color':["firebrick", "navy"],
             'alpha':[0.1, 0.1]}

source = ColumnDataSource(data_dict)

p.multi_line('x','y',source=source,
             color='color', alpha='alpha', line_width=4,
             hover_line_alpha=1.0,hover_line_color='color')

p.add_tools(HoverTool(show_arrow=True,
                      line_policy='nearest',
                      ))
show(p)

您可以使用Plotly的
FigureWidget
功能。


您是想将其用于IPython笔记本还是使用Dash?Jupyter笔记本或labIn Jupyter lab,f不会渲染,而是只返回一些文本。@IvoMerchiers:您的环境中是否正确显示了其他绘图图形?我遇到了同样的问题,复制了上面的代码,除非我将f更改为f,否则该图形将不会渲染。show(),尽管如此,on_click事件仍然不起作用。运行Jupyter笔记本,知道我做错了什么吗?@MSalty-hm,它仍在使用最新的Plotly版本(4.10.0)在Windows上运行在Python3.7 Jupyter笔记本中。有时需要一段时间来“预热”,直到听到咔嗒声。也许你可以将你的代码和设置作为新问题发布?我最终解决了这个问题,并在这里更新了我的问题:conda中没有启用扩展。
import plotly.graph_objs as go
import random

f = go.FigureWidget()
f.layout.hovermode = 'closest'
f.layout.hoverdistance = -1 #ensures no "gaps" for selecting sparse data
default_linewidth = 2
highlighted_linewidth_delta = 2

# just some traces with random data points  
num_of_traces = 5
random.seed = 42
for i in range(num_of_traces):
    y = [random.random() + i / 2 for _ in range(100)]
    trace = go.Scatter(y=y, mode='lines', line={ 'width': default_linewidth })
    f.add_trace(trace)

# our custom event handler
def update_trace(trace, points, selector):
    # this list stores the points which were clicked on
    # in all but one trace they are empty
    if len(points.point_inds) == 0:
        return
        
    for i,_ in enumerate(f.data):
        f.data[i]['line']['width'] = default_linewidth + highlighted_linewidth_delta * (i == points.trace_index)


# we need to add the on_click event to each trace separately       
for i in range( len(f.data) ):
    f.data[i].on_click(update_trace)

# let's show the figure 
f