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

Dash/Plotly:按日期切换折线颜色失效问题求助

解决Plotly折线图分段颜色不生效的问题

你的代码无法实现分段着色的核心原因是:Plotly的scatter类型trace不支持给单个折线设置多颜色列表,line.color参数仅接受单一颜色值,因此你传入的颜色列表被忽略,折线使用默认蓝色。

要实现历史数据(日期小于min_forecast_date)黑色、预测数据(日期大于等于min_forecast_date)绿色的需求,需要将两部分数据拆分为独立的trace绘制,具体修改如下:

if start_date is None or end_date is None:
    raise PreventUpdate

start_date = pd.to_datetime(start_date)
end_date = pd.to_datetime(end_date)

forecast_df['ds'] = pd.to_datetime(forecast_df['ds'])
historical_df['ds'] = pd.to_datetime(historical_df['ds'])

forecast_df['store_id'] = 'ЦБ000005'

min_forecast_date = forecast_df['ds'].min()

# 初始化figure数据列表
figure_data = []

if start_date < min_forecast_date:
    # 筛选历史数据并添加为黑色折线trace
    historical_data = historical_df[(historical_df['store_id'] == selected_store) &
                                    (historical_df['sku'] == selected_product) &
                                    (historical_df['ds'] >= start_date) &
                                    (historical_df['ds'] < min_forecast_date)]
    if not historical_data.empty:
        figure_data.append({
            'x': historical_data['ds'], 
            'y': historical_data['y'],  # 若历史数据存储实际销量,替换为对应字段,比如'y'
            'type': 'scatter', 
            'mode': 'lines', 
            'name': 'Historical Sales',
            'line': {'color': 'black'}
        })
    
    # 筛选预测数据并添加为绿色折线trace
    forecast_data = forecast_df[(forecast_df['store_id'] == selected_store) &
                                (forecast_df['sku'] == selected_product) &
                                (forecast_df['ds'] >= min_forecast_date) &
                                (forecast_df['ds'] <= end_date)]
    if not forecast_data.empty:
        figure_data.append({
            'x': forecast_data['ds'], 
            'y': forecast_data['yhat'], 
            'type': 'scatter', 
            'mode': 'lines', 
            'name': 'Sales Prediction',
            'line': {'color': 'green'}
        })
else:
    # 仅筛选预测数据并添加绿色折线trace
    forecast_data = forecast_df[(forecast_df['store_id'] == selected_store) &
                                (forecast_df['sku'] == selected_product) &
                                (forecast_df['ds'] >= start_date) &
                                (forecast_df['ds'] <= end_date)]
    if not forecast_data.empty:
        figure_data.append({
            'x': forecast_data['ds'], 
            'y': forecast_data['yhat'], 
            'type': 'scatter', 
            'mode': 'lines', 
            'name': 'Sales Prediction',
            'line': {'color': 'green'}
        })

forecast_figure = {
    'data': figure_data,
    'layout': {
        'title': f'Sales prediction for {selected_product}',
        'xaxis': {'title': 'Date'},
        'yaxis': {'title': 'Sales'}
    }
}

关键修改说明:

  • 拆分历史和预测数据为两个独立的trace,分别设置line.color为black和green
  • 添加if not ...empty判断,避免空数据导致的无效trace
  • 注意历史数据的y轴字段:如果historical_df存储的是实际销量而非预测值,需将示例中的y替换为你数据表中对应的字段名

内容的提问来源于stack exchange,提问作者Mukhammadsodik Khabibulloev

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.26 00:38:09