ThreeDAxes是Manim中用于创建三维坐标系的类。
在数学、物理和工程等领域,三维坐标系的绘制是非常重要的。
ThreeDAxes使得用户能够在动画中直观地展示三维空间中的对象和关系,从而提高演示文稿和教学的效果。
ThreeDAxes提供了多种参数,如坐标轴的范围、长度、颜色、粗细等,以及光源位置和光泽度等,这些参数使得用户能够根据需要自定义坐标系的外观和行为。
1. 主要参数
ThreeDAxes的主要参数有:
| 参数名称 |
类型 |
说明 |
| x_range |
Sequence[float] |
X轴的范围,格式为(起始值,结束值,步长) |
| y_range |
Sequence[float] |
Y轴的范围,格式为(起始值,结束值,步长) |
| z_range |
Sequence[float] |
Z轴的范围,格式为(起始值,结束值,步长) |
| x_length |
float |
X轴的长度 |
| y_length |
float |
Y轴的长度 |
| z_length |
float |
Z轴的长度 |
| z_axis_config |
dict |
对Z轴的配置,如颜色、粗细等 |
| z_normal |
Vector3D |
定义Z轴的“正向”方向 |
| num_axis_pieces |
int |
轴的细分数量 |
| light_source |
Sequence[float] |
光源的位置,影响阴影和光照效果 |
ThreeDAxes也是继承自Axes的,所以也有x_axis_config和y_axis_config参数,这里没有再列出来。
2. 主要方法
ThreeDAxes主要使用的是下面 3 个方法:
| 名称 |
说明 |
| get_axis_labels |
为每个轴添加标签 |
| add_coordinates |
在坐标系中添加网格线或刻度标记 |
| plot_surface |
在坐标系中绘制二元函数 z = f(x, y) 的曲面 |
三个方法都要「接住返回值」:get_axis_labels() 返回 VGroup,add_coordinates() 返回 self,plot_surface() 返回 Surface。除了 add_coordinates() 是原地修改,另两个返回值都必须自己 self.add / self.play 才会上屏。add_coordinates() 还必须写在 self.play(FadeIn(axes)) 之前,否则后加的刻度标签不在已上屏的组里。
3D 的前提是相机:ThreeDAxes 要放在 ThreeDScene 里,并用 self.set_camera_orientation(phi=65*DEGREES, theta=-50*DEGREES) 转动相机,否则沿默认 z 轴方向看过去只是一根平面十字(老视频正是这样)。中文一律用 Text(font="STLiti");贴在三轴上的文字会随相机倾斜,重要说明可用 add_fixed_in_frame_mobjects() 固定在屏幕上。
3. 使用示例
下面通过几个示例展示ThreeDAxes的关键功能和应用场景,每个示例都聚焦于该类的一个或多个核心参数或方法。
3.1. 坐标轴范围和刻度
在这个示例中,我们使用ThreeDAxes的x_range, y_range, 和z_range参数来设置坐标轴的范围,
并通过x_length, y_length, z_length来调整坐标轴的长度。
示例中:
- 设置x轴范围为
(-5, 5),y轴范围为(-3, 3),z轴范围为(-2, 2)
- 调整x轴长度为
10个单位,y轴长度为6个单位,z轴长度为4个单位
- 设置x轴刻度单位为
1.5,y轴刻度单位为1,z轴刻度单位为0.5
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| from manim import * class ThreeDAxesRange(ThreeDScene): """3.1 三维坐标轴的范围与刻度。 必须用 ThreeDScene 并 set_camera_orientation 转动相机, 否则沿默认的 z 轴方向看过去,ThreeDAxes 只是一个平面十字。 add_coordinates() 同样要在 FadeIn 之前调用。 """ def construct(self): self.set_camera_orientation(phi=65 * DEGREES, theta=-50 * DEGREES) title = Text("三维坐标轴:范围与刻度", font="STLiti", font_size=32).to_edge(UP) self.add_fixed_in_frame_mobjects(title) axes = ThreeDAxes(x_range=(-5, 5, 1.5), y_range=(-3, 3, 1), z_range=(-2, 2, 0.5), x_length=8, y_length=5, z_length=4) axes.add_coordinates() self.play(FadeIn(title), run_time=0.6) self.play(FadeIn(axes), run_time=1.8) note = Text("range 第三位是刻度步长:x=1.5、y=1、z=0.5", font="STLiti", font_size=22, color=YELLOW).to_edge(DOWN) self.add_fixed_in_frame_mobjects(note) self.play(FadeIn(note), run_time=0.6) self.wait(3.2)
|
下载 Python 脚本
3.2. 自定义坐标轴颜色和标签
本示例将展示如何使用*_axis_config参数来自定义坐标轴的颜色、粗细以及标签。
使用x_axis_config将x轴设置红色,轴的粗细设为1,
使用y_axis_config将y轴设置绿色,轴的粗细设为3,
使用z_axis_config将z轴设置蓝色,轴的粗细设为5。
然后,再为每个轴添加自定义标签:x 轴为「人口」,y 轴为「年龄」,z 轴为「收入」。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
| from manim import * class ThreeDAxesLabels(ThreeDScene): """3.2 自定义三轴颜色、粗细与中文轴标签。 get_axis_labels() 返回 VGroup,必须自己上屏; 中文 Text 必须指定 font="STLiti",否则是豆腐块。 """ def construct(self): self.set_camera_orientation(phi=65 * DEGREES, theta=-50 * DEGREES) title = Text("自定义三轴颜色与标签", font="STLiti", font_size=32).to_edge(UP) self.add_fixed_in_frame_mobjects(title) axes = ThreeDAxes( x_axis_config=dict(color=RED, stroke_width=2), y_axis_config=dict(color=GREEN, stroke_width=4), z_axis_config=dict(color=BLUE, stroke_width=6), ).scale(0.65) labels = axes.get_axis_labels( Text("人口", font="STLiti", font_size=26, color=RED), Text("年龄", font="STLiti", font_size=26, color=GREEN), Text("收入", font="STLiti", font_size=26, color=BLUE), ) legend = VGroup( Text("x 轴 人口", font="STLiti", font_size=22, color=RED), Text("y 轴 年龄", font="STLiti", font_size=22, color=GREEN), Text("z 轴 收入", font="STLiti", font_size=22, color=BLUE), ).arrange(DOWN, aligned_edge=LEFT, buff=0.16).to_corner(DL) self.add_fixed_in_frame_mobjects(legend) self.play(FadeIn(title), run_time=0.6) self.play(FadeIn(axes), run_time=1.6) self.play(LaggedStart(*[FadeIn(x) for x in labels], lag_ratio=0.3, run_time=1.2)) self.play(FadeIn(legend), run_time=0.8) note = Text("3D 中的 Text 会随相机倾斜,重要中文建议做固定朝向图例", font="STLiti", font_size=20, color=YELLOW).to_edge(DOWN) self.add_fixed_in_frame_mobjects(note) self.play(FadeIn(note), run_time=0.6) self.wait(2.6)
|
下载 Python 脚本
3.3. 绘制三维函数
二维坐标系Axes中绘制的是曲线函数,在三维坐标系ThreeDAxes中,可以使用plot_surface函数来绘制曲面函数。
本示例绘制抛物面 z = x² + y²。
构造 ≠ 上屏:plot_surface() 只返回 Surface 对象,必须 self.add(surface) 或 self.play(FadeIn(surface)) 才会出现在画面里。早期示例把 graph 算出来就结束,视频因此是全黑的;现在下载的脚本已补上淡入。u_range / v_range 是曲面的参数范围,与 x_range / y_range(坐标轴的显示范围)互相独立。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
| from manim import * class ThreeDAxesSurface(ThreeDScene): """3.3 plot_surface 绘制 z = x² + y² 抛物面。 返回的 Surface 只是被构造出来,必须 self.add / self.play 才会上屏, 老示例把 graph 算出来就结束,视频因此是全黑的。 """ def construct(self): self.set_camera_orientation(phi=65 * DEGREES, theta=-50 * DEGREES) title = Text("plot_surface 绘制三维曲面", font="STLiti", font_size=32).to_edge(UP) self.add_fixed_in_frame_mobjects(title) axes = ThreeDAxes(x_range=[-3, 3], y_range=[-3, 3], z_range=[-1, 3], x_length=6, y_length=6, z_length=4) surface = axes.plot_surface( lambda u, v: u ** 2 + v ** 2, u_range=[-1, 1], v_range=[-1, 1], fill_color=BLUE_E, fill_opacity=0.85, checkerboard_colors=[BLUE_D, BLUE_E], stroke_width=0.5) self.play(FadeIn(title), run_time=0.6) self.play(FadeIn(axes), run_time=1.2) self.play(FadeIn(surface), run_time=1.8) note = Text("z = x² + y²:曲面必须 self.add / self.play 才上屏", font="STLiti", font_size=22, color=YELLOW).to_edge(DOWN) self.add_fixed_in_frame_mobjects(note) self.play(FadeIn(note), run_time=0.6) self.wait(3.0)
|
下载 Python 脚本
4. 附件
本文 3 个脚本都是 ThreeDScene,可直接运行(Manim CE 0.21.0),例如 manim -qh --disable_caching -a 20241006075828000.py;三个脚本分别对应 3.1~3.3 的视频,中文一律用 Text(font="STLiti"),否则会显示成豆腐块。
主观感受,仅供参考。本文采用 本文采用 CC BY-NC-SA 4.0 许可 协议,
完整转载请注明来自
数学动画引擎。