PolarPlane,顾名思义,是用于创建极坐标平面的类。

与笛卡尔坐标系不同,极坐标系是基于角度和半径来定位点的,这里的每个点由一个角度和距离原点的距离表示。

在 Manim 中,PolarPlane 通过极径(r)和极角(θ)来定位点,每个点的位置由「角度 + 到原点的距离」确定,便于处理与角度、半径相关的数学概念。

无论是坐标系网格,还是坐标的标记,PolarPlane都提供了直观的展示方式。

PolarPlane一般用于展示极函数、幅角等极坐标相关的数学概念。

1. 主要参数

极坐标系的参数和之前介绍的直角坐标系差别很大,主要参数如下:

参数名称 类型 说明
azimuth_step float 方位角(极角)标记之间的角度步长
azimuth_units str 方位角的单位
azimuth_compact_fraction bool 是否以紧凑的分数形式显示方位角标签
azimuth_offset float 方位角的偏移量,影响角度的起始位置
azimuth_direction str 方位角的递增方向
azimuth_label_buff float 方位角标签与极坐标图的距离
azimuth_label_font_size float 方位角标签的字体大小
size float 极坐标平面的大小,若未指定,则根据radius_max自动计算
radius_step float 半径标记之间的间隔
radius_max float 极坐标平面上半径的最大值
radius_config dict 自定义半径标记的样式
background_line_style dict 背景线的样式
faded_line_style dict 淡化线条的样式,用于控制辅助线的风格
faded_line_ratio int 控制淡化线条的比例

上面的参数有几个需要补充说明一下,

一个是azimuth_units参数,表示方位角的单位,它的值固定为以下5种:

  1. "PI radians":π 弧度,范围 [0, 2π](默认值)
  2. "TAU radians":τ 弧度,范围 [0, τ],其中 τ = 2π
  3. "degrees":度数,范围 [0, 360]
  4. "gradians":梯度,范围 [0, 400]
  5. None:归一化数值,范围 [0, 1]

还有azimuth_direction参数,它的值有2种:

  1. CW:顺时针
  2. CCW:逆时针

0.21 实测补充:azimuth_step 省略时会按单位自动取值——PI radians / TAU radians 为 20、degrees 为 36、gradians 为 40、None 为 1;数值越小,整圈的角度刻度越密。radius_max 省略时取 config["frame_y_radius"],size 省略时按 radius_max 自动计算;azimuth_direction 默认 CCW(逆时针),azimuth_units 默认 "PI radians"。

2. 主要方法

PolarPlane也继承了坐标系统CoordinateSystem类的方法,

其中,常用的是以下2个方法:

名称 说明
add_coordinates 在极坐标平面上添加坐标轴和刻度标签
plot_polar_graph 在极坐标平面上绘制极坐标函数 r = f(θ) 的图像

两个方法都有一个坑:add_coordinates(r_values=None, a_values=None) 返回 self,可以写成 plane = PolarPlane(...).add_coordinates();但它必须在 self.play(FadeIn(plane)) 之前调用,否则后加的标签不在已上屏的组里,观众看不到。plot_polar_graph(r_func, theta_range, **kwargs) 返回的是 ParametricFunction,必须自己 self.add 或 self.play 才会上屏,这是大量「黑片」示例的根因。

3. 使用示例

下面通过示例展示如何使用PolarPlane的参数和方法来创建和自定义极坐标平面。

3.1. 基本极坐标平面

这个示例创建了一个基本的极坐标平面,没有过多的自定义设置。

只是启用了add_coordinates方法来显示坐标轴和刻度标签。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
from manim import *
class PolarPlaneBasic(Scene):
"""3.1 基本极坐标平面。
add_coordinates() 返回 self,且必须在 FadeIn 之前调用,
否则后加的刻度标签不在已上屏的组里,观众看不到。
"""
def construct(self):
title = Text("基本极坐标平面 PolarPlane", font="STLiti", font_size=34).to_edge(UP)
plane = PolarPlane(size=5.6, radius_max=4.0, azimuth_step=8,
azimuth_label_font_size=20)
plane.add_coordinates()
self.play(FadeIn(title), run_time=0.6)
self.play(FadeIn(plane), run_time=1.6)
note = Text("先 add_coordinates() 再上屏,同心圆与角度刻度才会一起显示",
font="STLiti", font_size=22, color=YELLOW).to_edge(DOWN)
self.play(FadeIn(note), run_time=0.6)
self.wait(3.4)

下载 Python 脚本

3.2. 自定义角度单位和范围

这个示例先创建一个极坐标平面,然后对其角度单位和范围进行了自定义。

我们设置不同的azimuth_units和azimuth_step的值来更改角度刻度的单位和间隔,使其更加密集或稀疏,以适应不同的展示需求。

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
from manim import *
class PolarPlaneUnits(Scene):
"""3.2 azimuth_units 的三种角度单位并排对照。
degrees / PI radians / gradians 分别把整圈分成 12 / 10 / 20 格,
azimuth_step 越小标签越密;每个平面都要单独 add_coordinates()。
"""
def construct(self):
title = Text("azimuth_units 三种角度单位", font="STLiti", font_size=32).to_edge(UP)
specs = [
("degrees 角度 12 格", dict(azimuth_units="degrees", azimuth_step=12)),
("PI radians 弧度 10 格", dict(azimuth_units="PI radians", azimuth_step=10)),
("gradians 梯度 20 格", dict(azimuth_units="gradians", azimuth_step=20)),
]
planes = VGroup()
caps = VGroup()
for cap, kw in specs:
p = PolarPlane(size=3.5, radius_max=4.0,
azimuth_label_font_size=16, **kw)
p.add_coordinates()
planes.add(p)
caps.add(Text(cap, font="STLiti", font_size=19, color=YELLOW))
planes.arrange(RIGHT, buff=0.7).shift(DOWN * 0.35)
for c, p in zip(caps, planes):
c.next_to(p, UP, buff=0.28)
self.play(FadeIn(title), run_time=0.6)
self.play(LaggedStart(*[FadeIn(p) for p in planes],
lag_ratio=0.25, run_time=1.8))
self.play(LaggedStart(*[FadeIn(c) for c in caps],
lag_ratio=0.2, run_time=1.0))
note = Text("默认步长会按单位自动取值:PI/TAU 20、角度 36、梯度 40",
font="STLiti", font_size=20, color=GREY).to_edge(DOWN)
self.play(FadeIn(note), run_time=0.6)
self.wait(3.0)

三个平面并排对照:每个都要单独调用 add_coordinates(),再一起交给 self.play 淡入。

下载 Python 脚本

上图把三种刻度单位并排展示(角度 12 格、弧度 10 格、梯度 20 格);三个平面都调用了 add_coordinates() 并一起淡入,老示例只上屏了最后一个平面。

3.3. 自定义极坐标样式

这个示例演示如何通过PolarPlane的background_line_style参数和faded_line_style参数来控制极坐标系的背景线和淡化线的显示效果。

线的颜色,粗细都可以根据显示需要灵活调整。

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
37
38
39
40
from manim import *
class PolarPlaneStyles(Scene):
"""3.3 background_line_style / faded_line_style 三组配色对照。
background_line_style 控制主网格(同心圆 + 射线),
faded_line_style 控制被淡化比例 faded_line_ratio 拆出的辅助线。
"""
def construct(self):
title = Text("背景线与淡化线的配色", font="STLiti", font_size=32).to_edge(UP)
specs = [
("默认 蓝色", {}),
("红线 / 灰淡化线", dict(
background_line_style=dict(stroke_color=RED, stroke_width=3,
stroke_opacity=0.6),
faded_line_style=dict(stroke_color=GREY, stroke_width=2,
stroke_opacity=0.35),
faded_line_ratio=2)),
("绿线 / 青淡化线", dict(
background_line_style=dict(stroke_color=GREEN, stroke_width=3,
stroke_opacity=0.8),
faded_line_style=dict(stroke_color=TEAL, stroke_width=2,
stroke_opacity=0.6),
faded_line_ratio=2)),
]
planes = VGroup()
caps = VGroup()
for cap, kw in specs:
planes.add(PolarPlane(size=3.5, radius_max=4.0, **kw))
caps.add(Text(cap, font="STLiti", font_size=19, color=WHITE))
planes.arrange(RIGHT, buff=0.7).shift(DOWN * 0.35)
for c, p in zip(caps, planes):
c.next_to(p, UP, buff=0.28)
self.play(FadeIn(title), run_time=0.6)
self.play(LaggedStart(*[FadeIn(p) for p in planes],
lag_ratio=0.25, run_time=1.8))
self.play(LaggedStart(*[FadeIn(c) for c in caps],
lag_ratio=0.2, run_time=1.0))
note = Text("stroke_color / stroke_width / stroke_opacity 可分别调整",
font="STLiti", font_size=20, color=GREY).to_edge(DOWN)
self.play(FadeIn(note), run_time=0.6)
self.wait(3.0)

下载 Python 脚本

3.4. 极坐标函数图像

在这个示例中,我们利用PolarPlane的plot_polar_graph方法来在极坐标系中绘制函数图像。

通过函数 r = 3 × sin(6θ) 绘制六瓣玫瑰线;

通过函数 r = 2.5 × (1 − sin θ) 绘制心形线(cardioid)。

构造 ≠ 上屏:plot_polar_graph() 只返回曲线对象,必须 self.add(graph) 或 self.play(Create(graph)) 才会出现在画面里。早期示例把 graph1、graph2 算出来却从没上屏,所以视频是黑的;现在下载的脚本用 LaggedStart(Create(rose), Create(heart)) 把两条曲线逐个画出来。

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
from manim import *
import numpy as np
class PolarPlaneGraphs(Scene):
"""3.4 plot_polar_graph 绘制两条极坐标曲线。
r=3sin(6θ) 是六瓣玫瑰线,r=2.5(1-sinθ) 是心形线;
返回值是 ParametricFunction,必须 self.add/play 才会上屏。
"""
def construct(self):
title = Text("plot_polar_graph 极坐标函数图像", font="STLiti", font_size=32).to_edge(UP)
plane = PolarPlane(size=5.4, radius_max=5.5, radius_step=1, azimuth_step=12,
background_line_style=dict(stroke_color=BLUE_D,
stroke_width=1.5,
stroke_opacity=0.5))
rose = plane.plot_polar_graph(lambda t: 3 * np.sin(6 * t),
[0, 2 * PI], color=YELLOW, stroke_width=4)
heart = plane.plot_polar_graph(lambda t: 2.5 * (1 - np.sin(t)),
[0, 2 * PI], color=RED, stroke_width=4)
legend = VGroup(
Text("r = 3 sin(6θ) 六瓣玫瑰线", font="STLiti", font_size=21, color=YELLOW),
Text("r = 2.5 (1 - sinθ) 心形线", font="STLiti", font_size=21, color=RED),
).arrange(DOWN, aligned_edge=LEFT, buff=0.18).to_corner(DL)
self.play(FadeIn(title), run_time=0.6)
self.play(FadeIn(plane), run_time=1.0)
self.play(LaggedStart(Create(rose), Create(heart),
lag_ratio=0.5, run_time=2.0))
self.play(FadeIn(legend), run_time=0.8)
self.wait(3.0)

下载 Python 脚本

4. 附件

本文 4 个脚本分别对应 3.1~3.4 的视频,可直接运行(Manim CE 0.21.0),例如 manim -qh --disable_caching -a 20241005143101000.py。中文文字一律使用 Text(font="STLiti"),否则会显示成豆腐块。