You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Plotly交互式图表重复显示两次的问题求助

Plotly交互式图表重复显示两次的问题求助

我在尝试用Plotly创建一个交互式图表,但它总是显示两个图表而不是一个,这可太让人困扰了😅

下面是我使用的代码,以及出现问题的结果:

# Initialize the position of the bar with the first start value
start_value_bot = plateaus_bot[0][0]  # First start value in plateaus_bot
start_value_top = plateaus_top[0][0]  # First start value in plateaus_top

# Calculate the limits for the x and y axes based on the values of X_top and X_bot
x_min = min(fit['tAtm'])
x_max = max(fit['tAtm'])
y_min = min(fit['X_bot'].min(), fit['X_top'].min())  # The minimum value between X_bot and X_top
y_max = max(fit['X_bot'].max(), fit['X_top'].max()) + 100  # The maximum value between X_bot and X_top

# Create the initial figure with FigureWidget
fig = go.FigureWidget()

# Add the X_top and X_bot curves
fig.add_trace(go.Scatter(x=fit['tAtm'], y=fit['X_top'], mode='lines', name='X top'))
fig.add_trace(go.Scatter(x=fit['tAtm'], y=fit['X_bot'], mode='lines', name='X bot'))

# Add the first vertical line initially
line1 = go.layout.Shape(
    type="line",
    x0=fit['tAtm'][start_value_bot], x1=fit['tAtm'][start_value_bot],  # Initial position at the first start value
    y0=y_min, y1=y_max,  # y-range from y_min to y_max
    line=dict(color="red", width=1)
)

# Add the second vertical line initially
line2 = go.layout.Shape(
    type="line",
    x0=fit['tAtm'][start_value_top], x1=fit['tAtm'][start_value_top],
    y0=y_min, y1=y_max,  # y-range from y_min to y_max
    line=dict(color="blue", width=1)
)

# Add the vertical lines to the figure
fig.update_layout(shapes=[line1, line2])

# Add title and axes
fig.update_layout(
    title="Select stop point for tensio top and bottom",
    xaxis=dict(range=[x_min, x_max]),  # Dynamic x-axis range
    yaxis=dict(range=[y_min, y_max]),  # Dynamic y-axis range
    showlegend=True
)

# Function to update the position of the two vertical bars
def update_line_positions(x_position1, x_position2):
    # Clear the old data to only show one graph
    fig.data = []
    # Add the X_top and X_bot curves to the figure
    fig.add_trace(go.Scatter(x=fit['tAtm'], y=fit['X_top'], mode='lines', name='X top'))
    fig.add_trace(go.Scatter(x=fit['tAtm'], y=fit['X_bot'], mode='lines', name='X bot'))
    # Update the figure with the new bar positions
    fig.update_layout(
        shapes=[
            go.layout.Shape(
                type="line",
                x0=x_position1, x1=x_position1,  # Position of the first bar modified
                y0=y_min, y1=y_max,  # y-range from y_min to y_max
                line=dict(color="blue", width=1)
            ),
            go.layout.Shape(
                type="line",
                x0=x_position2, x1=x_position2,  # Position of the second bar modified
                y0=y_min, y1=y_max,  # y-range from y_min to y_max
                line=dict(color="red", width=1)
            )
        ]
    )
    # Show the updated figure again
    fig.show()

# Create a slider to move the first bar between 1 and 7 on the x-axis
slider_x1 = widgets.FloatSlider(value=fit['tAtm'][start_value_top], min=x_min, max=x_max, step=0.01, description='Stop top tension')

# Create a second slider to move the second bar between 1 and 7 on the x-axis
slider_x2 = widgets.FloatSlider(value=fit['tAtm'][start_value_bot], min=x_min, max=x_max, step=0.01, description='Stop bottom tension',)

# Use interact to link the two sliders to the function that updates the bars' positions
interact(update_line_positions, x_position1=slider_x1, x_position2=slider_x2)

(此处为用户提供的重复显示两个图表的截图)

我知道问题应该和fig.show()函数的位置有关,但试了好几个地方都没搞清楚到底该放哪儿才对。

提前谢谢大家的帮忙啦!

备注:内容来源于stack exchange,提问作者Aurélien Lengrand

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.15 03:42:57