I am trying to link a click on the plotly express tree map to a filter on the data frame. So when a user clicks on a country on the graph, the dataframe will filter for the specific country instead of showing all
There is “Update points using a click callback” for plotly.graph_objects but I am not sure how to make it work for plotly express.
import numpy as np
df = px.data.gapminder().query("year == 2007")
df["world"] = "world" # in order to have a single root node
fig = px.treemap(df, path=['world', 'continent', 'country'], values='pop',
color='lifeExp', hover_data=['iso_alpha'],
color_continuous_scale='RdBu',
color_continuous_midpoint=np.average(df['lifeExp'], weights=df['pop']))
fig.show()
## when user clicks on a country in the tree-map, the get_data function filters the df and outputs specific data on the country
def get_data(df, click):
return df[df['country'] == click]
get_data(df,click)