0

我刚刚开始涉足 Manim 并已正确安装(社区版本)并运行了一些示例程序。我想做的一件事是改变图表中使用的线条的粗细。我能够使用 graph.set_stroke(width=1) 更改正在绘制的函数的粗细,但我不知道如何更改轴的粗细。任何想法将不胜感激。(PS 我不是专业的程序员。)

我尝试在 CreateGraph(GraphScene) 类中使用 stroke_width=1,虽然它没有导致错误,但它也不起作用。

这是代码:

from manim import *

class CreateGraph(GraphScene):
    def __init__(self, **kwargs):
        GraphScene.__init__(
            self,
            x_min=-3,
            x_max=3,
            y_min=-5,
            y_max=5,
            graph_origin=ORIGIN,
            axes_color=BLUE,
            stroke_width=1
        )

    def construct(self):
        # Create Graph
        self.setup_axes(animate=True)
        graph = self.get_graph(lambda x: x**2, WHITE)
        graph_label = self.get_graph_label(graph, label='x^{2}')
        graph.set_stroke(width=1)

        graph2 = self.get_graph(lambda x: x**3, WHITE)
        graph_label2 = self.get_graph_label(graph2, label='x^{3}')

        # Display graph
        self.play(ShowCreation(graph), Write(graph_label))
        self.wait(1)
        self.play(Transform(graph, graph2), Transform(graph_label, graph_label2))
        self.wait(1)
4

1 回答 1

0

在 GraphScene 中,self.setup_axes()将启动轴并将它们存储在self.axes.
因此,通过将 self.axes 解释为您的线对象,您可以应用set_stroke()和更改笔划宽度:

self.setup_axes()
self.axes.set_stroke(width=0.5)   

你甚至可以单独做 x 轴和 y 轴:

self.setup_axes()
self.x_axis.set_stroke(width=1)
self.y_axis.set_stroke(width=7)

预习:

但我意识到,这不适用于self.setup_axis(animate=True),因为轴首先使用默认笔划宽度进行动画处理,然后才更改为所需的宽度。

我的建议是省略设置animate=True并使用.ShowCreation()Create()self.axes

from manim import *

class CreateGraph(GraphScene):
    def __init__(self, **kwargs):
        GraphScene.__init__(
            self,
            x_min=-3,
            x_max=3,
            y_min=-5,
            y_max=5,
            graph_origin=ORIGIN,
            axes_color=BLUE,
            stroke_width=1
        )

    def construct(self):
        # Create Graph

        self.setup_axes()                         # Leave out animate

        # Both axes
        # self.axes.set_stroke(width=1)           # Set stroke
        # or

        # Individually
        self.x_axis.set_stroke(width=1)     
        self.y_axis.set_stroke(width=7)


        
        graph = self.get_graph(lambda x: x**2, WHITE)
        graph_label = self.get_graph_label(graph, label='x^{2}')
        graph.set_stroke(width=1)

        graph2 = self.get_graph(lambda x: x**3, WHITE)
        graph_label2 = self.get_graph_label(graph2, label='x^{3}')


        # Display graph

        self.play(Create(self.axes))                # Play axes creation 


        self.play(Create(graph), Write(graph_label))
        self.wait(1)
        self.play(Transform(graph, graph2), Transform(graph_label, graph_label2))
        self.wait(1)
于 2021-06-06T08:29:38.907 回答