Skip to content

Instantly share code, notes, and snippets.

@EteimZ
Created January 20, 2025 11:31
Show Gist options
  • Save EteimZ/ef998ffaf9e8d738a6df30b6f73a42a6 to your computer and use it in GitHub Desktop.
Save EteimZ/ef998ffaf9e8d738a6df30b6f73a42a6 to your computer and use it in GitHub Desktop.
This animation is used to show the Probability vs Suprise Graph using manim.
class Suprise(Scene):
"""
This animation is used to show the Probability vs Suprise Graph.
The graph shows the inverse relationship between Probability and
Suprise.
"""
def construct(self):
# Define the graph axes
axes = Axes(
x_range = [0, 1, 0.1],
y_range = [0, 7, 1],
tips=False,
axis_config = {"include_numbers": True},
x_length=8,
y_length=5)
# Add axes labels
y_label = axes.get_y_axis_label(Text("Suprise").rotate(90 * DEGREES).scale(0.5), edge=LEFT, direction=LEFT, buff=0.5)
x_label = axes.get_x_axis_label(Text("Probability").scale(0.5), edge=DOWN, direction=DOWN, buff=0.5)
# Create a value tracker
t = ValueTracker(1)
# Define the Suprise function
def func(x):
return math.log2(1/x)
# Plot the graph
graph = axes.plot(func, x_range=[0.01, 1, 0.001], color=BLUE)
# Define the title
title = Title(
r"Graph of Surprise: $y=\log\left(\frac{1}{x}\right)$",
include_underline=False,
font_size=40,
)
# Define the dots and use the value tracker to make their position dynamic
initial_point = [axes.coords_to_point(t.get_value(), func(t.get_value()))]
dot = Dot(point=initial_point, color=GREEN)
initial_point2 = [axes.coords_to_point(t.get_value(), 0)]
dot2 = Dot(point=initial_point2, color=GREEN)
dot.add_updater(lambda x: x.move_to(axes.c2p(t.get_value(), func(t.get_value()))))
dot2.add_updater(lambda x: x.move_to(axes.c2p(t.get_value(), 0)))
# Define the line
line = Line(dot.get_center(), dot2.get_center()).set_z_index(-1)
line.add_updater(lambda x: x.become(Line(dot.get_center(), dot2.get_center())))
# Animation sequence for title and graph
self.play(Create(title))
self.play(Create(axes))
self.play(Create(x_label))
self.play(Create(y_label))
self.play(FadeIn(graph))
# Add the dots and line
self.add(dot)
self.add(dot2)
self.add(line)
# Update the value tracker every frame
self.play(t.animate.set_value(0.01), run_time=8)
# Wait for 3 seconds then end the animation
self.wait(3)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment