运行Manim代码时传入color参数触发TypeError错误的原因?
TypeError: Mobject.getattr..getter() 解决方法(Manim正弦/余弦图像动画)
错误原因:调用ax.get_graph()时直接传入color参数,但当前Manim版本中该方法不支持这种传参方式,样式参数需要通过graph_config字典传递。
修正后的代码:
from manim import * import numpy as np class Function(Scene): def construct(self): ax = Axes( x_range=[-5, 5, 0.5], y_range=[-3, 3, 0.5], x_axis_config={"numbers_to_include": np.arange(-5, 5, 1)}, y_axis_config={"numbers_to_include": [1]} ) ax_labels = ax.get_axis_labels() # 通过graph_config传递颜色参数 sin_graph = ax.get_graph(lambda x: np.sin(2*x), graph_config={"color": BLUE}) cos_graph = ax.get_graph(lambda x: np.cos(2*x), graph_config={"color": RED_B}) # 用raw字符串简化LaTeX写法,同时让标签与函数表达式对应 sin_label = ax.get_graph_label(sin_graph, label=r"\sin(2x)", x_val=-4.5, direction=UP*4) cos_label = ax.get_graph_label(cos_graph, label=r"\cos(2x)", x_val=4.5, direction=DOWN*2) ax_group = VGroup(ax, ax_labels) self.play(Create(ax_group), run_time=6) self.wait() self.play(Write(sin_label)) self.play(Create(sin_graph), run_time=2) self.wait() self.play(Create(cos_graph), run_time=2) self.play(Write(cos_label)) self.wait()
关键修改说明:
- 将
color=BLUE替换为graph_config={"color": BLUE},这是当前Manim Community版本指定图像样式的标准方式 - 把标签字符串改为raw格式(
r"\sin(2x)"),避免冗余的转义符,同时修正标签内容与函数表达式匹配 - 显式导入numpy,确保
np.arange能正常运行(若环境已全局导入可省略)
若使用的是旧版Manim(如原3Blue1Brown维护版本),也可以用以下方式设置颜色:
sin_graph = ax.get_graph(lambda x: np.sin(2*x)) sin_graph.set_color(BLUE)
内容的提问来源于stack exchange,提问作者Ahmed
相关产品推荐
相关产品推荐

