
在使用manim绘制线图时,`plot_line_graph`方法默认会在每个数据点处绘制圆点。本教程将详细介绍如何利用`add_vertex_dots`参数来精确控制这些顶点圆点的显示与隐藏,通过具体的代码示例,帮助用户实现更简洁或定制化的图表展示,避免不必要的视觉干扰,从而提升manim动画的专业性和清晰度。
在Manim中创建动态和静态图表是其强大功能之一。Axes类配合plot_line_graph方法是绘制折线图的常用组合。然而,默认情况下,plot_line_graph不仅会绘制连接数据点的线条,还会自动在每个顶点(即数据点)上添加一个小圆点,这在某些场景下可能不是我们期望的效果,例如当图表线条本身已足够清晰,或需要自定义顶点样式时。
plot_line_graph方法是Axes类的一个成员函数,用于根据给定的数据点列表绘制一条折线。其核心功能是连接一系列坐标点,形成一个视觉上的线条路径。默认情况下,为了强调数据点的位置,Manim会在这些连接点上额外绘制小圆点。
Manim的官方文档为plot_line_graph方法提供了一个名为add_vertex_dots的参数。通过将此参数设置为False,可以轻松地禁用默认的顶点圆点显示。
参数详解:
以下代码示例将展示如何使用plot_line_graph方法绘制折线图,并分别演示启用和禁用顶点圆点的效果。
from manim import *
class LineGraphWithoutDots(Scene):
def construct(self):
# 1. 创建坐标系
axes = Axes(
x_range=[0, 10, 1],
y_range=[0, 10, 1],
x_length=7,
y_length=5,
axis_config={"color": BLUE_GRAY},
tips=False,
).add_coordinates()
axes_label = axes.get_axis_labels(x_label="X", y_label="Y")
# 2. 定义数据点
# 格式为 [(x1, y1), (x2, y2), ...]
data_points = [
(1, 2), (2, 5), (3, 4), (4, 7), (5, 6), (6, 9)
]
# 3. 绘制带有默认顶点圆点的折线图
graph_with_dots = axes.plot_line_graph(
graph_origin=axes.get_origin(),
x_values=[p[0] for p in data_points],
y_values=[p[1] for p in data_points],
line_color=RED,
add_vertex_dots=True, # 明确指定显示圆点 (默认行为)
vertex_dot_radius=0.08, # 可以调整圆点大小
vertex_dot_color=YELLOW
)
graph_label_with_dots = Text("带顶点圆点", font_size=24).next_to(graph_with_dots[0], UP)
# 4. 绘制不带顶点圆点的折线图
# 为了区分,我们将第二个图向上平移
data_points_shifted = [
(p[0], p[1] + 0.5) for p in data_points
]
graph_without_dots = axes.plot_line_graph(
graph_origin=axes.get_origin(),
x_values=[p[0] for p in data_points_shifted],
y_values=[p[1] for p in data_points_shifted],
line_color=GREEN,
add_vertex_dots=False # 禁用顶点圆点
)
graph_label_without_dots = Text("不带顶点圆点", font_size=24).next_to(graph_without_dots[0], DOWN)
# 5. 将所有Mobject添加到场景中
self.play(
Create(axes),
Write(axes_label)
)
self.wait(0.5)
self.play(
Create(graph_with_dots),
Write(graph_label_with_dots)
)
self.wait(1)
self.play(
Create(graph_without_dots),
Write(graph_label_without_dots)
)
self.wait(2)
代码解析:
通过掌握add_vertex_dots参数,Manim用户可以更灵活地控制线图的视觉呈现,从而创建出更符合特定需求和美学标准的动画。
以上就是Manim教程:控制plot_line_graph中的顶点显示的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号