我正在尝试将plotly express树地图上的单击链接到数据框上的过滤器。因此,当用户单击图表上的某个国家时,数据框将过滤特定国家,而不是显示所有国家
plotly.graph_对象有“使用单击回调更新点”,但我不确定如何使其适用于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)
这里有一些技巧:
data
和layout
属性的FigureWidget
。这使我们能够添加回调李>display_id
的显示句柄dh
来更新显示的数据帧李>display(fig)
而不是fig.show()
来调用小部件上的回调李>为了便于检查,我还将
filtered_df
标记为全局,但如果您只想显示它,则不需要这样做参考:
相关问题 更多 >
编程相关推荐