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

使用Plotly的go.scatter绘图时报错TypeError: 'module' object is not callable的原因

问题原因及解决方案

你遇到的TypeError: 'module' object is not callable错误,本质是Plotly图形对象的命名大小写错误。

在Plotly的graph_objects(通常简写为go)模块中,创建折线图的正确类名是Scatter(首字母大写),但你代码里写的是小写的go.scatter——Python会把小写的scatter识别成一个子模块,而非可实例化的类,自然无法调用它生成图形对象。

修正后的代码

import plotly.graph_objects as go

# 假设stud、maths、science、programing、database_subject已提前定义
fig = go.Figure()

fig.add_trace(go.Scatter(x=stud,y=maths,name="Maths marks",line= dict(color='blue',width = 4)))

fig.add_trace(go.Scatter(x = stud, y = science, name = "Science marks", line = dict(color='red',width = 4)))

fig.add_trace(go.Scatter(x = stud ,y = programing, name = "Programing marks", line = dict(color='green',width = 4,dash='dash')))

fig.add_trace(go.Scatter(x = stud ,y = database_subject, name = "Database marks", line = dict(color='firebrick',width = 4,dash='dot')))


fig.update_layout(title="Students Marks", xaxis_title="Students", yaxis_title="Marks")

fig.show()

关键注意事项

  • Plotly的所有图形类(如Bar、Pie、Scatter等)均为首字母大写,必须严格遵循命名规范;
  • 若你习惯更简洁的语法,也可以改用plotly.express模块,它的API更直观且无需关注这类大小写问题,但graph_objects的自定义灵活性更强。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.09 11:19:58