gpt4 book ai didi

plotly on_hover function doesn't call(PLATLY ON_HOVER函数不调用)

转载 作者:bug小助手 更新时间:2023-10-25 22:34:30 27 4
gpt4 key购买 nike



I want to create 2 subplots:

我想创作两个次要情节:



  • first subplot is scatter plot.

  • and the second subplot will be regenerate every time the mouse is hover on one sample from the first sub plot.


I'm trying to run this simple code:

我试着运行这个简单的代码:


import plotly.graph_objs as go
from plotly.subplots import make_subplots
import numpy as np

# Create sample data for the main scatter plot
x_data = np.random.rand(50)
y_data = np.random.rand(50)

# Create a main scatter plot
trace_main = go.Scatter(
x=x_data,
y=y_data,
mode='markers',
text=[f'Point {i}' for i in range(50)],
hoverinfo='text'
)

# Create a subplot for the main scatter plot
fig = make_subplots(rows=2, cols=1)
fig.add_trace(trace_main, row=1, col=1)

# Create an empty scatter plot for displaying new data
trace_new = go.Scatter(
x=[],
y=[],
mode='markers',
marker=dict(size=10, color='red'),
name='New Plot'
)

# Add the empty scatter plot to the second row
fig.add_trace(trace_new, row=2, col=1)

# Define a callback function for hovering
def hover_fn(trace, points, state):

print("---> hover_fn")

if points.point_inds:
# Get the x and y coordinates of the hovered point
x_hovered = x_data[points.point_inds[0]]
y_hovered = y_data[points.point_inds[0]]

# Create new data for the lower scatter plot (customize as needed)
new_x_data = [x_hovered - 0.1, x_hovered, x_hovered + 0.1]
new_y_data = [y_hovered - 0.1, y_hovered, y_hovered - 0.1]

# Update the lower scatter plot with new data
fig.update_traces(x=new_x_data, y=new_y_data, row=2, col=1)
else:
# Clear the lower scatter plot when not hovering over a point
fig.update_traces(x=[], y=[], row=2, col=1)

# Attach the hover event callback to the main scatter plot
trace_main.on_hover(hover_fn)

# Show the figure
fig.show()

And it seems that hover_fn is never called (even the print is not called)

而且,HOVER_FN似乎从未被调用过(甚至不调用打印)


Python: 3.10
plotly==5.16.1

What do I need to change ?

我需要改变什么?


更多回答

i think on_hover only works for go.FigureWidget objects

我认为ON_HOVER只适用于GO。图Widget对象

优秀答案推荐
更多回答

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