使用manim的Axes类绘制坐标轴时,难以直接修改x轴和y轴的长度。但可以通过修改坐标轴对应的NumberLine类的线长度来实现。
以修改x轴长度为例,可以在Axes类的构造函数中添加如下代码:
class Axes(VGroup):
CONFIG = {
"x_min": -10,
"x_max": 10,
"y_min": -10,
"y_max": 10,
"x_axis_config": {},
"y_axis_config": {},
"graph_style": {},
"make_smooth_after_applying_functions": True,
}
def __init__(self, **kwargs):
super().__init__(**kwargs)
x_axis = NumberLine(
x_min=self.x_min,
x_max=self.x_max,
**self.x_axis_config
)
x_axis.stretch_to_fit_width(WIDTH) # 修改x轴长度
y_axis = NumberLine(
y_min=self.y_min,
y_max=self.y_max,
color=self.x_axis_config.get("color", GREY),
**self.y_axis_config
)
self.x_axis, self.y_axis = x_axis, y_axis
self.coords_to_point = Transform(
self.axes_coords_to_point, self.get_center
)
self.point_to_coords = Transform(
self.get_center, self.axes_point_to_coords
)
self.add(x_axis, y_axis)
将x_axis.stretch_to_fit_width(WIDTH)添加到x轴初始化时。其中WIDTH可以设为任意值,表示所需的长度。
同样,可以在y轴初始化时添加如下代码来修改y轴长度:
y_axis.stretch_to_fit_height(HEIGHT) # 修改y轴长度
其中HEIGHT同样可以设为任意值,表示所需的长度。