使用Manim编码时遇TypeError:getter()仅接受1个参数却传入2个
问题排查与解决
错误原因
这个错误源于Manim Community版本的API参数要求变化:新版中传递给get_graph的函数需接受两个参数(x和y),或调用get_riemann_rectangles时需明确指定采样函数,你的单参数抛物线函数不符合当前API要求,导致参数传递不匹配。
修复方案
提供两种可行的修复方式:
方式一:调整抛物线函数参数
修改自定义函数,使其接受两个参数(第二个参数可忽略),适配新版API:
from manim import * class ParabolaWithRiemannRectangles(Scene): def construct(self): # 创建坐标系 plane = NumberPlane( x_range=(-4, 4, 1), y_range=(-1, 9, 1), x_length=8, y_length=10, ) # 定义适配API的抛物线函数 def parabola(x, y): return x ** 2 # 生成抛物线图形 parab_graph = plane.get_graph(parabola).set_color(GREEN) # 生成黎曼矩形 rectangles = plane.get_riemann_rectangles( parab_graph, x_range=(-4, 4), dx=0.5 ) # 依次添加元素到场景 self.play(Create(plane)) self.play(Create(parab_graph)) self.play(Create(rectangles)) self.wait()
方式二:用Lambda包装函数或直接指定func参数
保留原单参数函数,通过Lambda包装或直接传递func参数给get_riemann_rectangles:
from manim import * class ParabolaWithRiemannRectangles(Scene): def construct(self): plane = NumberPlane( x_range=(-4, 4, 1), y_range=(-1, 9, 1), x_length=8, y_length=10, ) def parabola(x): return x ** 2 # 用Lambda包装原函数生成图形 parab_graph = plane.get_graph(lambda x: parabola(x)).set_color(GREEN) # 直接传递func参数生成黎曼矩形 rectangles = plane.get_riemann_rectangles( func=parabola, x_range=(-4, 4), dx=0.5 ) self.play(Create(plane)) self.play(Create(parab_graph)) self.play(Create(rectangles)) self.wait()
补充说明
如果你的代码基于旧版Manim教程编写,就会出现这类版本兼容问题——旧版get_graph仅要求单参数函数,而新版Manim Community为支持更灵活的图形绘制,调整了函数参数要求。
内容的提问来源于stack exchange,提问作者Here Youtube
相关产品推荐
相关产品推荐

