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

Plotly express line函数不接受文档标注的markers关键字参数报错求解

问题核心原因

px.line的markers关键字参数是plotly 5.5版本才正式上线的功能,你当前使用的5.3.1版本尚未支持该参数,才会出现参数不识别的报错。

解决方法

  • 方案1:升级plotly到兼容版本
    直接执行升级命令,升级到5.5及以上版本后原有代码无需修改即可正常运行:

    pip install --upgrade plotly
    

    升级完成后可以添加以下代码确认运行环境的plotly版本是否正确,避免多虚拟环境版本混淆:

    import plotly
    print(plotly.__version__)
    
  • 方案2:兼容旧版本的修改方案,无需升级
    移除px.line中的markers=True参数,生成图表后调用update_traces手动开启标记模式,修改后的回调函数示例如下:

    @app.callback(
    Output(component_id = "graph-analysis", component_property = "figure"),
    Input(component_id = "brand-choice", component_property = "value")
    )
    def interactive_graphing(brand_value):
        dff = sales_df[sales_df.Brand == brand_value]
        # 去掉不支持的markers参数
        fig=px.line(data_frame=dff, x="Month", y="Sales", color ="Color")
        # 手动开启折线+标记模式
        fig.update_traces(mode='lines+markers')
        # 如需自定义标记的大小、颜色等样式,可追加以下配置
        # fig.update_traces(marker=dict(size=8, line=dict(width=1, color='DarkSlateGrey')))
        return fig
    

如果需要自定义不同系列的标记颜色,可以在px.line中添加color_discrete_map参数,映射Color列对应的值到你需要的色值即可,示例:

fig=px.line(data_frame=dff, x="Month", y="Sales", color="Color",
            color_discrete_map={"红色系列": "#ff0000", "蓝色系列": "#0000ff"})

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.03 09:18:03