I want to extract the indices or a mask from a selection made in a plotly.express figure. The figure is created in 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.
There seems to be some attributes/functions that are to aid with this, such as fig.data[0].selectedpoints. I am unable to utilize them.
plotly is version: '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. 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()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.