单击python绘图中的事件Dash Scatterpolar绘图

2024-09-28 03:17:32 发布

您现在位置:Python中文网/ 问答频道 /正文

我想创建两个包含iframed plotly dash图的网页。我想实现第一个图形的单击事件,以重定向到另一个页面(提供有关该特定数据的更多信息)

我遵循了this指南,但在我的设置中不会触发单击事件,即使我在运行它的python版本中安装了IPyWidget、node.js和plotlywidgets扩展。上面的例子中有一个注释:

Note: Callbacks will only be triggered when the trace belongs to a instance of plotly.graph_objs.FigureWidget and it is displayed in an ipywidget context. Callbacks will not be triggered on figures that are displayed using plot/iplot.

这个例子使用了FigureWidget,我通过将图传递给dcc.Graph来绘制它,这个图应该与fig.show()相同。整个代码如下所示

import plotly.graph_objects as go
import numpy as np
np.random.seed(1)

x = np.random.rand(100)
y = np.random.rand(100)

fig = go.FigureWidget([go.Scatter(x=x, y=y, mode='markers')])

scatter = fig.data[0]
colors = ['#a3a7e4'] * 100
scatter.marker.color = colors
scatter.marker.size = [10] * 100
fig.layout.hovermode = 'closest'


# create our callback function
def update_point(trace, points, selector):
    c = list(scatter.marker.color)
    s = list(scatter.marker.size)
    for i in points.point_inds:
        c[i] = '#bae2be'
        s[i] = 20
        with fig.batch_update():
            scatter.marker.color = c
            scatter.marker.size = s


scatter.on_click(update_point)

import dash
import dash_core_components as dcc
import dash_html_components as html

app = dash.Dash()
app.layout = html.Div([
    dcc.Graph(figure=fig)
]) 

app.run_server(debug=True, use_reloader=False)

但我不知道如何确保它显示在ipywidget上下文中

我知道有一种类似于this的解决方法,它带有layout.clickmode = select和一个单独的按钮,但我希望使图形的on_click()事件能够工作

有人能帮我做这件事吗


Tags: importgoonasnpfig事件random
1条回答
网友
1楼 · 发布于 2024-09-28 03:17:32

关于文件的这一部分:

in an ipywidget context

这意味着这将只适用于jupyter笔记本

如果您希望在使用dash时具有这种图形交互,那么应该使用dcc.Graph属性来触发回调(hoverDataclickDataselectedDatarelayoutData

更多信息请参见:Interactive Visualizations

相关问题 更多 >

    热门问题