完全无法为多个跟踪器返回所选数据点的信息 [英] Plotly failed to return information for selected data points for multiple tracers

查看:52
本文介绍了完全无法为多个跟踪器返回所选数据点的信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

In this example, they plot everything in a single go.Scatter tracer and then they can use the selection_fn to get the information for the selected points.

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

I want to do similar thing with my dataset with consists of 3 clusters. In order to make the clusters easier to be seen, I use one tracer for one class. Therefore, I try to modify the example code to adapt to my dataset as shown below.

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条信息,但这是错误的.

Wrong Behaviors 1: Wrong information returned

When selecting two data points from trace 0, it does return 2 information to me, yet it's wrong.

从跟踪器1和2选择数据点时,它甚至不返回信息

When selecting data points from tracer 1 and 2, it doesn't even return the information

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

After a brief debugging, I notices that there is mismatch in the index for each tracer and the complete dataset. This code can return the index from tracer 0 only, however, when it passes the index to the full dataset, it gives you the mis-matached information for the points. When selecting points from tracer 1 and 2, it can't even return the index, thus no information can be extracted.

尽管我了解问题所在,但由于我仍然不熟悉Plotly,所以我不知道如何修改代码.

Although I understand the problem, I don't know how to modify the code since I am still new to plotly.

推荐答案

尝试了几天之后,我想出了一种实现方法. (也许有人仍然可以提供更好的方法?)

After trying it for several days, I have figured out a hack to achieve it. (Maybe someone can still provide a better way?)

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

The trick is to create 3 lists for each of the column in the table, and the append the data of the selected points to the list, and update the table in the end.

这是完整的代码.

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))

代码输出

如您所见,该表将准确返回三个选定数据点的信息.

As you can see, the table return exactly the information for the three selected data points.

这篇关于完全无法为多个跟踪器返回所选数据点的信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆