【问题标题】:Plotly scatter update a table with related data after clicking a point单击一个点后,用相关数据绘制散点图更新表
【发布时间】:2022-01-02 11:31:32
【问题描述】:

我希望能够在 Python 中单击 Plotly 散点图中的一个点后使用相关信息更新表格。或者换句话说,当我单击散点图中的一个点(更具体地说是 UMAP 图)时,表格将显示该集群(物种)的数据,我正在使用 Scikit-learn Iris 数据集进行尝试。

from umap import UMAP
import plotly.express as px
from sklearn.datasets import load_iris

df = px.data.iris()

features = df.loc[:, :'petal_width']

umap_2d = UMAP(n_components=2, init='random', random_state=0)

proj_2d = umap_2d.fit_transform(features)

fig = px.scatter(
    proj_2d, x=0, y=1,
    color=df.species, 
    labels={'color': 'species'}
)
fig.show() 

我正在创建表格,使用来自https://datahub.io/machine-learning/iris 的 iris 数据集 csv 文件:

import pandas as pd
import numpy as np
import plotly.graph_objects as go

iris_df = pd.read_csv("iris_csv.csv")
table = go.Figure(data=[go.Table(
    header=dict(values=list(iris_df.columns),
                fill_color='paleturquoise',
                align='left'),
    cells=dict(values=[iris_df.sepallength, iris_df.sepalwidth, iris_df.petallength, iris_df.petalwidth, iris_df.species],
               fill_color='lavender',
               align='left'))
])

table.show()

我的想法是使用 'on_click' 函数使用基于点的“物种”标签的过滤器来更新表格。然而,到目前为止,我所尝试的一切都失败了,这对我来说相当新。任何想法将不胜感激。

【问题讨论】:

    标签: python plotly plotly-dash plotly-python


    【解决方案1】:

    这是一种在 jupyter notebook 中使用 ipywidgets 的方法。我仍在尝试找到一种将其嵌入网页的方法...

    import plotly.graph_objs as go
    import plotly.express as px
    import umap.umap_ as umap
    from umap import UMAP
    from ipywidgets import HBox, VBox
    
    
    df = px.data.iris()
    
    
    features = df.loc[:, :'petal_width']
    
    umap = UMAP(n_components=2, init='random', random_state=0)
    
    projection = umap.fit_transform(features)
    print(projection)
    
    cs = [[0, '#EF553B'], [0.5, '#636EFA'], [1.0, '#00CC96']]
    
    f = go.FigureWidget([go.Scatter(
        x = projection[:,0],
        y = projection[:,1],
        mode = 'markers', 
        marker=dict(
            size=10,
            color=df['species_id'],
            colorscale=cs,
            line=dict(
                width=1,
                color='Black'
            )
        )
    )])
    scatter = f.data[0]
    
    
    t = go.FigureWidget([go.Table(
        header=dict(values=['sepal_length', 'sepal_width', 'petal_length', 'petal_width', 'species', 'species_id'],
                    fill = dict(color='#C2D4FF'),
                    align = ['left'] * 5),
        cells=dict(values=[df[col] for col in ['sepal_length', 'sepal_width', 'petal_length', 'petal_width', 'species', 'species_id']],
                   fill = dict(color='#F5F8FF'),
                   align = ['left'] * 5))])
    
    def selection_fn(trace,points,selector):
        t.data[0].cells.values = [df.loc[points.point_inds][col] for col in ['sepal_length', 'sepal_width', 'petal_length', 'petal_width', 'species', 'species_id']]
    
    scatter.on_selection(selection_fn)
    
    
    VBox((HBox(),f,t))
    

    有什么想法吗?

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-05-13
      • 1970-01-01
      • 1970-01-01
      • 2015-10-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多