gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 18:34:47 24 4
gpt4 key购买 nike

我希望在用鼠标悬停选中时突出显示轨迹(颜色或不透明度变化)。我研究了 restyle 功能,但它可能不适合我的用例。

enter image description here

看起来像this has been discussed on Github , 但我不确定它是否已经解决/实现。

这是我想在 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)

hover above top line hover above bottom line

最佳答案

您可以使用 Plotly 的 FigureWidget 功能。 enter image description here

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

关于python - 如何在 Plotly for Python 中悬停时突出显示整个跟踪?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53327572/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com