【问题标题】:Thickness of axes in Manim plotManim图中轴的厚度
【发布时间】:2021-02-18 19:59:47
【问题描述】:

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

我尝试在 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)

【问题讨论】:

    标签: graph axes manim thickness


    【解决方案1】:

    在 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)
    

    Preview:

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

    我的建议是在设置中省略animate=True,并在self.axes 上使用ShowCreation() 动画(或更新版本Create())替换它。

    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)
    

    【讨论】:

    • 太棒了。非常感谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-13
    • 1970-01-01
    • 2012-06-17
    • 1970-01-01
    相关资源
    最近更新 更多