简体   繁体   English

在 JupyterLab 中提取 plotly.express 选择

[英]Extracting plotly.express selection in JupyterLab

I want to extract the indices or a mask from a selection made in a plotly.express figure.我想从 plotly.express 图中所做的选择中提取索引或掩码。 The figure is created in JupyterLab.该图是在 JupyterLab 中创建的。

import plotly.express as px
df = px.data.iris()
fig = px.scatter(df x="sepal_width", y="sepal_length", color="species")
fig.show()

This figure shows the untouched figure.此图显示未触及的图形。
在此处输入图像描述

This figure show a arbitrary selection.此图显示了任意选择。 From this selection, I would like to extract a list of indices or a boolean mask, or anything that will allow the selection to be extracted from the original DataFrame.从这个选择中,我想提取索引列表或 boolean 掩码,或任何允许从原始 DataFrame 中提取选择的内容。 在此处输入图像描述

There seems to be some attributes/functions that are to aid with this, such as fig.data[0].selectedpoints.似乎有一些属性/函数可以帮助解决这个问题,例如 fig.data[0].selectedpoints。 I am unable to utilize them.我无法使用它们。

plotly is version: '4.14.3' plotly 是版本:'4.14.3'

As far as I know, there is no way to get the range selected by the user.据我所知,没有办法获得用户选择的范围。 The feature you pointed out in your question, selectedpoints , is there for graphers to use to highlight specific ranges.您在问题中指出的特征selectedpoints可供绘图员用来突出显示特定范围。 It can be used as a scenario for the creator rather than a user choice.它可以作为创建者的场景而不是用户的选择。 I have customized this feature with information from this page .我已使用此页面中的信息自定义此功能。

import  plotly.graph_objects as go
import numpy as np

df = px.data.iris()

fig = go.Figure()
fig.add_trace(go.Scatter(x=df['sepal_width'],
                         y=df['sepal_length'],
                         mode='markers',
                         marker=dict(color='rgba(0, 45, 240)', size=10)))

fig.update_layout(width=600, 
              height=550, 
              autosize=False,
              xaxis=dict(zeroline=False),
              hovermode='closest')

fig.show()

在此处输入图像描述

inds = [15+k for k in range(30)]
fig.data[0].update(selectedpoints=inds,
                   selected=dict(marker=dict(color='red')),#color of selected points
                   unselected=dict(marker=dict(color='rgb(200,200, 200)',#color of unselected pts
                                   opacity=0.9)));
fig.show()

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM