Manim v0.18.0应用矩阵后坐标轴无法设为黑色的解决咨询
ManimCE v0.18.0:线性变换后坐标轴黑色设置无效的解决方法
问题描述
在使用ManimCE v0.18.0的LinearTransformationScene做线性变换可视化时,设置白色背景后希望将x、y轴改为黑色,但应用矩阵后坐标轴颜色未发生变化。相关代码片段如下:
def __init__(self, **kwargs): LinearTransformationScene.__init__( self, show_coordinates=False, leave_ghost_vectors=False, show_basis_vectors=False, *kwargs ) def construct(self): self.camera.background_color = WHITE Mobject.set_default(color=BLACK) MyTexTemplate = TexTemplate( tex_compiler="xelatex", output_format=".xdv", ) matrix = np.array([[2, -1], [0, 1]]) self.apply_matrix(matrix)
已升级至ManimCE v0.18.0并重启程序,但问题依旧,需要明确如何确保坐标轴显示为黑色,以及对应的属性或方法。
解决方法
LinearTransformationScene的坐标轴是场景初始化时就创建好的组件,全局的Mobject.set_default(color=BLACK)无法覆盖已初始化对象的颜色,需要直接修改坐标系的轴对象属性:
- 定位坐标轴对象:场景中的坐标系存储在
self.coordinate_system中,x轴对应self.coordinate_system.x_axis,y轴对应self.coordinate_system.y_axis。 - 设置轴的描边颜色:通过修改
stroke_color属性将轴设为黑色,可在应用矩阵之后执行该操作(确保变换后的轴颜色生效)。
修改后的代码示例
def __init__(self, **kwargs): LinearTransformationScene.__init__( self, show_coordinates=False, leave_ghost_vectors=False, show_basis_vectors=False, *kwargs ) def construct(self): self.camera.background_color = WHITE MyTexTemplate = TexTemplate( tex_compiler="xelatex", output_format=".xdv", ) matrix = np.array([[2, -1], [0, 1]]) self.apply_matrix(matrix) # 关键:设置坐标轴颜色为黑色 self.coordinate_system.x_axis.set_stroke(BLACK) self.coordinate_system.y_axis.set_stroke(BLACK)
如果需要在变换前就设置颜色(变换后颜色会保留),也可以将设置代码放在apply_matrix之前。
若要一次性修改所有坐标系相关元素(如刻度线),可以遍历self.coordinate_system的子元素并设置颜色:
for mob in self.coordinate_system.get_mobjects(): mob.set_stroke(BLACK)
内容的提问来源于stack exchange,提问作者Noel Gugler
相关产品推荐
相关产品推荐

