Skip to content
TheChatArchiveThe Chat Archive
o1-mini-2024-09-12· OpenAIAi2 WildChat-4.8M (AI2)

AttributeError: Axes object has no attribute 'addticklabels' %%manim -ql -v WARNING…

  1. User

    AttributeError: Axes object has no attribute 'add_tick_labels'

    %%manim -ql -v WARNING FourierSeriesScene

    from manim import *

    class FourierSeriesScene(Scene): def construct(self): # ------------------------------ # 1. Title # ------------------------------ title = Text("Fourier Series Approximation of a Square Wave", font_size=36) self.play(Write(title)) self.wait(1) self.play(FadeOut(title))

        # ------------------------------
        # 2. Axes Configuration
        # ------------------------------
        axes = Axes(
            x_range=(-PI, PI, PI/2),
            y_range=(-1.5, 1.5, 0.5),
            x_length=10,
            y_length=4,
            axis_config={"color": WHITE},
        ).to_edge(DOWN)
        
        # Add axis labels
        axes_labels = axes.get_axis_labels(x_label="x", y_label="f(x)")
        
        # Add tick labels using 'add_tick_labels()' method
        axes.add_tick_labels(font_size=10)
        
        # Display axes and labels
        self.play(Create(axes), Write(axes_labels))
        
        # ------------------------------
        # 3. Target Square Wave
        # ------------------------------
        def square_wave(x):
            return 1 if x > 0 else -1
    
        square_wave_graph = axes.get_graph(
            lambda x: square_wave(x),
            discontinuities=[0],
            color=YELLOW
        )
        square_wave_label = axes.get_graph_label(
            square_wave_graph, label="\\text{Square Wave}", x_val=PI, direction=UR
        )
        self.play(Create(square_wave_graph), Write(square_wave_label))
        
        self.wait(1)
        
        # ------------------------------
        # 4. Fourier Series Initialization
        # ------------------------------
        fourier_series = VMobject()
        fourier_series.set_color(BLUE)
        fourier_series.set_stroke(width=2)
        
        self.add(fourier_series)
        
        # ------------------------------
        # 5. Fourier Series Construction
        # ------------------------------
        num_harmonics = 9  # Number of harmonics to include (only odds)
        harmonics = []
        
        # Initial Fourier Series (zero function)
        fourier_sum = axes.get_graph(lambda x: 0, color=BLUE)
        self.play(Transform(fourier_series, fourier_sum))
        
        for n in range(1, num_harmonics + 1, 2):  # Only odd harmonics
            # Coefficient for nth harmonic
            coeff = (4) / (n * PI)
            
            # Define the current harmonic function
            harmonic = axes.get_graph(
                lambda x, n=n: coeff * np.sin(n * x),
                color=BLUE
            )
            harmonics.append(harmonic)
            
            # Calculate the current sum of harmonics
            fourier_sum = axes.get_graph(
                lambda x, harmonics=harmonics: sum(
                    (4/(k * PI)) * np.sin(k * x) for k in range(1, len(harmonics)*2, 2)
                ),
                color=BLUE
            )
            
            # Update the Fourier series by adding the new harmonic
            self.play(Create(harmonic), run_time=1)
            self.play(Transform(fourier_series, fourier_sum), run_time=1)
            self.wait(0.5)
        
        # ------------------------------
        # 6. Final Wait and Cleanup
        # ------------------------------
        self.wait(2)
        
        # Clean Up
        self.play(FadeOut(VGroup(axes, axes_labels, square_wave_graph, square_wave_label, fourier_series)))
    
  2. Assistant

    The error you're encountering:

    AttributeError: Axes object has no attribute 'add_tick_labels'
    

    occurs because the Axes class in Manim does not have an add_tick_labels method. To add tick labels to your axes, you should use the add_numbers method on each axis individually (get_x_axis() and get_y_axis()). Below, I'll guide you through how to modify your code to resolve this error and correctly add tick labels with the desired font size.

    Step-by-Step Solution

    1. Remove the Incorrect Method Call

      First, remove or comment out the line causing the error:

      axes.add_tick_labels(font_size=10)
      
    2. Add Tick Labels Properly

      Use the add_numbers method on each axis to add tick labels. You can specify the font_size within this method to control the appearance of the labels.

      Here's how you can modify your code:

      # ------------------------------
      # 2. Axes Configuration
      # ------------------------------
      axes = Axes(
          x_range=(-PI, PI, PI/2),
          y_range=(-1.5, 1.5, 0.5),
          x_length=10,
          y_length=4,
          axis_config={
              "color": WHITE,
              "include_numbers": True,  # Ensure numbers are included
              "numbers_to_include": [-PI, -PI/2, 0, PI/2, PI],  # Customize as needed
              "decimal_number_config": {"num_decimal_places": 0},  # Optional: remove decimals
          },
      ).to_edge(DOWN)
      
      # Add axis labels
      axes_labels = axes.get_axis_labels(x_label="x", y_label="f(x)")
      
      # Add tick labels using 'add_numbers()' method
      x_labels = axes.get_x_axis().add_numbers(font_size=10)
      y_labels = axes.get_y_axis().add_numbers(font_size=10)
      
      # Display axes, labels, and tick numbers
      self.play(Create(axes), Write(axes_labels), Write(x_labels), Write(y_labels))
      

      Explanation of Changes:

      • include_numbers: Ensure that numbers (tick labels) are included on the axes.
      • numbers_to_include: Specify which tick labels you want to display. Adjust this list based on your requirements.
      • decimal_number_config: (Optional) Configure the number displays, such as removing decimal places.
      • add_numbers: This method adds the numerical labels to each axis. By specifying font_size=10, you control the size of the tick labels.
    3. Complete Updated Code

      Incorporating the above changes, here's the complete updated script:

      %%manim -ql -v WARNING FourierSeriesScene
      
      from manim import *
      
      class FourierSeriesScene(Scene):
          def construct(self):
              # ------------------------------
              # 1. Title
              # ------------------------------
              title = Text("Fourier Series Approximation of a Square Wave", font_size=36)
              self.play(Write(title))
              self.wait(1)
              self.play(FadeOut(title))
              
              # ------------------------------
              # 2. Axes Configuration
              # ------------------------------
              axes = Axes(
                  x_range=(-PI, PI, PI/2),
                  y_range=(-1.5, 1.5, 0.5),
                  x_length=10,
                  y_length=4,
                  axis_config={
                      "color": WHITE,
                      "include_numbers": True,
                      "numbers_to_include": [-PI, -PI/2, 0, PI/2, PI],
                      "decimal_number_config": {"num_decimal_places": 0},
                  },
              ).to_edge(DOWN)
              
              # Add axis labels
              axes_labels = axes.get_axis_labels(x_label="x", y_label="f(x)")
              
              # Add tick labels using 'add_numbers()' method
              x_labels = axes.get_x_axis().add_numbers(font_size=10)
              y_labels = axes.get_y_axis().add_numbers(font_size=10)
              
              # Display axes, labels, and tick numbers
              self.play(Create(axes), Write(axes_labels), Write(x_labels), Write(y_labels))
              
              # ------------------------------
              # 3. Target Square Wave
              # ------------------------------
              def square_wave(x):
                  return 1 if x > 0 else -1
      
              square_wave_graph = axes.get_graph(
                  lambda x: square_wave(x),
                  discontinuities=[0],
                  color=YELLOW
              )
              square_wave_label = axes.get_graph_label(
                  square_wave_graph, label="\\text{Square Wave}", x_val=PI, direction=UR
              )
              self.play(Create(square_wave_graph), Write(square_wave_label))
              
              self.wait(1)
              
              # ------------------------------
              # 4. Fourier Series Initialization
              # ------------------------------
              fourier_series = VMobject()
              fourier_series.set_color(BLUE)
              fourier_series.set_stroke(width=2)
              
              self.add(fourier_series)
              
              # ------------------------------
              # 5. Fourier Series Construction
              # ------------------------------
              num_harmonics = 9  # Number of harmonics to include (only odds)
              harmonics = []
              
              # Initial Fourier Series (zero function)
              fourier_sum = axes.get_graph(lambda x: 0, color=BLUE)
              self.play(Transform(fourier_series, fourier_sum))
              
              for n in range(1, num_harmonics + 1, 2):  # Only odd harmonics
                  # Coefficient for nth harmonic
                  coeff = (4) / (n * PI)
                  
                  # Define the current harmonic function
                  harmonic = axes.get_graph(
                      lambda x, n=n: coeff * np.sin(n * x),
                      color=BLUE
                  )
                  harmonics.append(harmonic)
                  
                  # Calculate the current sum of harmonics
                  fourier_sum = axes.get_graph(
                      lambda x, harmonics=harmonics: sum(
                          (4/(k * PI)) * np.sin(k * x) for k in range(1, len(harmonics)*2, 2)
                      ),
                      color=BLUE
                  )
                  
                  # Update the Fourier series by adding the new harmonic
                  self.play(Create(harmonic), run_time=1)
                  self.play(Transform(fourier_series, fourier_sum), run_time=1)
                  self.wait(0.5)
              
              # ------------------------------
              # 6. Final Wait and Cleanup
              # ------------------------------
              self.wait(2)
              
              # Clean Up
              self.play(FadeOut(VGroup(axes, axes_labels, x_labels, y_labels, square_wave_graph, square_wave_label, fourier_series)))
      
    4. Additional Recommendations

      • Customization: Adjust the numbers_to_include list based on the range and intervals you desire for your tick labels.

      • Styling: You can further style the tick labels by modifying the decimal_number_config or by applying additional transformations to x_labels and y_labels.

      • Axis Configuration: If you need more control over the axes (e.g., changing tick sizes, colors, etc.), refer to the Manim Axes Documentation for additional configuration options.

    By implementing these changes, your script should run without the AttributeError, and the axes will display tick labels with the specified font size.