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

在Streamlit中展示Plotly图表时出现'Figure' object is not callable错误

问题分析与解决:'Figure' object is not callable 错误

错误原因

这个错误的核心是变量名冲突,导致调用st.plotly_chart()时,实际是在尝试调用一个Plotly Figure对象(而非Streamlit的plotly_chart函数)。常见触发场景:

  • 代码某处错误地将st或plotly_chart变量赋值为Plotly Figure对象,比如st = fig或plotly_chart = fig
  • 错误导入Plotly组件,比如from plotly.graph_objects import Figure as plotly_chart,直接覆盖了Streamlit的函数名
  • 极少数情况是Python环境缓存异常,重启即可解决

另外你提供的代码存在变量名不一致问题:定义的DataFrame是df,但绘图时用了pays_prod_moy,这会触发NameError;如果单独运行图表代码正常,说明你实际代码中pays_prod_moy是有效变量,但建议统一变量名避免混淆。

解决步骤

  1. 排查变量名冲突
    全局搜索代码文件,确认没有以下错误赋值:

    # 错误示例:覆盖了st变量
    st = fig
    # 错误示例:覆盖了plotly_chart函数
    plotly_chart = fig
    

    确保始终通过import streamlit as st导入Streamlit,且后续未修改st变量。

  2. 修正变量名不一致问题
    将绘图代码中的pays_prod_moy替换为实际定义的DataFrame变量(比如代码里的df):

    fig = px.scatter(x = df['Citations'],
                     y = df['Documents'],
                     size = df['H.index'], 
                     color = df['Rank'],
                     hover_name = df['Country'], 
                     log_x=True, 
                     size_max=40,
                     color_continuous_scale='RdBu')
    
  3. 重启运行环境
    关闭当前Streamlit服务,重新执行streamlit run your_script.py,排除环境缓存导致的异常。

  4. 验证版本兼容性
    执行以下命令检查库版本,确保Streamlit ≥1.0、Plotly ≥4.0:

    pip show streamlit plotly
    

    版本过低则重新安装:

    pip install --upgrade streamlit plotly
    

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.27 16:30:06