manimce中Axes设置x_length/y_length在LinearTransformationScene不生效问题
问题原因
Axes完全可以在LinearTransformationScene中使用,你遇到的问题是使用逻辑错误导致的,具体原因有两点:
- 你仅在代码中实例化了
Axes对象,没有调用self.add(graph)将其添加到当前场景中,因此无法渲染显示。 LinearTransformationScene本身会在初始化阶段自动创建内置坐标轴,你自定义的Axes不属于场景默认的变换作用域,apply_matrix方法默认只会对内置的坐标轴、向量等元素生效,不会自动作用到你手动创建的外部Axes实例上。
另外你贴出的可复现代码存在缩进错误,类下面的__init__、construct方法没有缩进至类的作用域下,运行会直接报语法错误。
解决方案
根据你的使用需求,可以选择两种适配方案:
方案1:修改场景内置坐标轴参数(推荐)
你不需要手动创建Axes实例,直接在初始化LinearTransformationScene时传入坐标轴长度参数即可,修改后的可运行代码如下:
from manim import * class LinearTransformationSceneExample(LinearTransformationScene): def __init__(self): LinearTransformationScene.__init__( self, show_coordinates=True, leave_ghost_vectors=True, x_length=6, # 直接指定内置x轴长度 y_length=3, # 直接指定内置y轴长度 ) def construct(self): # 不需要手动创建Axes,场景已自动生成符合配置的坐标轴 matrix = [[2, -1],[-1, 3]] self.apply_matrix(matrix) self.wait(0) matrix2 = [[16/5, 2/5],[-3/5, 9/5]] self.apply_matrix(matrix2) self.wait()
这种方式生成的坐标轴会自动被apply_matrix方法作用,不需要额外配置。
方案2:使用自定义Axes
如果有特殊需求必须使用自己实例化的Axes,需要完成两个额外操作:
- 调用
self.add(graph)将自定义坐标轴添加到场景 - 调用
apply_matrix时显式指定要作用的对象
如果不需要显示场景默认的内置坐标轴,可以在初始化时配置隐藏,参考代码如下:
from manim import * class LinearTransformationSceneExample(LinearTransformationScene): def __init__(self): LinearTransformationScene.__init__( self, show_coordinates=True, leave_ghost_vectors=True, axis_config={"visible": False} # 隐藏内置坐标轴 ) def construct(self): graph = Axes( x_length=6, y_length=3, ) self.add(graph) # 将自定义坐标轴添加到场景 matrix = [[2, -1],[-1, 3]] self.apply_matrix(matrix, graph) # 显式指定变换作用于自定义坐标轴 self.wait(0) matrix2 = [[16/5, 2/5],[-3/5, 9/5]] self.apply_matrix(matrix2, graph) self.wait()
内容的提问来源于stack exchange,提问作者Sophile
相关产品推荐
相关产品推荐

