graphaxesmanimthickness

Thickness of axes in Manim plot


I am just beginning my foray into Manim and have it installed correctly (community version) and have run some sample programs. One of the things I want to do is to change the thickness of lines used in a graph. I was able to change the thickness of the function being plotted using graph.set_stroke(width=1), but I can't figure out how to change the thickness of the axes. Any thoughts would be appreciated. (P.S. I am not an expert programmer.)

I tried using stroke_width=1 within the CreateGraph(GraphScene) class, and although it didn't cause an error, it also didn't work.

Here is the code:

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)

Solution

  • In a GraphScene, self.setup_axes() will initiate the axes and store them in self.axes.
    So by interpreting self.axes as your line object, you can apply set_stroke() and change the stroke width:

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

    You could even do x-axis and y-axis individually:

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

    Preview:

    I realized though, that this does not work properly with self.setup_axis(animate=True), as the axes are first animated with default stroke width and only then changed to the desired width.

    My suggestion is to leave out animate=True in the setup and substitute it by using the ShowCreation() animation (or more recent version Create()) on 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)