创建下拉列表以根据 cable_id 更新绘图

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

我有一个具有不同值的 cable_id 列,我正在绘制在电缆上执行的几个测试的平均值。每根电缆都有一个唯一的 cable_id。我正在使用以下代码绘制图形,并且包含一个下拉菜单,我可以在其中选择 cable_id。更改 cable_id 后,图表没有更新。

以下是剧情代码:

import pandas as pd
import matplotlib.pyplot as plt
import ipywidgets as widgets
from IPython.display import display

# Define a function to update the plot based on the selected cable ID
def update_plot(cable_id):
    Tandelta_values_dataframe_single = Tandelta_values_dataframe[Tandelta_values_dataframe["cable_id"] == cable_id]
    line_nums = Tandelta_values_dataframe_single['line_nr'].unique()
    fig, ax = plt.subplots()
    ax.set_title(f"Cable ID {cable_id}")
    ax.set_xlabel("Step Voltage")
    ax.set_ylabel("Mean")
    for line_num in line_nums:
        line_df = Tandelta_values_dataframe_single.loc[Tandelta_values_dataframe_single['line_nr'] == line_num]
        ax.plot(line_df['step_voltage'], line_df['mean'], label=f"Line {line_num}")
        for index, row in line_df.iterrows():
            ax.annotate(row['step_id'], xy=(row['step_voltage'], row['mean']))
    ax.legend()
    plt.show()

# Create a dropdown widget for cable ID selection
cable_id_dropdown = widgets.Dropdown(options=Tandelta_values_dataframe['cable_id'].unique(), description='Cable ID')


# Define a function to handle changes in the dropdown selection
def on_cable_id_change(change):
    # print(change.new)
    update_plot(change.new)

# Attach the on_cable_id_change function to the dropdown widget
cable_id_dropdown.observe(on_cable_id_change, names='value')

# Display the dropdown widget and initial plot
display(cable_id_dropdown)
update_plot(Tandelta_values_dataframe['cable_id'].unique()[0])
python matplotlib ipython visualization interactive
© www.soinside.com 2019 - 2024. All rights reserved.