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

Plotly带滑块图表添加标记报错:datetime.date类型不支持

解决Plotly Scatter组件x轴值为单个datetime.date的报错问题

问题出在第二行代码的x=time_qin[row]和y=q_in_cum_plot[row]——你取出的是单个datetime.date对象和单个数值,但Plotly的scatter组件要求x、y属性必须是数组类型(列表、numpy数组、pandas Series等可迭代结构),而非单个值,这才触发了ValueError。

你可以用以下两种方法快速修复:

  • 方法1:将单个值包装为列表
    把单个datetime.date和数值分别放进列表里,让Plotly识别为数组结构:

    fig.add_trace(go.Scatter(visible=False, x=[time_qin[row]], y=[q_in_cum_plot[row]], mode='markers', marker=dict(color='red')), row=3, col=1)
    
  • 方法2:用切片保留数组/Series结构
    如果time_qin是numpy数组或pandas Series,用切片语法row:row+1取出单个元素但仍保持数组/Series类型,避免返回单个对象:

    fig.add_trace(go.Scatter(visible=False, x=time_qin[row:row+1], y=q_in_cum_plot[row:row+1], mode='markers', marker=dict(color='red')), row=3, col=1)
    

注意:Plotly本身是支持datetime.date类型的,只要你传入的是数组形式而非单个值,就能正常渲染标记点。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.24 08:54:21