In the tutorial there is parent-child example. but I want to achieve two way selection (assuming 2 plots plotting using different dimension of a single df). how would i achieve that?
I tried to ask different LLM without success yet.
import pandas as pd
import plotly.express as px
import panel as pn
pn.extension("plotly")
df = (
pd.read_csv("https://datasets.holoviz.org/penguins/v1/penguins.csv")
.dropna()
.reset_index(drop=True)
)
df["index"] = df.index # Used to filter rows for child view
color_map = {"Adelie": "blue", "Chinstrap": "red", "Gentoo": "green"}
fig_parent = px.scatter(
df,
x="flipper_length_mm",
y="body_mass_g",
color_discrete_map=color_map,
custom_data="index", # Used to filter rows for child view
color="species",
title="<b>Parent Plot</b>: Box or Lasso Select Points",
)
def fig_child(selectedData):
if selectedData:
indices = [point["customdata"][0] for point in selectedData["points"]]
filtered_df = df.iloc[indices]
title = f"<b>Child Plot</b>: Selected Points({len(indices)})"
else:
filtered_df = df
title = f"<b>Child Plot</b>: All Points ({len(filtered_df)})"
fig = px.scatter(
filtered_df,
x="bill_length_mm",
y="bill_depth_mm",
color_discrete_map=color_map,
color="species",
hover_data={"flipper_length_mm": True, "body_mass_g": True},
title=title,
)
return fig
parent_config = {
"modeBarButtonsToAdd": ["select2d", "lasso2d"],
"modeBarButtonsToRemove": [
"zoomIn2d",
"zoomOut2d",
"pan2d",
"zoom2d",
"autoScale2d",
],
"displayModeBar": True,
"displaylogo": False,
}
parent_pane = pn.pane.Plotly(fig_parent, config=parent_config)
ifig_child = pn.bind(fig_child, parent_pane.param.selected_data)
child_config = {
"modeBarButtonsToRemove": [
"select2d",
"lasso2d",
"zoomIn2d",
"zoomOut2d",
"pan2d",
"zoom2d",
"autoScale2d",
],
"displayModeBar": True,
"displaylogo": False,
}
child_pane = pn.pane.Plotly(ifig_child, config=child_config)
pn.FlexBox(parent_pane, child_pane)