首页 > 解决方案 > Plotly 交互式条形图在显示新迹线之前不会删除先前的迹线

问题描述

我正在尝试创建一个交互式绘图条形图。

select_feat = widgets.Dropdown(options=['overall_rating', 'potential'], description='Feature')

fig_1 = go.FigureWidget()

def response_1(feature):
    x = player_info.groupby(['player_name'])[feature].mean().nlargest(5)
    val = list(x.values)
    lab = list(x.index.values)
    fig_1.add_trace(go.Bar(x=lab, y=val))
    fig_1.update_layout(title='Top five playes by average')

VBox((fig_1, interactive(response_1, feature=select_feat)))

当我执行代码时,我得到了overall_rating 的图。When chose option potential, I got this plot which has trace zero and one . 梅西、罗纳尔多和伊涅斯塔在综合评分和潜力方面均名列前茅。所以它为他们显示了两个条形图。那么我可以对只显示与选择的特征相关的跟踪做些什么。

我在 jupyter notebook 中离线使用 plotly。

标签: pythonplotly-python

解决方案


通过用 update_traces 替换 add_trace 并在 FigureWidget() 中添加 (data=[{'type':'bar'}] ,我解决了这个问题。


推荐阅读