-
-
Save fbkarsdorp/d8351d1361fb2f5e7d078c157560a193 to your computer and use it in GitHub Desktop.
Manim animation for sampling without replacement
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import random | |
rows, cols = 9, 9 | |
class SamplingWithoutReplacement(Scene): | |
def construct(self): | |
bg = FullScreenRectangle() | |
bg.set_fill("#FFFFFF", 1) | |
self.add(bg) | |
colors = 'yellow red purple pink orange navyblue green grey blue'.split() | |
colors = random.choices(colors, k=rows * cols) | |
sailors = Group(*[ImageMobject(f"sailor-{sailor}.png").scale(0.12) for sailor in colors]) | |
sailors.arrange_in_grid(rows=rows, cols=cols, buff=0.1) | |
sailors.shift(LEFT * 3) | |
texts = Group( | |
Text("Our data also contain repeated observations;", font_size=24, font="Alegreya", color=BLACK), | |
Text("But repetitions are not potentially infinite;", font_size=24, font="Alegreya", color=BLACK), | |
).arrange(DOWN, aligned_edge=LEFT).shift(RIGHT * 3) | |
self.play( | |
FadeIn(sailors) | |
) | |
self.wait(3) | |
blue_sailors = [i for i, color in enumerate(colors) if color == "red"] | |
wiggles = [Wiggle(sailors[i], scale_value=1.5, run_time=3) for i in blue_sailors] | |
self.play( | |
AnimationGroup(*wiggles), | |
FadeIn(texts[0]) | |
) | |
self.wait(1) | |
self.play(FadeIn(texts[1])) | |
self.wait(3) | |
heading = Text("Archive is a sample without replacement;", font_size=24, font="Alegreya", color=BLACK) | |
footer = Text("Wrong sampling model can lead to overestimations;", font_size=24, font="Alegreya", color=BLACK) | |
sample = Group(*[Circle().scale(0.2) for _ in range(25)]) | |
sample.arrange_in_grid(rows=5, cols=5, buff=0.2) | |
sample.shift(RIGHT * 4) | |
heading.next_to(sample, UP * 2) | |
sample_animations = [] | |
sailor_ids = random.sample(list(range(rows * cols)), 25) | |
for i, sailor_id in enumerate(sailor_ids): | |
sample_animations.append( | |
sailors[sailor_id].animate.move_to(sample[i].get_center())) # .scale(1.5) | |
self.play( | |
FadeOut(texts[0]), | |
FadeOut(texts[1]), | |
FadeIn(heading), | |
LaggedStart(*sample_animations, lag_ratio=0.5)) | |
self.wait(3) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment