绘图下拉列表没有根据需要更改绘图

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

我正在尝试使用plotly 绘制折线图。我需要选择等级,然后在所选等级的图上绘制两个最小值和最大值。


import plotly.express as px
import pandas as pd
import ipywidgets as widgets
from IPython.display import display

# Sample data
data = {
    'index': [1, 2, 3, 4, 5],
    'Grade': ['STEEL', 'IRON', 'CU', 'ALU', 'BRO'],
    'SIZE': [1, 2, 3, 4, 5],
    'min': [10, 20, 15, 25, 30],
    'max': [30, 40, 35, 45, 50]
}

df = pd.DataFrame(data)

# Create an initial plot for min and max for all Grades
fig = px.line(df, x='index', y=['min', 'max'], line_shape='linear',
              labels={'value': 'Values'}, title='min and max Plot for All Grades', template='plotly_white')

fig.update_layout(
    xaxis_title='Index',
    yaxis_title='Values',
    legend_title='Grade'
)

# Dropdown widget for selecting Grade
grade_dropdown = widgets.Dropdown(
    options=df['Grade'].unique().tolist(),
    value=df['Grade'].unique()[0],
    description='Select Grade:',
)

# Function to update the plot based on selected Grade
def update_plot(selected_grade):
    filtered_df = df[df['Grade'] == selected_grade]
    fig.update_traces(x=filtered_df['index'], y=[filtered_df['min'], filtered_df['max']])
    fig.update_layout(title=f'min and max Plot for Grade {selected_grade}')

# Dropdown value change event handler
def on_dropdown_change(change):
    update_plot(change['new'])

grade_dropdown.observe(on_dropdown_change, names='value')

# Display the dropdown widget and the initial plot
display(grade_dropdown)
fig.show()

输出如下

当我更改等级时,可以说“钢”,它不会改变情节。它保持静态。对于钢,它应该仅显示最小值 10 和最大值 30。任何人都可以帮助我吗?

python plot plotly visualization
1个回答
0
投票

我们可以使用

clear_output
中的
IPython.display
来清除屏幕。然后我们在清除后再次绘制下拉列表和图表。

import plotly.express as px
import pandas as pd
import ipywidgets as widgets
from IPython.display import display, clear_output


# Sample data
data = {
    'index': [1, 2, 3, 4, 5],
    'Grade': ['STEEL', 'IRON', 'CU', 'ALU', 'BRO'],
    'SIZE': [1, 2, 3, 4, 5],
    'min': [10, 20, 15, 25, 30],
    'max': [30, 40, 35, 45, 50]
}

df = pd.DataFrame(data)

# Create an initial plot for min and max for all Grades
fig = px.line(df, x='index', y=['min', 'max'], line_shape='linear',
              labels={'value': 'Values'}, title='min and max Plot for All Grades', template='plotly_white')

fig.update_layout(
    xaxis_title='Index',
    yaxis_title='Values',
    legend_title='Grade'
)

# Dropdown widget for selecting Grade
grade_dropdown = widgets.Dropdown(
    options=df['Grade'].unique().tolist(),
    value=df['Grade'].unique()[0],
    description='Select Grade:',
)

# Function to update the plot based on selected Grade
def update_plot(selected_grade):
    filtered_df = df[df['Grade'] == selected_grade]
    fig.update_traces(y=[filtered_df['min'].iloc[0], filtered_df['max'].iloc[0]])
    fig.update_layout(title=f'min and max Plot for Grade {selected_grade}')
    clear_output()
    display(grade_dropdown)
    fig.show()

# Dropdown value change event handler
def on_dropdown_change(change):
    update_plot(change['new'])

grade_dropdown.observe(on_dropdown_change, names='value')

# Display the dropdown widget and the initial plot
display(grade_dropdown)
fig.show()
© www.soinside.com 2019 - 2024. All rights reserved.