仪表板面板:为什么数据已更新,但绘图未更新?

Panel for dashboard: why data is updated but plot is not?

提问人:Eshmel 提问时间:11/4/2023 更新时间:11/4/2023 访问量:54

问:

我正在使用 Python 中的面板创建一个仪表板。我无法解决的一个问题是,当我移动滑块(用于模型参数)时,数据(使用 Stock2P)文件会更新,但仪表板中的绘图不会更新。

“使用 stockP1.csv”是基线数据。 有 6 种型号(型号 1 至 3_1),具有 5 个参数(回收、翻新、转售、采矿、再制造)。每个模型都会生成一个输出,该输出会稍作修改并输入到下一个模型中,直到该过程到达model3_1。

我的Python代码结构如下:

import pysd
import panel as pn
import pandas as pd
import holoviews as hv
import numpy as np
import matplotlib.pyplot as plt
import fileinput

# Load the Bokeh plotting extension
hv.extension('bokeh')

df1 = pd.read_csv('Extraction demand_shifted.csv')
# Load your models (model1, model1_1, model2, model2_1, model3, model3_1)
........
........
........

model3_1.run(
    return_columns=["Use stock"],
    return_timestamps=None,
    final_time=1008,
    output_file="Use stockP1.csv",
)

# Load the saved stock data CSV file or create an empty DataFrame
try:
    baseline_data = pd.read_csv('Use stockP1.csv')
except FileNotFoundError:
    baseline_data = pd.DataFrame(columns=['Time', 'Use stock'])

# Create a function to update the plot
def update_plot(data, title):
    if not data.empty:
        hv_plot = hv.Curve(data, 'Time', 'Use stock').opts(title=title, ylabel="Tonnage", xlabel="Month")
    else:
        hv_plot = hv.Curve([]).opts(title=title, ylabel="Tonnage", xlabel="Month")
    return pn.panel(hv_plot, name=title)

# Create sliders for common parameters
recycle_slider = pn.widgets.FloatSlider(name="refurbish_circularity_rate_", start=0, end=0.2, step= 0.01, value=0)
refurbish_slider = pn.widgets.FloatSlider(name="material_recycling_circularity_rate_", start=0, end=0.2, step= 0.01, value=0)
remanufacture_slider = pn.widgets.FloatSlider(name="remanufacture_circualrity_rate_", start=0, end=0.2, step= 0.01, value=0)
mining_slider = pn.widgets.FloatSlider(name="urban_mining_circularity_rate_", start=0, end=0.2, step= 0.01, value=0)
resale_slider = pn.widgets.FloatSlider(name="resalereuse_circualrity_rate_", start=0, end=0.2, step= 0.01, value=0.0)

# Create a Panel plot object for baseline plot
baseline_plot = update_plot(baseline_data, "Use stock (Baseline)")

# Create a Panel plot object for updated plot
updated_plot = update_plot(baseline_data, "Use stock (Updated)")

# Define a function to update models and the plot
def update_models(event):
    # Get the current slider values
    recycling = recycle_slider.value
    refurbish = refurbish_slider.value
    remanufacture = remanufacture_slider.value
    resale = resale_slider.value
    mining = mining_slider.value

    df1 = pd.read_csv('Extraction demand_shifted.csv')

    # Load your models (model1, model1_1, model2, model2_1, model3, model3_1)
..........
..........
..........

model3_1.run(
        return_columns=["Use stock"],
        return_timestamps=None,
        final_time=1008,
        output_file="Use stockP2.csv",
    )

    # Enable live updates
    pn.state.onload(pn.state.set_embedded)   
    
    # Load the saved stock data CSV file or create an empty DataFrame
    try:
        updated_data = pd.read_csv('Use stockP2.csv')
    except FileNotFoundError:
        updated_data = pd.DataFrame(columns=['Time', 'Use stock'])
    # Create a Panel plot object for updated plot
    updated_plot = update_plot(updated_data, "Use stock (Updated)")
    # Dashboard Layout
    dashboard_layout = pn.Column(
        recycle_slider,
        refurbish_slider,
        remanufacture_slider,
        resale_slider,
        mining_slider,
        #baseline_plot,  # Display baseline plot
        #updated_plot,   # Display updated plot
        width_policy="max"
    )

# Watch for slider changes and trigger the update_models function
recycle_slider.param.watch(update_models, 'value')
refurbish_slider.param.watch(update_models, 'value')
remanufacture_slider.param.watch(update_models, 'value')
mining_slider.param.watch(update_models, 'value')
resale_slider.param.watch(update_models, 'value')

# Dashboard Layout
dashboard_layout = pn.Column(
    recycle_slider,
    refurbish_slider,
    remanufacture_slider,
    resale_slider,
    mining_slider,
    baseline_plot,  # Display baseline plot
    #updated_plot,   # Display updated plot
    width_policy="max"
)

# Display the dashboard
dashboard_layout.servable()
Python Matplotlib 面板

评论


答: 暂无答案