gpt4 book ai didi

python-3.x - Plotly 无法为多个跟踪器返回选定数据点的信息

转载 作者:行者123 更新时间:2023-12-04 15:51:12 28 4
gpt4 key购买 nike

在此example ,他们在单个 go.Scatter 跟踪器中绘制所有内容,然后他们可以使用 selection_fn 获取所选点的信息。

我想对包含 3 个集群的数据集做类似的事情。为了使集群更容易被看到,我对一个类使用一个示踪剂。因此,我尝试修改示例代码以适应我的数据集,如下所示。

import plotly.plotly as py
import plotly.graph_objs as go
from plotly.tools import set_credentials_file
import plotly.offline as py

import pandas as pd
import numpy as np
from ipywidgets import interactive, HBox, VBox

from sklearn.datasets import make_blobs

X, y = make_blobs(30,random_state=101)

py.init_notebook_mode()

f = go.FigureWidget([go.Scatter(y = X[y==0][:,1], x = X[y==0][:,0], mode = 'markers'),
go.Scatter(y = X[y==1][:,1], x = X[y==1][:,0], mode = 'markers'),
go.Scatter(y = X[y==2][:,1], x = X[y==2][:,0], mode = 'markers')])
scatter = f.data[0]
N = len(X)

# Create a table FigureWidget that updates on selection from points in the scatter plot of f
t = go.FigureWidget([go.Table(
header=dict(values=['x','y','class'],
fill = dict(color='#C2D4FF'),
align = ['left'] * 5),
cells=dict(values=[X[:,0], X[:,1], y],
fill = dict(color='#F5F8FF'),
align = ['left'] * 5))])

def selection_fn(trace,points,selector):
print(points.point_inds)
t.data[0].cells.values = [X[points.point_inds,0], X[points.point_inds,1], y[points.point_inds]]

scatter.on_selection(selection_fn)

# Put everything together
VBox((HBox(),f,t))

错误行为1:返回错误信息

当从 trace 0 中选择两个数据点时,它确实返回了 2 个信息给我,但这是错误的。

enter image description here enter image description here

错误行为2:不返回信息

从tracer 1和tracer 2中选择数据点时,它甚至不返回信息 enter image description here enter image description here

经过简短调试后,我注意到每个示踪剂的索引与完整数据集不匹配。此代码只能从跟踪器 0 返回索引,但是,当它将索引传递给完整数据集时,它会为您提供点的错误匹配信息。从tracer 1和tracer 2中选点时,连索引都不能返回,因此无法提取任何信息。

虽然我明白了这个问题,但我不知道如何修改代码,因为我还是 plotly 的新手。

最佳答案

经过几天的尝试,我想出了一个 hack 来实现它。 (也许有人还能提供更好的方法?)

技巧是为表中的每一列创建 3 个列表,并将所选点的数据追加到列表中,最后更新表。

这是完整的代码。

X, y = make_blobs(30,random_state=101)

py.init_notebook_mode()

f = go.FigureWidget([go.Scatter(y = X[y==0][:,1], x = X[y==0][:,0], text=y[y==0], mode = 'markers', name='class 0'),
go.Scatter(y = X[y==1][:,1], x = X[y==1][:,0], text=y[y==1], mode = 'markers', name='class 1'),
go.Scatter(y = X[y==2][:,1], x = X[y==2][:,0], text=y[y==2], mode = 'markers', name='class 2')])


# Create a table FigureWidget that updates on selection from points in the scatter plot of f
t = go.FigureWidget([go.Table(
header=dict(values=['x','y', 'class'],
fill = dict(color='#C2D4FF'),
align = ['left'] * 5),
cells=dict(values=[X[:,0], X[:,1], y],
fill = dict(color='#F5F8FF'),
align = ['left'] * 5))])

# def data_append(trace,points,selector):
# X1 = []
# X2 = []
# c = []


X1 = []
X2 = []
data_cluster = []
num_called = 0
def selection_fn(trace,points,selector):
global num_called
global X1, X2, data_cluster
if num_called == 3: # number of scatters
num_called = 0
X1 = []
X2 = []
data_cluster = []
X1.extend(trace['x'][points.point_inds])
X2.extend(trace['y'][points.point_inds])
data_cluster.extend(trace['text'][points.point_inds])
t.data[0].cells.values = [X1, X2,data_cluster]
num_called +=1
for scatter in f.data:
scatter.on_selection(selection_fn)

# Put everything together
VBox((HBox(),f,t))

代码输出

enter image description here enter image description here

如您所见,表格准确地返回了三个选定数据点的信息。

关于python-3.x - Plotly 无法为多个跟踪器返回选定数据点的信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53827778/

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