Below is a complete Manim script to create the 3D animation you described. The animation will:
- Start by drawing the 2D polar coordinate graph for ( r = 1 ), which is a circle of radius 1 in the xy-plane.
- Transition to a 3D view, keeping the circle visible.
- Move to show the side profile for ( y(\theta) ) vs. ( \theta ) (i.e., plot ( y = \sin(\theta) ) vs. ( \theta ) in the yz-plane).
- Then, move to show the side profile for ( x(\theta) ) vs. ( \theta ) (i.e., plot ( x = \cos(\theta) ) vs. ( \theta ) in the xz-plane).
This is implemented using Manim's 3D capabilities. I'll explain the code step by step in comments within the script for clarity.
Prerequisites
- Install Manim if you haven't already:
pip install manim.
- Run the script using:
manim -pql your_script.py PolarGraphScene (this renders the animation in low quality for preview).
Manim Script
import manim as m
import numpy as np
class PolarGraphScene(m.ThreeDScene):
def construct(self):
# Step 1: Draw the 2D polar graph (r=1 circle) in the xy-plane
circle = m.Circle(radius=1, color=m.BLUE) # Create a circle of radius 1
self.set_camera_orientation( # Set initial camera to look at the xy-plane
phi=75 * m.DEGREES, # Elevation angle
theta=30 * m.DEGREES, # Azimuth angle
distance=5 # Camera distance from origin
)
self.play(m.Create(circle)) # Animate drawing the circle
self.wait(2) # Pause to show the 2D graph
# Add axes for better 3D context
axes = m.ThreeDAxes(
x_range=[-1.5, 1.5, 1],
y_range=[-1.5, 1.5, 1],
z_range=[0, 2*np.pi, np.pi], # Z-range for θ (0 to 2π)
x_length=4,
y_length=4,
z_length=4
)
self.play(m.Create(axes)) # Animate adding the axes
self.wait(1)
# Step 2: Create and add the parametric curve for y(θ) vs. θ
# y(θ) = sin(θ), plotted in the yz-plane (x=0, y=θ on z-axis as parameter)
y_vs_theta_curve = m.ParametricFunction(
lambda t: np.array([0, np.sin(t), t]), # Points: (x=0, y=sin(t), z=t)
t_range=[0, 2*np.pi], # θ from 0 to 2π
color=m.GREEN
)
self.play(m.Create(y_vs_theta_curve)) # Animate drawing the curve
self.wait(1)
# Animate camera to focus on the yz-plane (side profile for y(θ) vs. θ)
self.play(
self.camera.animate.set_theta(90 * m.DEGREES).set_phi(60 * m.DEGREES) # Rotate to view yz-plane
)
self.wait(2) # Hold to show the view
# Step 3: Create and add the parametric curve for x(θ) vs. θ
# x(θ) = cos(θ), plotted in the xz-plane (y=0, x=θ on z-axis as parameter)
x_vs_theta_curve = m.ParametricFunction(
lambda t: np.array([np.cos(t), 0, t]), # Points: (x=cos(t), y=0, z=t)
t_range=[0, 2*np.pi], # θ from 0 to 2π
color=m.RED
)
self.play(m.Create(x_vs_theta_curve)) # Animate drawing the curve
self.wait(1)
# Animate camera to focus on the xz-plane (side profile for x(θ) vs. θ)
self.play(
self.camera.animate.set_theta(0 * m.DEGREES).set_phi(60 * m.DEGREES) # Rotate to view xz-plane
)
self.wait(2) # Hold to show the view
# Final wait to end the animation
self.wait(3)
if __name__ == "__main__":
m.main()
Explanation of the Animation
-
Drawing the 2D Polar Graph:
- The circle (
r=1) is drawn first in the xy-plane using m.Circle.
- This represents the polar equation ( r = 1 ), which is equivalent to ( x = \cos(\theta) ) and ( y = \sin(\theta) ).
-
Transition to 3D and Adding Axes:
- We switch to a 3D scene by adding 3D axes. The z-axis is scaled to accommodate ( \theta ) from 0 to ( 2\pi ), as we'll use it for the parametric plots.
-
Showing y(θ) vs. θ:
- The curve for ( y(\theta) = \sin(\theta) ) is plotted as a parametric function in the yz-plane (x=0).
- The camera then animates to focus on this plane, giving a "side profile" view.
-
Showing x(θ) vs. θ:
- The curve for ( x(\theta) = \cos(\theta) ) is plotted as a parametric function in the xz-plane (y=0).
- The camera animates again to focus on this plane, showing the other side profile.
-
Camera Movements:
- The camera rotations are smooth transitions that "move" the view to the respective planes, as per your request.
How to Customize or Run
- Rendering: Save this code in a file (e.g.,
polar_graph_scene.py) and run it with Manim as mentioned above.
- Adjustments:
- Change colors, speeds, or ranges in the code (e.g., modify
t_range for different θ limits).
- If you want more dramatic camera movements, tweak the
set_theta and set_phi values.
- Output: This will generate a video file (e.g.,
media/videos/.../480p15/PolarGraphScene.mp4) that you can watch.
This script should produce a clear, educational animation that visualizes the polar graph and its side profiles in 3D space. If you need further modifications, let me know!