绘图下拉选择不更新绘图

问题描述 投票:0回答:0

我使用下拉菜单选择一列并根据所选列更新绘图。这是我做同样事情的代码。

def update_plot(column):
   value_counts = df[column].value_counts()
   fig = px.bar(x=value_counts.index, y=value_counts.values, labels={'x': column, 'y': 'Count'},
                title=f'Value Counts of {column}')
   fig.show()

column_dropdown = widgets.Dropdown(options=df[object_columns].columns, description='Select Column:')
# Display the widget
display(column_dropdown)

# Create the initial plot based on the first column in the DataFrame
initial_column = df[object_columns].columns[0]
update_plot(initial_column)

# Set up the callback to update the plot when the dropdown selection changes
column_dropdown.observe(lambda change: update_plot(change.new), names='value')

此代码仅显示第一列的图。当我使用下拉菜单更改列时,绘图保持不变。我在网上寻找解决方案,但似乎没有任何效果。有人可以帮我解决这个问题吗?

python matplotlib jupyter-notebook plotly visualization
© www.soinside.com 2019 - 2024. All rights reserved.