statanim 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- statanim/__init__.py +0 -0
- statanim/animations/__init__.py +0 -0
- statanim/animations/clt_demo.py +1852 -0
- statanim/animations/flip_roll.py +1851 -0
- statanim/animations/sampling.py +2291 -0
- statanim/animations/transitions.py +1857 -0
- statanim/axes/__init__.py +0 -0
- statanim/axes/axes3d.py +1989 -0
- statanim/axes/grid3d.py +1127 -0
- statanim/axes/number_plane3d.py +1526 -0
- statanim/charts/__init__.py +0 -0
- statanim/charts/bar_chart3d.py +1572 -0
- statanim/charts/box_plot3d.py +1977 -0
- statanim/charts/heat_map3d.py +1639 -0
- statanim/charts/histogram3d.py +1019 -0
- statanim/charts/line_plot3d.py +1891 -0
- statanim/charts/scatter_plot3d.py +1469 -0
- statanim/charts/violin_plot3d.py +1567 -0
- statanim/core/__init__.py +0 -0
- statanim/core/base.py +1585 -0
- statanim/core/colors.py +1373 -0
- statanim/core/math_utils.py +2081 -0
- statanim/core/tex_utils.py +2111 -0
- statanim/distributions/__init__.py +0 -0
- statanim/distributions/base_dist.py +1936 -0
- statanim/distributions/cdf_viz.py +1773 -0
- statanim/distributions/continuous_dists.py +1769 -0
- statanim/distributions/discrete_dists.py +1734 -0
- statanim/distributions/normal3d.py +1877 -0
- statanim/distributions/pdf_viz.py +2247 -0
- statanim/distributions/pmf_viz.py +1936 -0
- statanim/inference/__init__.py +0 -0
- statanim/inference/confidence_interval.py +1693 -0
- statanim/inference/error_types.py +1825 -0
- statanim/inference/hypothesis.py +1821 -0
- statanim/inference/sampling_dist.py +1933 -0
- statanim/probability/__init__.py +0 -0
- statanim/probability/bayes.py +1682 -0
- statanim/probability/prob_tree.py +1478 -0
- statanim/probability/sample_space.py +1642 -0
- statanim/probability/venn3d.py +1857 -0
- statanim/props/__init__.py +0 -0
- statanim/props/card.py +2259 -0
- statanim/props/coin.py +1011 -0
- statanim/props/die.py +1391 -0
- statanim/props/spinner.py +1591 -0
- statanim/props/urn.py +1531 -0
- statanim/regression/__init__.py +0 -0
- statanim/regression/correlation.py +2805 -0
- statanim/regression/regression_plane.py +2286 -0
- statanim/regression/residuals.py +2599 -0
- statanim/scenes/__init__.py +0 -0
- statanim/scenes/demo_bayes.py +2056 -0
- statanim/scenes/demo_clt.py +1756 -0
- statanim/scenes/demo_distributions.py +1905 -0
- statanim/scenes/demo_hypothesis.py +1791 -0
- statanim/ui/__init__.py +0 -0
- statanim/ui/labels.py +1844 -0
- statanim/ui/panels.py +1937 -0
- statanim/ui/table3d.py +1745 -0
- statanim/ui/ticker.py +2007 -0
- statanim-0.1.0.dist-info/METADATA +463 -0
- statanim-0.1.0.dist-info/RECORD +66 -0
- statanim-0.1.0.dist-info/WHEEL +5 -0
- statanim-0.1.0.dist-info/licenses/LICENSE +21 -0
- statanim-0.1.0.dist-info/top_level.txt +1 -0
|
@@ -0,0 +1,1857 @@
|
|
|
1
|
+
"""
|
|
2
|
+
statanim/animations/transitions.py
|
|
3
|
+
=======================================
|
|
4
|
+
Reusable transition and morphing animations for statistical visualizations.
|
|
5
|
+
|
|
6
|
+
Design philosophy
|
|
7
|
+
-----------------
|
|
8
|
+
This module owns animations that *cross the boundary* between chart types —
|
|
9
|
+
motions that none of the chart-specific modules can own alone. Every
|
|
10
|
+
function or class here is composable: it takes existing VGroup / VMobject
|
|
11
|
+
instances from other modules (``BarChart3D``, ``LineSeries3D``, etc.) and
|
|
12
|
+
returns Manim animation objects that the caller plays.
|
|
13
|
+
|
|
14
|
+
No visual geometry is built here from scratch. ``transitions.py`` is a
|
|
15
|
+
pure animation layer.
|
|
16
|
+
|
|
17
|
+
Organisation
|
|
18
|
+
------------
|
|
19
|
+
The module is split into seven conceptual groups:
|
|
20
|
+
|
|
21
|
+
1. Curve morphing – ``DistMorph3D``, ``HistMorph3D``
|
|
22
|
+
Interpolate one distribution shape into another point-by-point.
|
|
23
|
+
|
|
24
|
+
2. CDF / PDF coupling – ``CDFBuild3D``, ``HistToCurve3D``
|
|
25
|
+
Show the relationship between density and cumulative functions.
|
|
26
|
+
|
|
27
|
+
3. Parameter sweeps – ``ParameterSweep3D``
|
|
28
|
+
Animate a distribution changing as μ, σ, λ, p, … vary.
|
|
29
|
+
|
|
30
|
+
4. Scene transitions – ``CurtainReveal3D``, ``SceneWipe3D``
|
|
31
|
+
Structured wipes and reveals for transitioning between topics.
|
|
32
|
+
|
|
33
|
+
5. Camera transitions – ``FocusZoom3D``, ``OrbitTransition``
|
|
34
|
+
Smooth camera repositioning to emphasise a region of interest.
|
|
35
|
+
|
|
36
|
+
6. Statistical reveals – ``CIBuild3D``, ``RippleUpdate3D``
|
|
37
|
+
Animations that teach a specific statistical concept through motion.
|
|
38
|
+
|
|
39
|
+
7. Cloud / scatter – ``CollapseToMean3D``, ``ScatterToRegression3D``
|
|
40
|
+
Dot-cloud animations for teaching expectation and regression.
|
|
41
|
+
|
|
42
|
+
All public classes follow the same interface:
|
|
43
|
+
``build(*args) → Animation`` or ``run(scene, *args) → None``
|
|
44
|
+
When a transition needs to modify the scene directly (e.g. camera moves)
|
|
45
|
+
it uses ``run``; otherwise it returns a composable animation.
|
|
46
|
+
|
|
47
|
+
Classes
|
|
48
|
+
-------
|
|
49
|
+
DistMorph3D
|
|
50
|
+
HistMorph3D
|
|
51
|
+
CDFBuild3D
|
|
52
|
+
HistToCurve3D
|
|
53
|
+
ParameterSweep3D
|
|
54
|
+
CurtainReveal3D
|
|
55
|
+
SceneWipe3D
|
|
56
|
+
FocusZoom3D
|
|
57
|
+
OrbitTransition
|
|
58
|
+
CIBuild3D
|
|
59
|
+
RippleUpdate3D
|
|
60
|
+
CollapseToMean3D
|
|
61
|
+
ScatterToRegression3D
|
|
62
|
+
|
|
63
|
+
Helpers / internals
|
|
64
|
+
-------------------
|
|
65
|
+
_resample_vmobject_points
|
|
66
|
+
_bar_heights_from_group
|
|
67
|
+
_build_normal_curve_points
|
|
68
|
+
_build_kde_curve_points
|
|
69
|
+
|
|
70
|
+
Module-level convenience functions
|
|
71
|
+
-----------------------------------
|
|
72
|
+
dist_morph – quick DistMorph3D.build() call
|
|
73
|
+
hist_morph – quick HistMorph3D.build() call
|
|
74
|
+
cdf_build – quick CDFBuild3D.build() call
|
|
75
|
+
parameter_sweep – quick ParameterSweep3D.run() call
|
|
76
|
+
curtain_reveal – quick CurtainReveal3D.build() call
|
|
77
|
+
ci_build – quick CIBuild3D.build() call
|
|
78
|
+
collapse_to_mean – quick CollapseToMean3D.build() call
|
|
79
|
+
|
|
80
|
+
Usage
|
|
81
|
+
-----
|
|
82
|
+
from statanim.animations.transitions import DistMorph3D, ParameterSweep3D
|
|
83
|
+
from statanim.charts.line_plot3d import line_from_function, PDF_LINE
|
|
84
|
+
import numpy as np
|
|
85
|
+
|
|
86
|
+
class MyScene(ThreeDScene):
|
|
87
|
+
def construct(self):
|
|
88
|
+
self.set_camera_orientation(phi=65*DEGREES, theta=-45*DEGREES)
|
|
89
|
+
|
|
90
|
+
# Morph a normal PDF into a t-distribution
|
|
91
|
+
normal_curve = line_from_function(
|
|
92
|
+
lambda x: np.exp(-0.5*x**2) / np.sqrt(2*np.pi),
|
|
93
|
+
x_range=(-4, 4, 0.05), config=PDF_LINE,
|
|
94
|
+
)
|
|
95
|
+
t3_curve = line_from_function(
|
|
96
|
+
lambda x: (1 + x**2/3)**(-2) * 0.608,
|
|
97
|
+
x_range=(-4, 4, 0.05), config=PDF_LINE,
|
|
98
|
+
)
|
|
99
|
+
self.add(normal_curve)
|
|
100
|
+
self.play(
|
|
101
|
+
DistMorph3D(normal_curve.stroke, t3_curve.stroke).build(run_time=1.5)
|
|
102
|
+
)
|
|
103
|
+
|
|
104
|
+
# Sweep σ from 0.5 to 2.0
|
|
105
|
+
ParameterSweep3D(
|
|
106
|
+
func=lambda x, s: np.exp(-0.5*(x/s)**2)/(s*np.sqrt(2*np.pi)),
|
|
107
|
+
x_range=(-4, 4, 0.05),
|
|
108
|
+
param_range=(0.5, 2.0, 0.1),
|
|
109
|
+
param_name="σ",
|
|
110
|
+
existing_curve=normal_curve.stroke,
|
|
111
|
+
).run(self, run_time_total=3.0)
|
|
112
|
+
"""
|
|
113
|
+
|
|
114
|
+
from __future__ import annotations
|
|
115
|
+
|
|
116
|
+
from dataclasses import dataclass, field
|
|
117
|
+
from typing import (
|
|
118
|
+
List, Sequence, Tuple, Optional, Callable, Union, Dict, Iterator
|
|
119
|
+
)
|
|
120
|
+
import numpy as np
|
|
121
|
+
|
|
122
|
+
from manim import (
|
|
123
|
+
VGroup, VMobject, Polygon, Line, DashedLine, Dot3D,
|
|
124
|
+
Text, MathTex, Arrow, Rectangle,
|
|
125
|
+
ThreeDScene, Scene,
|
|
126
|
+
AnimationGroup, LaggedStart, Succession,
|
|
127
|
+
Create, FadeIn, FadeOut, Transform,
|
|
128
|
+
UpdateFromAlphaFunc, Flash,
|
|
129
|
+
DEGREES, PI, TAU,
|
|
130
|
+
RIGHT, LEFT, UP, DOWN, OUT, IN, ORIGIN,
|
|
131
|
+
X_AXIS, Y_AXIS, Z_AXIS,
|
|
132
|
+
WHITE, BLACK, GRAY, BLUE, GREEN, RED, YELLOW,
|
|
133
|
+
ManimColor, color_to_rgb, rgba_to_color, color_to_rgba,
|
|
134
|
+
rate_functions, smooth,
|
|
135
|
+
)
|
|
136
|
+
|
|
137
|
+
# ---------------------------------------------------------------------------
|
|
138
|
+
# Colour helpers
|
|
139
|
+
# ---------------------------------------------------------------------------
|
|
140
|
+
|
|
141
|
+
def _with_opacity(color: ManimColor, opacity: float) -> ManimColor:
|
|
142
|
+
r, g, b = color_to_rgb(color)
|
|
143
|
+
return rgba_to_color([r, g, b, max(0.0, min(1.0, opacity))])
|
|
144
|
+
|
|
145
|
+
def _darken(color: ManimColor, factor: float = 0.65) -> ManimColor:
|
|
146
|
+
r, g, b = color_to_rgb(color)
|
|
147
|
+
return ManimColor([r * factor, g * factor, b * factor])
|
|
148
|
+
|
|
149
|
+
def _lighten(color: ManimColor, factor: float = 1.35) -> ManimColor:
|
|
150
|
+
r, g, b = color_to_rgb(color)
|
|
151
|
+
return ManimColor([min(r * factor, 1.0), min(g * factor, 1.0), min(b * factor, 1.0)])
|
|
152
|
+
|
|
153
|
+
def _lerp_color(a: ManimColor, b: ManimColor, t: float) -> ManimColor:
|
|
154
|
+
ra, ga, ba = color_to_rgb(a)
|
|
155
|
+
rb, gb, bb = color_to_rgb(b)
|
|
156
|
+
return ManimColor([ra + (rb - ra) * t, ga + (gb - ga) * t, ba + (bb - ba) * t])
|
|
157
|
+
|
|
158
|
+
|
|
159
|
+
# ---------------------------------------------------------------------------
|
|
160
|
+
# Internal geometry helpers
|
|
161
|
+
# ---------------------------------------------------------------------------
|
|
162
|
+
|
|
163
|
+
def _resample_vmobject_points(mob: VMobject, n: int) -> np.ndarray:
|
|
164
|
+
"""Return *n* evenly-spaced points along *mob*'s path.
|
|
165
|
+
|
|
166
|
+
Used to normalise two curves to the same point count so they can be
|
|
167
|
+
interpolated point-by-point without topology jumps.
|
|
168
|
+
|
|
169
|
+
Parameters
|
|
170
|
+
----------
|
|
171
|
+
mob : VMobject
|
|
172
|
+
The source mobject. Must have at least 2 points.
|
|
173
|
+
n : int
|
|
174
|
+
Number of output points.
|
|
175
|
+
|
|
176
|
+
Returns
|
|
177
|
+
-------
|
|
178
|
+
np.ndarray, shape (n, 3)
|
|
179
|
+
"""
|
|
180
|
+
raw = mob.get_all_points()
|
|
181
|
+
if len(raw) == 0:
|
|
182
|
+
return np.zeros((n, 3))
|
|
183
|
+
if len(raw) == 1:
|
|
184
|
+
return np.tile(raw[0], (n, 1))
|
|
185
|
+
|
|
186
|
+
# Cumulative arc-length parameterisation
|
|
187
|
+
deltas = np.linalg.norm(np.diff(raw, axis=0), axis=1)
|
|
188
|
+
cum = np.concatenate([[0.0], np.cumsum(deltas)])
|
|
189
|
+
total = cum[-1]
|
|
190
|
+
if total < 1e-9:
|
|
191
|
+
return np.tile(raw[0], (n, 1))
|
|
192
|
+
|
|
193
|
+
ts_uniform = np.linspace(0.0, total, n)
|
|
194
|
+
out = np.zeros((n, 3))
|
|
195
|
+
for i, t in enumerate(ts_uniform):
|
|
196
|
+
idx = np.searchsorted(cum, t, side="right") - 1
|
|
197
|
+
idx = np.clip(idx, 0, len(raw) - 2)
|
|
198
|
+
seg_len = deltas[idx] if deltas[idx] > 1e-12 else 1e-12
|
|
199
|
+
frac = (t - cum[idx]) / seg_len
|
|
200
|
+
out[i] = raw[idx] + frac * (raw[idx + 1] - raw[idx])
|
|
201
|
+
return out
|
|
202
|
+
|
|
203
|
+
|
|
204
|
+
def _build_normal_curve_points(
|
|
205
|
+
mu: float,
|
|
206
|
+
sigma: float,
|
|
207
|
+
x_range: Tuple[float, float],
|
|
208
|
+
n_pts: int,
|
|
209
|
+
y_pos: float,
|
|
210
|
+
scale: float,
|
|
211
|
+
) -> np.ndarray:
|
|
212
|
+
"""Sample a Normal(μ, σ) PDF curve in 3D scene coordinates.
|
|
213
|
+
|
|
214
|
+
Parameters
|
|
215
|
+
----------
|
|
216
|
+
mu, sigma : float
|
|
217
|
+
x_range : (x_min, x_max)
|
|
218
|
+
n_pts : int
|
|
219
|
+
y_pos : float
|
|
220
|
+
Y coordinate (depth).
|
|
221
|
+
scale : float
|
|
222
|
+
Vertical scale factor (z units per PDF unit).
|
|
223
|
+
|
|
224
|
+
Returns
|
|
225
|
+
-------
|
|
226
|
+
np.ndarray, shape (n_pts, 3)
|
|
227
|
+
"""
|
|
228
|
+
xs = np.linspace(x_range[0], x_range[1], n_pts)
|
|
229
|
+
zs = np.exp(-0.5 * ((xs - mu) / sigma) ** 2) / (sigma * np.sqrt(TAU))
|
|
230
|
+
zs = zs * scale
|
|
231
|
+
return np.column_stack([xs, np.full(n_pts, y_pos), zs])
|
|
232
|
+
|
|
233
|
+
|
|
234
|
+
def _build_kde_curve_points(
|
|
235
|
+
data: np.ndarray,
|
|
236
|
+
x_range: Tuple[float, float],
|
|
237
|
+
n_pts: int,
|
|
238
|
+
y_pos: float,
|
|
239
|
+
scale: float,
|
|
240
|
+
bandwidth: Optional[float] = None,
|
|
241
|
+
) -> np.ndarray:
|
|
242
|
+
"""Evaluate a manual Gaussian KDE over *data* in 3D scene coordinates.
|
|
243
|
+
|
|
244
|
+
No scipy dependency. Uses Silverman's rule-of-thumb for bandwidth
|
|
245
|
+
if *bandwidth* is None.
|
|
246
|
+
|
|
247
|
+
Returns
|
|
248
|
+
-------
|
|
249
|
+
np.ndarray, shape (n_pts, 3)
|
|
250
|
+
"""
|
|
251
|
+
n = len(data)
|
|
252
|
+
if n == 0:
|
|
253
|
+
return np.zeros((n_pts, 3))
|
|
254
|
+
|
|
255
|
+
if bandwidth is None:
|
|
256
|
+
std = float(np.std(data))
|
|
257
|
+
bw = 1.06 * std * n ** (-0.2) if std > 0 else 0.1
|
|
258
|
+
else:
|
|
259
|
+
bw = bandwidth
|
|
260
|
+
|
|
261
|
+
xs = np.linspace(x_range[0], x_range[1], n_pts)
|
|
262
|
+
zs = np.array([
|
|
263
|
+
np.mean(np.exp(-0.5 * ((x - data) / bw) ** 2) / (bw * np.sqrt(TAU)))
|
|
264
|
+
for x in xs
|
|
265
|
+
])
|
|
266
|
+
z_max = zs.max()
|
|
267
|
+
if z_max > 0:
|
|
268
|
+
zs = zs / z_max * scale
|
|
269
|
+
return np.column_stack([xs, np.full(n_pts, y_pos), zs])
|
|
270
|
+
|
|
271
|
+
|
|
272
|
+
def _bar_heights_from_group(bar_group: VGroup) -> List[float]:
|
|
273
|
+
"""Extract the z-height of each bar in a BarChart3D bar_group.
|
|
274
|
+
|
|
275
|
+
Works by reading the z-coordinate of the top-left corner of each
|
|
276
|
+
bar's front face polygon. Assumes bars are ``VGroup(front, right, top)``.
|
|
277
|
+
|
|
278
|
+
Parameters
|
|
279
|
+
----------
|
|
280
|
+
bar_group : VGroup
|
|
281
|
+
The ``bar_group`` attribute of a ``BarChart3D``.
|
|
282
|
+
|
|
283
|
+
Returns
|
|
284
|
+
-------
|
|
285
|
+
list of float
|
|
286
|
+
One height per bar.
|
|
287
|
+
"""
|
|
288
|
+
heights = []
|
|
289
|
+
for bar in bar_group:
|
|
290
|
+
# bar is VGroup(face_front, face_right, face_top)
|
|
291
|
+
# face_front corners: BL, BR, TR, TL — z of TR = bar height
|
|
292
|
+
try:
|
|
293
|
+
pts = bar[0].get_all_points()
|
|
294
|
+
if len(pts) >= 3:
|
|
295
|
+
heights.append(float(pts[2][2])) # third point = top-right
|
|
296
|
+
else:
|
|
297
|
+
heights.append(0.0)
|
|
298
|
+
except (IndexError, AttributeError):
|
|
299
|
+
heights.append(0.0)
|
|
300
|
+
return heights
|
|
301
|
+
|
|
302
|
+
|
|
303
|
+
# ============================================================================
|
|
304
|
+
# 1. Curve morphing
|
|
305
|
+
# ============================================================================
|
|
306
|
+
|
|
307
|
+
class DistMorph3D:
|
|
308
|
+
"""Morph one distribution curve into another point-by-point.
|
|
309
|
+
|
|
310
|
+
Both curves are resampled to *n_interp_pts* points via arc-length
|
|
311
|
+
parameterisation so the interpolation is smooth and free of
|
|
312
|
+
topology jumps (no sudden re-routing of the path).
|
|
313
|
+
|
|
314
|
+
The morph works on the raw ``VMobject`` (the stroke line), not on any
|
|
315
|
+
wrapper class, so it is agnostic of chart type. Pass
|
|
316
|
+
``some_lineseries.stroke`` directly.
|
|
317
|
+
|
|
318
|
+
Parameters
|
|
319
|
+
----------
|
|
320
|
+
source : VMobject
|
|
321
|
+
The curve to morph *from*. Modified in place during animation.
|
|
322
|
+
target : VMobject
|
|
323
|
+
The curve to morph *to*. Not added to the scene; used only
|
|
324
|
+
for its point geometry.
|
|
325
|
+
n_interp_pts : int
|
|
326
|
+
Number of resampled points. Higher = smoother but heavier.
|
|
327
|
+
Default 300 is adequate for most distribution curves.
|
|
328
|
+
color_start : ManimColor or None
|
|
329
|
+
Starting stroke colour. ``None`` preserves *source*'s current colour.
|
|
330
|
+
color_end : ManimColor or None
|
|
331
|
+
Ending stroke colour. ``None`` preserves *source*'s current colour.
|
|
332
|
+
rate_func : Callable
|
|
333
|
+
Manim rate function for the morph.
|
|
334
|
+
"""
|
|
335
|
+
|
|
336
|
+
def __init__(
|
|
337
|
+
self,
|
|
338
|
+
source: VMobject,
|
|
339
|
+
target: VMobject,
|
|
340
|
+
n_interp_pts: int = 300,
|
|
341
|
+
color_start: Optional[ManimColor] = None,
|
|
342
|
+
color_end: Optional[ManimColor] = None,
|
|
343
|
+
rate_func: Callable = rate_functions.ease_in_out_cubic,
|
|
344
|
+
):
|
|
345
|
+
self.source = source
|
|
346
|
+
self._src_pts = _resample_vmobject_points(source, n_interp_pts)
|
|
347
|
+
self._tgt_pts = _resample_vmobject_points(target, n_interp_pts)
|
|
348
|
+
self._n = n_interp_pts
|
|
349
|
+
self._c0 = color_start
|
|
350
|
+
self._c1 = color_end
|
|
351
|
+
self._rate = rate_func
|
|
352
|
+
|
|
353
|
+
def build(self, run_time: float = 1.5) -> UpdateFromAlphaFunc:
|
|
354
|
+
"""Return an animation that morphs the source curve.
|
|
355
|
+
|
|
356
|
+
Parameters
|
|
357
|
+
----------
|
|
358
|
+
run_time : float
|
|
359
|
+
|
|
360
|
+
Returns
|
|
361
|
+
-------
|
|
362
|
+
UpdateFromAlphaFunc
|
|
363
|
+
Play with ``scene.play(morph.build())``.
|
|
364
|
+
"""
|
|
365
|
+
src = self._src_pts
|
|
366
|
+
tgt = self._tgt_pts
|
|
367
|
+
c0 = self._c0
|
|
368
|
+
c1 = self._c1
|
|
369
|
+
rate = self._rate
|
|
370
|
+
|
|
371
|
+
def updater(mob: VMobject, alpha: float) -> None:
|
|
372
|
+
t = rate(alpha)
|
|
373
|
+
pts = src + (tgt - src) * t
|
|
374
|
+
mob.set_points_as_corners(pts)
|
|
375
|
+
if c0 is not None and c1 is not None:
|
|
376
|
+
col = _lerp_color(c0, c1, t)
|
|
377
|
+
mob.set_stroke(color=col)
|
|
378
|
+
|
|
379
|
+
return UpdateFromAlphaFunc(self.source, updater, run_time=run_time)
|
|
380
|
+
|
|
381
|
+
|
|
382
|
+
class HistMorph3D:
|
|
383
|
+
"""Morph a bar chart from one set of heights to another.
|
|
384
|
+
|
|
385
|
+
Works on the ``bar_group`` VGroup of a ``BarChart3D`` object.
|
|
386
|
+
Each bar's three faces (front, right, top) are reshaped by
|
|
387
|
+
direct point manipulation — the same technique used by
|
|
388
|
+
``BarChart3D.animate_update``, but generalized here as a
|
|
389
|
+
standalone transition.
|
|
390
|
+
|
|
391
|
+
Parameters
|
|
392
|
+
----------
|
|
393
|
+
bar_group : VGroup
|
|
394
|
+
The ``bar_group`` attribute of a ``BarChart3D``.
|
|
395
|
+
new_heights : sequence of float
|
|
396
|
+
Target z-height for each bar (already in scene units — multiply
|
|
397
|
+
by ``z_scale`` before passing if needed).
|
|
398
|
+
bar_positions : list of (float, float, float, float, float)
|
|
399
|
+
Per-bar geometry: ``(x, y, hw, hd, z_floor)`` where *hw* is
|
|
400
|
+
half-width, *hd* is half-depth. If None, inferred from
|
|
401
|
+
bar face points.
|
|
402
|
+
color_map : list of ManimColor or None
|
|
403
|
+
Per-bar target colour (optional colour morph).
|
|
404
|
+
rate_func : Callable
|
|
405
|
+
"""
|
|
406
|
+
|
|
407
|
+
def __init__(
|
|
408
|
+
self,
|
|
409
|
+
bar_group: VGroup,
|
|
410
|
+
new_heights: Sequence[float],
|
|
411
|
+
bar_positions: Optional[List[Tuple[float, float, float, float, float]]] = None,
|
|
412
|
+
color_map: Optional[List[ManimColor]] = None,
|
|
413
|
+
rate_func: Callable = rate_functions.ease_in_out_cubic,
|
|
414
|
+
):
|
|
415
|
+
self.bar_group = bar_group
|
|
416
|
+
self.new_heights = list(new_heights)
|
|
417
|
+
self._rate = rate_func
|
|
418
|
+
self._color_map = color_map
|
|
419
|
+
|
|
420
|
+
# Extract current heights and geometry
|
|
421
|
+
self._old_heights = _bar_heights_from_group(bar_group)
|
|
422
|
+
|
|
423
|
+
# Extract bar geometry (x, y, hw, hd, z_floor) per bar
|
|
424
|
+
if bar_positions is not None:
|
|
425
|
+
self._geom = bar_positions
|
|
426
|
+
else:
|
|
427
|
+
self._geom = self._infer_geometry()
|
|
428
|
+
|
|
429
|
+
def _infer_geometry(self) -> List[Tuple[float, float, float, float, float]]:
|
|
430
|
+
"""Read (x, y, hw, hd, z_floor) from each bar's front face."""
|
|
431
|
+
result = []
|
|
432
|
+
for bar in self.bar_group:
|
|
433
|
+
try:
|
|
434
|
+
pts = bar[0].get_all_points() # front face
|
|
435
|
+
x0, x1 = float(pts[:, 0].min()), float(pts[:, 0].max())
|
|
436
|
+
y0 = float(pts[:, 1].min())
|
|
437
|
+
z0 = float(pts[:, 2].min())
|
|
438
|
+
x = (x0 + x1) / 2
|
|
439
|
+
hw = (x1 - x0) / 2
|
|
440
|
+
# hd from right face
|
|
441
|
+
try:
|
|
442
|
+
rpts = bar[1].get_all_points()
|
|
443
|
+
hd = (float(rpts[:, 1].max()) - float(rpts[:, 1].min())) / 2
|
|
444
|
+
y = (float(rpts[:, 1].max()) + float(rpts[:, 1].min())) / 2
|
|
445
|
+
except Exception:
|
|
446
|
+
hd, y = 0.2, y0
|
|
447
|
+
result.append((x, y, hw, hd, z0))
|
|
448
|
+
except Exception:
|
|
449
|
+
result.append((0.0, 0.0, 0.25, 0.2, 0.0))
|
|
450
|
+
return result
|
|
451
|
+
|
|
452
|
+
def build(self, run_time: float = 1.2) -> UpdateFromAlphaFunc:
|
|
453
|
+
"""Return an animation that reshapes all bars simultaneously.
|
|
454
|
+
|
|
455
|
+
Returns
|
|
456
|
+
-------
|
|
457
|
+
UpdateFromAlphaFunc
|
|
458
|
+
"""
|
|
459
|
+
old_h = list(self._old_heights)
|
|
460
|
+
new_h = list(self.new_heights)
|
|
461
|
+
geom = self._geom
|
|
462
|
+
rate = self._rate
|
|
463
|
+
bar_group = self.bar_group
|
|
464
|
+
color_map = self._color_map
|
|
465
|
+
|
|
466
|
+
def updater(mob: VGroup, alpha: float) -> None:
|
|
467
|
+
t = rate(alpha)
|
|
468
|
+
for i, bar in enumerate(mob):
|
|
469
|
+
if i >= len(old_h) or i >= len(new_h):
|
|
470
|
+
break
|
|
471
|
+
h = old_h[i] + (new_h[i] - old_h[i]) * t
|
|
472
|
+
h = max(h, 1e-3)
|
|
473
|
+
x, y, hw, hd, z0 = geom[i]
|
|
474
|
+
z1 = z0 + h
|
|
475
|
+
|
|
476
|
+
# Front face
|
|
477
|
+
try:
|
|
478
|
+
bar[0].set_points_as_corners([
|
|
479
|
+
np.array([x-hw, y-hd, z0]),
|
|
480
|
+
np.array([x+hw, y-hd, z0]),
|
|
481
|
+
np.array([x+hw, y-hd, z1]),
|
|
482
|
+
np.array([x-hw, y-hd, z1]),
|
|
483
|
+
np.array([x-hw, y-hd, z0]),
|
|
484
|
+
])
|
|
485
|
+
bar[1].set_points_as_corners([
|
|
486
|
+
np.array([x+hw, y-hd, z0]),
|
|
487
|
+
np.array([x+hw, y+hd, z0]),
|
|
488
|
+
np.array([x+hw, y+hd, z1]),
|
|
489
|
+
np.array([x+hw, y-hd, z1]),
|
|
490
|
+
np.array([x+hw, y-hd, z0]),
|
|
491
|
+
])
|
|
492
|
+
bar[2].set_points_as_corners([
|
|
493
|
+
np.array([x-hw, y-hd, z1]),
|
|
494
|
+
np.array([x+hw, y-hd, z1]),
|
|
495
|
+
np.array([x+hw, y+hd, z1]),
|
|
496
|
+
np.array([x-hw, y+hd, z1]),
|
|
497
|
+
np.array([x-hw, y-hd, z1]),
|
|
498
|
+
])
|
|
499
|
+
except (IndexError, AttributeError):
|
|
500
|
+
pass
|
|
501
|
+
|
|
502
|
+
if color_map is not None and i < len(color_map):
|
|
503
|
+
try:
|
|
504
|
+
bar[0].set_fill(color=_with_opacity(color_map[i], 0.90))
|
|
505
|
+
except Exception:
|
|
506
|
+
pass
|
|
507
|
+
|
|
508
|
+
return UpdateFromAlphaFunc(bar_group, updater, run_time=run_time)
|
|
509
|
+
|
|
510
|
+
|
|
511
|
+
# ============================================================================
|
|
512
|
+
# 2. CDF / PDF coupling
|
|
513
|
+
# ============================================================================
|
|
514
|
+
|
|
515
|
+
class CDFBuild3D:
|
|
516
|
+
"""Animate a CDF curve assembling from the left as a running integral.
|
|
517
|
+
|
|
518
|
+
Simultaneously shades the area under the PDF curve with a growing
|
|
519
|
+
fill region. The two curves must share the same x-axis.
|
|
520
|
+
|
|
521
|
+
Parameters
|
|
522
|
+
----------
|
|
523
|
+
pdf_curve : VMobject
|
|
524
|
+
The already-rendered PDF stroke (e.g. ``LineSeries3D.stroke``).
|
|
525
|
+
Used only for reading point positions — not modified.
|
|
526
|
+
cdf_curve : VMobject
|
|
527
|
+
The CDF stroke to animate building left-to-right. Modified in place.
|
|
528
|
+
area_fill : VMobject or None
|
|
529
|
+
An ``AreaFill3D`` or plain ``Polygon`` beneath the PDF. If given,
|
|
530
|
+
it will grow in sync with the CDF.
|
|
531
|
+
x_range : (float, float)
|
|
532
|
+
The data x-range used to align the sweep.
|
|
533
|
+
y_pos : float
|
|
534
|
+
Scene y-coordinate of both curves.
|
|
535
|
+
accent_color : ManimColor
|
|
536
|
+
Colour of the sweep-line indicator that tracks the integral front.
|
|
537
|
+
"""
|
|
538
|
+
|
|
539
|
+
def __init__(
|
|
540
|
+
self,
|
|
541
|
+
pdf_curve: VMobject,
|
|
542
|
+
cdf_curve: VMobject,
|
|
543
|
+
area_fill: Optional[VMobject] = None,
|
|
544
|
+
x_range: Tuple[float, float] = (-4.0, 4.0),
|
|
545
|
+
y_pos: float = 0.0,
|
|
546
|
+
accent_color: ManimColor = ManimColor("#E0AA40"),
|
|
547
|
+
):
|
|
548
|
+
self.pdf_curve = pdf_curve
|
|
549
|
+
self.cdf_curve = cdf_curve
|
|
550
|
+
self.area_fill = area_fill
|
|
551
|
+
self.x_range = x_range
|
|
552
|
+
self.y_pos = y_pos
|
|
553
|
+
self.accent_color = accent_color
|
|
554
|
+
|
|
555
|
+
# Sample the full CDF curve points
|
|
556
|
+
self._full_cdf_pts = _resample_vmobject_points(cdf_curve, 300)
|
|
557
|
+
self._full_area_pts: Optional[np.ndarray] = None
|
|
558
|
+
if area_fill is not None:
|
|
559
|
+
self._full_area_pts = _resample_vmobject_points(area_fill, 300)
|
|
560
|
+
|
|
561
|
+
def build(self, run_time: float = 2.0) -> AnimationGroup:
|
|
562
|
+
"""Return an animation that builds the CDF from left to right.
|
|
563
|
+
|
|
564
|
+
Returns
|
|
565
|
+
-------
|
|
566
|
+
AnimationGroup
|
|
567
|
+
Contains the CDF draw animation and an optional area-fill grow.
|
|
568
|
+
"""
|
|
569
|
+
full_cdf = self._full_cdf_pts
|
|
570
|
+
cdf_mob = self.cdf_curve
|
|
571
|
+
area_mob = self.area_fill
|
|
572
|
+
full_area = self._full_area_pts
|
|
573
|
+
ac = self.accent_color
|
|
574
|
+
|
|
575
|
+
# Sweep tracker dot along the CDF
|
|
576
|
+
tracker = Dot3D(
|
|
577
|
+
point=full_cdf[0] if len(full_cdf) > 0 else ORIGIN,
|
|
578
|
+
radius=0.09,
|
|
579
|
+
color=_with_opacity(ac, 0.90),
|
|
580
|
+
)
|
|
581
|
+
tracker_glow = Dot3D(
|
|
582
|
+
point=full_cdf[0] if len(full_cdf) > 0 else ORIGIN,
|
|
583
|
+
radius=0.22,
|
|
584
|
+
color=_with_opacity(ac, 0.18),
|
|
585
|
+
)
|
|
586
|
+
|
|
587
|
+
def cdf_updater(mob: VMobject, alpha: float) -> None:
|
|
588
|
+
t = smooth(alpha)
|
|
589
|
+
n_show = max(2, int(t * len(full_cdf)))
|
|
590
|
+
pts = full_cdf[:n_show]
|
|
591
|
+
mob.set_points_as_corners(pts)
|
|
592
|
+
|
|
593
|
+
# Move tracker to current front
|
|
594
|
+
front = full_cdf[n_show - 1]
|
|
595
|
+
tracker.move_to(front)
|
|
596
|
+
tracker_glow.move_to(front)
|
|
597
|
+
|
|
598
|
+
def area_updater(mob: VMobject, alpha: float) -> None:
|
|
599
|
+
if full_area is None:
|
|
600
|
+
return
|
|
601
|
+
t = smooth(alpha)
|
|
602
|
+
n_show = max(2, int(t * len(full_area)))
|
|
603
|
+
pts = full_area[:n_show]
|
|
604
|
+
mob.set_points_as_corners(pts)
|
|
605
|
+
|
|
606
|
+
anims: List = [UpdateFromAlphaFunc(cdf_mob, cdf_updater, run_time=run_time)]
|
|
607
|
+
|
|
608
|
+
if area_mob is not None:
|
|
609
|
+
anims.append(UpdateFromAlphaFunc(area_mob, area_updater, run_time=run_time))
|
|
610
|
+
|
|
611
|
+
return AnimationGroup(
|
|
612
|
+
FadeIn(tracker, run_time=0.2),
|
|
613
|
+
FadeIn(tracker_glow, run_time=0.2),
|
|
614
|
+
AnimationGroup(*anims),
|
|
615
|
+
lag_ratio=0.1,
|
|
616
|
+
)
|
|
617
|
+
|
|
618
|
+
def build_with_scene(
|
|
619
|
+
self,
|
|
620
|
+
scene: Scene,
|
|
621
|
+
run_time: float = 2.0,
|
|
622
|
+
) -> None:
|
|
623
|
+
"""Add tracker dots and play the animation directly on *scene*."""
|
|
624
|
+
full_cdf = self._full_cdf_pts
|
|
625
|
+
ac = self.accent_color
|
|
626
|
+
|
|
627
|
+
tracker = Dot3D(
|
|
628
|
+
point=full_cdf[0] if len(full_cdf) > 0 else ORIGIN,
|
|
629
|
+
radius=0.09, color=_with_opacity(ac, 0.90),
|
|
630
|
+
)
|
|
631
|
+
tracker_glow = Dot3D(
|
|
632
|
+
point=full_cdf[0] if len(full_cdf) > 0 else ORIGIN,
|
|
633
|
+
radius=0.22, color=_with_opacity(ac, 0.18),
|
|
634
|
+
)
|
|
635
|
+
scene.add(tracker, tracker_glow)
|
|
636
|
+
|
|
637
|
+
anim = self.build(run_time=run_time)
|
|
638
|
+
scene.play(anim)
|
|
639
|
+
|
|
640
|
+
scene.play(FadeOut(tracker, tracker_glow, run_time=0.3))
|
|
641
|
+
|
|
642
|
+
|
|
643
|
+
class HistToCurve3D:
|
|
644
|
+
"""Dissolve a bar histogram into a smooth distribution curve.
|
|
645
|
+
|
|
646
|
+
Simulates the effect of n → ∞ (more data, finer bins) by:
|
|
647
|
+
1. Simultaneously fading out bar edges (strokes).
|
|
648
|
+
2. Fading out bar right/top faces.
|
|
649
|
+
3. Fading in a smooth KDE/theoretical curve in their place.
|
|
650
|
+
|
|
651
|
+
Parameters
|
|
652
|
+
----------
|
|
653
|
+
bar_group : VGroup
|
|
654
|
+
Bar chart's ``bar_group``.
|
|
655
|
+
target_curve : VMobject
|
|
656
|
+
The smooth curve that emerges. Must be already constructed but
|
|
657
|
+
not yet added to the scene.
|
|
658
|
+
run_time : float
|
|
659
|
+
overlap_fraction : float
|
|
660
|
+
Fraction of run_time over which bars and curve overlap (crossfade).
|
|
661
|
+
"""
|
|
662
|
+
|
|
663
|
+
def __init__(
|
|
664
|
+
self,
|
|
665
|
+
bar_group: VGroup,
|
|
666
|
+
target_curve: VMobject,
|
|
667
|
+
run_time: float = 1.8,
|
|
668
|
+
overlap_fraction: float = 0.4,
|
|
669
|
+
):
|
|
670
|
+
self.bar_group = bar_group
|
|
671
|
+
self.target_curve = target_curve
|
|
672
|
+
self.run_time = run_time
|
|
673
|
+
self.overlap = overlap_fraction
|
|
674
|
+
|
|
675
|
+
def build(self) -> AnimationGroup:
|
|
676
|
+
"""Return the crossfade animation."""
|
|
677
|
+
bars_fade_rt = self.run_time * (1.0 - self.overlap * 0.5)
|
|
678
|
+
curve_build_rt = self.run_time * (1.0 - self.overlap * 0.5)
|
|
679
|
+
lag = self.run_time * self.overlap
|
|
680
|
+
|
|
681
|
+
def bar_fade_updater(mob: VGroup, alpha: float) -> None:
|
|
682
|
+
t = smooth(alpha)
|
|
683
|
+
for bar in mob:
|
|
684
|
+
try:
|
|
685
|
+
# Fade front face only; keep ghost outlines briefly
|
|
686
|
+
bar[0].set_fill(opacity=max(0.0, 0.88 * (1 - t)))
|
|
687
|
+
bar[1].set_fill(opacity=max(0.0, 0.85 * (1 - t)))
|
|
688
|
+
bar[2].set_fill(opacity=max(0.0, 0.80 * (1 - t)))
|
|
689
|
+
except (IndexError, AttributeError):
|
|
690
|
+
pass
|
|
691
|
+
|
|
692
|
+
return AnimationGroup(
|
|
693
|
+
UpdateFromAlphaFunc(self.bar_group, bar_fade_updater, run_time=bars_fade_rt),
|
|
694
|
+
Succession(
|
|
695
|
+
FadeIn(VGroup(), run_time=lag), # delay slot
|
|
696
|
+
Create(self.target_curve, run_time=curve_build_rt),
|
|
697
|
+
),
|
|
698
|
+
lag_ratio=0.0,
|
|
699
|
+
)
|
|
700
|
+
|
|
701
|
+
|
|
702
|
+
# ============================================================================
|
|
703
|
+
# 3. Parameter sweeps
|
|
704
|
+
# ============================================================================
|
|
705
|
+
|
|
706
|
+
class ParameterSweep3D:
|
|
707
|
+
"""Animate a distribution curve as a single parameter varies.
|
|
708
|
+
|
|
709
|
+
On each step the curve is re-evaluated from ``func(x, param)`` and
|
|
710
|
+
the existing stroke VMobject is updated in place via
|
|
711
|
+
``set_points_as_corners``. A live annotation shows the current
|
|
712
|
+
parameter value.
|
|
713
|
+
|
|
714
|
+
Parameters
|
|
715
|
+
----------
|
|
716
|
+
func : Callable[[float, float], float]
|
|
717
|
+
``func(x, param) → z`` — the distribution function.
|
|
718
|
+
x_range : (float, float, float)
|
|
719
|
+
``(x_min, x_max, x_step)`` for sampling x.
|
|
720
|
+
param_range : (float, float, float)
|
|
721
|
+
``(param_min, param_max, param_step)`` — the sweep range.
|
|
722
|
+
param_name : str
|
|
723
|
+
Human-readable parameter name shown in the annotation.
|
|
724
|
+
existing_curve : VMobject
|
|
725
|
+
The stroke to update in place. Must already be in the scene.
|
|
726
|
+
y_pos : float
|
|
727
|
+
Scene y-coordinate of the curve.
|
|
728
|
+
z_scale : float
|
|
729
|
+
Vertical scale factor (scene z per PDF unit).
|
|
730
|
+
annotation_position : np.ndarray or None
|
|
731
|
+
Where to place the live parameter readout.
|
|
732
|
+
annotation_color : ManimColor
|
|
733
|
+
annotation_font_size : int
|
|
734
|
+
"""
|
|
735
|
+
|
|
736
|
+
def __init__(
|
|
737
|
+
self,
|
|
738
|
+
func: Callable[[float, float], float],
|
|
739
|
+
x_range: Tuple[float, float, float],
|
|
740
|
+
param_range: Tuple[float, float, float],
|
|
741
|
+
param_name: str,
|
|
742
|
+
existing_curve: VMobject,
|
|
743
|
+
y_pos: float = 0.0,
|
|
744
|
+
z_scale: float = 3.5,
|
|
745
|
+
annotation_position: Optional[np.ndarray] = None,
|
|
746
|
+
annotation_color: ManimColor = ManimColor("#E0AA40"),
|
|
747
|
+
annotation_font_size: int = 22,
|
|
748
|
+
):
|
|
749
|
+
self.func = func
|
|
750
|
+
self.x_range = x_range
|
|
751
|
+
self.param_range = param_range
|
|
752
|
+
self.param_name = param_name
|
|
753
|
+
self.curve = existing_curve
|
|
754
|
+
self.y_pos = y_pos
|
|
755
|
+
self.z_scale = z_scale
|
|
756
|
+
self.ann_pos = (
|
|
757
|
+
annotation_position
|
|
758
|
+
if annotation_position is not None
|
|
759
|
+
else np.array([x_range[1] + 0.5, y_pos, z_scale * 1.1])
|
|
760
|
+
)
|
|
761
|
+
self.ann_color = annotation_color
|
|
762
|
+
self.ann_font_size = annotation_font_size
|
|
763
|
+
|
|
764
|
+
# Build the parameter sequence
|
|
765
|
+
p_min, p_max, p_step = param_range
|
|
766
|
+
self._params: np.ndarray = np.arange(p_min, p_max + p_step * 0.5, p_step)
|
|
767
|
+
|
|
768
|
+
# Build all curve point arrays up front
|
|
769
|
+
x_min, x_max, x_step = x_range
|
|
770
|
+
self._xs = np.arange(x_min, x_max + x_step * 0.5, x_step)
|
|
771
|
+
self._all_pts: List[np.ndarray] = []
|
|
772
|
+
for p in self._params:
|
|
773
|
+
zs = np.array([max(func(x, p), 0.0) for x in self._xs])
|
|
774
|
+
z_max = zs.max()
|
|
775
|
+
if z_max > 0:
|
|
776
|
+
zs = zs / z_max * z_scale
|
|
777
|
+
pts = np.column_stack([self._xs, np.full(len(self._xs), y_pos), zs])
|
|
778
|
+
self._all_pts.append(pts)
|
|
779
|
+
|
|
780
|
+
# ------------------------------------------------------------------
|
|
781
|
+
|
|
782
|
+
def run(
|
|
783
|
+
self,
|
|
784
|
+
scene: Scene,
|
|
785
|
+
run_time_total: float = 3.0,
|
|
786
|
+
hold_at_ends: float = 0.5,
|
|
787
|
+
show_annotation: bool = True,
|
|
788
|
+
) -> None:
|
|
789
|
+
"""Sweep the parameter and update the curve in real time.
|
|
790
|
+
|
|
791
|
+
Plays animations directly on *scene*.
|
|
792
|
+
|
|
793
|
+
Parameters
|
|
794
|
+
----------
|
|
795
|
+
run_time_total : float
|
|
796
|
+
Total duration of the entire sweep.
|
|
797
|
+
hold_at_ends : float
|
|
798
|
+
Extra hold time at the first and last parameter values.
|
|
799
|
+
show_annotation : bool
|
|
800
|
+
Whether to display and update the parameter readout.
|
|
801
|
+
"""
|
|
802
|
+
n_steps = len(self._params)
|
|
803
|
+
rt_per_step = (run_time_total - 2 * hold_at_ends) / max(n_steps - 1, 1)
|
|
804
|
+
|
|
805
|
+
# Build annotation label
|
|
806
|
+
ann_label: Optional[Text] = None
|
|
807
|
+
if show_annotation:
|
|
808
|
+
ann_label = Text(
|
|
809
|
+
f"{self.param_name} = {self._params[0]:.3f}",
|
|
810
|
+
font_size=self.ann_font_size,
|
|
811
|
+
color=self.ann_color,
|
|
812
|
+
)
|
|
813
|
+
ann_label.move_to(self.ann_pos)
|
|
814
|
+
scene.add_fixed_orientation_mobjects(ann_label)
|
|
815
|
+
scene.add(ann_label)
|
|
816
|
+
scene.play(FadeIn(ann_label, run_time=0.2))
|
|
817
|
+
|
|
818
|
+
# Hold at first parameter
|
|
819
|
+
scene.wait(hold_at_ends)
|
|
820
|
+
|
|
821
|
+
for i in range(n_steps - 1):
|
|
822
|
+
src_pts = self._all_pts[i]
|
|
823
|
+
tgt_pts = self._all_pts[i + 1]
|
|
824
|
+
next_param = self._params[i + 1]
|
|
825
|
+
curve_ref = self.curve
|
|
826
|
+
|
|
827
|
+
def make_step_updater(sp, tp):
|
|
828
|
+
def updater(mob: VMobject, alpha: float) -> None:
|
|
829
|
+
t = smooth(alpha)
|
|
830
|
+
pts = sp + (tp - sp) * t
|
|
831
|
+
mob.set_points_as_corners(pts)
|
|
832
|
+
return updater
|
|
833
|
+
|
|
834
|
+
step_anim = UpdateFromAlphaFunc(
|
|
835
|
+
curve_ref,
|
|
836
|
+
make_step_updater(src_pts, tgt_pts),
|
|
837
|
+
run_time=rt_per_step,
|
|
838
|
+
)
|
|
839
|
+
|
|
840
|
+
if ann_label is not None:
|
|
841
|
+
# Build new label for next param step
|
|
842
|
+
new_lbl = Text(
|
|
843
|
+
f"{self.param_name} = {next_param:.3f}",
|
|
844
|
+
font_size=self.ann_font_size,
|
|
845
|
+
color=self.ann_color,
|
|
846
|
+
)
|
|
847
|
+
new_lbl.move_to(self.ann_pos)
|
|
848
|
+
scene.add_fixed_orientation_mobjects(new_lbl)
|
|
849
|
+
|
|
850
|
+
scene.play(
|
|
851
|
+
step_anim,
|
|
852
|
+
Transform(ann_label, new_lbl, run_time=rt_per_step),
|
|
853
|
+
)
|
|
854
|
+
else:
|
|
855
|
+
scene.play(step_anim)
|
|
856
|
+
|
|
857
|
+
# Hold at final parameter
|
|
858
|
+
scene.wait(hold_at_ends)
|
|
859
|
+
|
|
860
|
+
if ann_label is not None:
|
|
861
|
+
scene.play(FadeOut(ann_label, run_time=0.25))
|
|
862
|
+
|
|
863
|
+
def build_single_step(
|
|
864
|
+
self,
|
|
865
|
+
from_index: int,
|
|
866
|
+
to_index: int,
|
|
867
|
+
run_time: float = 0.5,
|
|
868
|
+
) -> UpdateFromAlphaFunc:
|
|
869
|
+
"""Return an animation for one step of the sweep.
|
|
870
|
+
|
|
871
|
+
Useful when the caller wants manual control over the sequence.
|
|
872
|
+
"""
|
|
873
|
+
src = self._all_pts[from_index]
|
|
874
|
+
tgt = self._all_pts[to_index]
|
|
875
|
+
curve = self.curve
|
|
876
|
+
|
|
877
|
+
def updater(mob: VMobject, alpha: float) -> None:
|
|
878
|
+
t = smooth(alpha)
|
|
879
|
+
mob.set_points_as_corners(src + (tgt - src) * t)
|
|
880
|
+
|
|
881
|
+
return UpdateFromAlphaFunc(curve, updater, run_time=run_time)
|
|
882
|
+
|
|
883
|
+
|
|
884
|
+
# ============================================================================
|
|
885
|
+
# 4. Scene transitions
|
|
886
|
+
# ============================================================================
|
|
887
|
+
|
|
888
|
+
class CurtainReveal3D:
|
|
889
|
+
"""A translucent plane sweeps across a VGroup, revealing it.
|
|
890
|
+
|
|
891
|
+
The curtain is a flat polygon in the yz-plane that travels from
|
|
892
|
+
``x_start`` to ``x_end``. As it passes each element, that element
|
|
893
|
+
fades in, creating a left-to-right reveal.
|
|
894
|
+
|
|
895
|
+
Parameters
|
|
896
|
+
----------
|
|
897
|
+
target : VGroup
|
|
898
|
+
The group to reveal.
|
|
899
|
+
x_start : float
|
|
900
|
+
Starting x position of the curtain (usually left edge of target).
|
|
901
|
+
x_end : float
|
|
902
|
+
Ending x position of the curtain (usually right edge of target).
|
|
903
|
+
y_range : (float, float)
|
|
904
|
+
Vertical (y) extent of the curtain plane.
|
|
905
|
+
z_range : (float, float)
|
|
906
|
+
Height (z) extent of the curtain plane.
|
|
907
|
+
curtain_color : ManimColor
|
|
908
|
+
curtain_opacity : float
|
|
909
|
+
reveal_mode : str
|
|
910
|
+
``"left_to_right"`` | ``"right_to_left"`` | ``"bottom_to_top"``
|
|
911
|
+
| ``"top_to_bottom"``.
|
|
912
|
+
"""
|
|
913
|
+
|
|
914
|
+
def __init__(
|
|
915
|
+
self,
|
|
916
|
+
target: VGroup,
|
|
917
|
+
x_start: float = -4.0,
|
|
918
|
+
x_end: float = 4.0,
|
|
919
|
+
y_range: Tuple[float, float] = (-1.0, 1.0),
|
|
920
|
+
z_range: Tuple[float, float] = (0.0, 5.0),
|
|
921
|
+
curtain_color: ManimColor = ManimColor("#4A90D9"),
|
|
922
|
+
curtain_opacity: float = 0.18,
|
|
923
|
+
reveal_mode: str = "left_to_right",
|
|
924
|
+
):
|
|
925
|
+
self.target = target
|
|
926
|
+
self.x_start = x_start
|
|
927
|
+
self.x_end = x_end
|
|
928
|
+
self.y_range = y_range
|
|
929
|
+
self.z_range = z_range
|
|
930
|
+
self.curtain_color = curtain_color
|
|
931
|
+
self.curtain_opacity = curtain_opacity
|
|
932
|
+
self.mode = reveal_mode
|
|
933
|
+
|
|
934
|
+
def build(self, run_time: float = 1.5) -> AnimationGroup:
|
|
935
|
+
"""Return the curtain reveal animation.
|
|
936
|
+
|
|
937
|
+
The target is set to opacity 0 initially; the animation fades in
|
|
938
|
+
each sub-element as the curtain passes over it.
|
|
939
|
+
|
|
940
|
+
Returns
|
|
941
|
+
-------
|
|
942
|
+
AnimationGroup
|
|
943
|
+
"""
|
|
944
|
+
target = self.target
|
|
945
|
+
x0, x1 = self.x_start, self.x_end
|
|
946
|
+
y0, y1 = self.y_range
|
|
947
|
+
z0, z1 = self.z_range
|
|
948
|
+
|
|
949
|
+
# Build the curtain plane
|
|
950
|
+
curtain = Polygon(
|
|
951
|
+
np.array([x0, y0, z0]),
|
|
952
|
+
np.array([x0, y1, z0]),
|
|
953
|
+
np.array([x0, y1, z1]),
|
|
954
|
+
np.array([x0, y0, z1]),
|
|
955
|
+
fill_color=_with_opacity(self.curtain_color, self.curtain_opacity),
|
|
956
|
+
fill_opacity=1.0,
|
|
957
|
+
stroke_width=0,
|
|
958
|
+
)
|
|
959
|
+
|
|
960
|
+
# Hide target initially
|
|
961
|
+
target.set_opacity(0)
|
|
962
|
+
|
|
963
|
+
def curtain_updater(mob: Polygon, alpha: float) -> None:
|
|
964
|
+
t = smooth(alpha)
|
|
965
|
+
x_cur = x0 + (x1 - x0) * t
|
|
966
|
+
mob.set_points_as_corners([
|
|
967
|
+
np.array([x_cur, y0, z0]),
|
|
968
|
+
np.array([x_cur, y1, z0]),
|
|
969
|
+
np.array([x_cur, y1, z1]),
|
|
970
|
+
np.array([x_cur, y0, z1]),
|
|
971
|
+
np.array([x_cur, y0, z0]),
|
|
972
|
+
])
|
|
973
|
+
# Reveal target elements that the curtain has passed
|
|
974
|
+
for sub in target:
|
|
975
|
+
try:
|
|
976
|
+
sub_x = sub.get_center()[0]
|
|
977
|
+
revealed = sub_x <= x_cur
|
|
978
|
+
sub.set_opacity(1.0 if revealed else 0.0)
|
|
979
|
+
except Exception:
|
|
980
|
+
pass
|
|
981
|
+
|
|
982
|
+
return AnimationGroup(
|
|
983
|
+
FadeIn(curtain, run_time=0.2),
|
|
984
|
+
UpdateFromAlphaFunc(curtain, curtain_updater, run_time=run_time),
|
|
985
|
+
FadeOut(curtain, run_time=0.25),
|
|
986
|
+
lag_ratio=0.05,
|
|
987
|
+
)
|
|
988
|
+
|
|
989
|
+
|
|
990
|
+
class SceneWipe3D:
|
|
991
|
+
"""Wipe between two VGroups using a sweeping divider plane.
|
|
992
|
+
|
|
993
|
+
The divider plane travels from one side of the scene to the other.
|
|
994
|
+
Group A fades out behind the wipe; Group B fades in ahead of it.
|
|
995
|
+
Useful for transitioning between two different distribution views.
|
|
996
|
+
|
|
997
|
+
Parameters
|
|
998
|
+
----------
|
|
999
|
+
group_out : VGroup
|
|
1000
|
+
The current scene content (fades out behind the wipe).
|
|
1001
|
+
group_in : VGroup
|
|
1002
|
+
The new scene content (fades in ahead of the wipe).
|
|
1003
|
+
direction : str
|
|
1004
|
+
``"right"`` | ``"left"`` | ``"up"`` | ``"down"``.
|
|
1005
|
+
divider_color : ManimColor
|
|
1006
|
+
divider_opacity : float
|
|
1007
|
+
scene_width : float
|
|
1008
|
+
Total scene width in scene units.
|
|
1009
|
+
scene_height : float
|
|
1010
|
+
Total scene height in scene units.
|
|
1011
|
+
"""
|
|
1012
|
+
|
|
1013
|
+
def __init__(
|
|
1014
|
+
self,
|
|
1015
|
+
group_out: VGroup,
|
|
1016
|
+
group_in: VGroup,
|
|
1017
|
+
direction: str = "right",
|
|
1018
|
+
divider_color: ManimColor = ManimColor("#E0AA40"),
|
|
1019
|
+
divider_opacity: float = 0.55,
|
|
1020
|
+
scene_width: float = 14.0,
|
|
1021
|
+
scene_height: float = 8.0,
|
|
1022
|
+
):
|
|
1023
|
+
self.group_out = group_out
|
|
1024
|
+
self.group_in = group_in
|
|
1025
|
+
self.direction = direction
|
|
1026
|
+
self.div_color = divider_color
|
|
1027
|
+
self.div_opacity = divider_opacity
|
|
1028
|
+
self.sw = scene_width
|
|
1029
|
+
self.sh = scene_height
|
|
1030
|
+
|
|
1031
|
+
def build(self, run_time: float = 1.4) -> AnimationGroup:
|
|
1032
|
+
"""Return the wipe transition animation."""
|
|
1033
|
+
g_out = self.group_out
|
|
1034
|
+
g_in = self.group_in
|
|
1035
|
+
sw, sh = self.sw, self.sh
|
|
1036
|
+
col = _with_opacity(self.div_color, self.div_opacity)
|
|
1037
|
+
|
|
1038
|
+
# Build divider as a thin vertical rectangle
|
|
1039
|
+
if self.direction in ("right", "left"):
|
|
1040
|
+
x_start = -sw / 2 if self.direction == "right" else sw / 2
|
|
1041
|
+
x_end = sw / 2 if self.direction == "right" else -sw / 2
|
|
1042
|
+
divider = Polygon(
|
|
1043
|
+
np.array([x_start, -sh / 2, 0]),
|
|
1044
|
+
np.array([x_start, sh / 2, 0]),
|
|
1045
|
+
np.array([x_start, sh / 2, sh]),
|
|
1046
|
+
np.array([x_start, -sh / 2, sh]),
|
|
1047
|
+
fill_color=col, fill_opacity=1.0, stroke_width=0,
|
|
1048
|
+
)
|
|
1049
|
+
else:
|
|
1050
|
+
x_start = -sw / 2
|
|
1051
|
+
x_end = sw / 2
|
|
1052
|
+
divider = Polygon(
|
|
1053
|
+
np.array([-sw/2, -sh/2, 0]),
|
|
1054
|
+
np.array([ sw/2, -sh/2, 0]),
|
|
1055
|
+
fill_color=col, fill_opacity=1.0, stroke_width=0,
|
|
1056
|
+
)
|
|
1057
|
+
|
|
1058
|
+
g_in.set_opacity(0)
|
|
1059
|
+
sign = 1 if self.direction == "right" else -1
|
|
1060
|
+
|
|
1061
|
+
def wipe_updater(mob: Polygon, alpha: float) -> None:
|
|
1062
|
+
t = smooth(alpha)
|
|
1063
|
+
x_cur = x_start + (x_end - x_start) * t
|
|
1064
|
+
mob.set_points_as_corners([
|
|
1065
|
+
np.array([x_cur, -sh/2, 0]),
|
|
1066
|
+
np.array([x_cur, sh/2, 0]),
|
|
1067
|
+
np.array([x_cur, sh/2, sh]),
|
|
1068
|
+
np.array([x_cur, -sh/2, sh]),
|
|
1069
|
+
np.array([x_cur, -sh/2, 0]),
|
|
1070
|
+
])
|
|
1071
|
+
# Out fades behind wipe, in reveals ahead
|
|
1072
|
+
for sub in g_out:
|
|
1073
|
+
try:
|
|
1074
|
+
sx = sub.get_center()[0]
|
|
1075
|
+
sub.set_opacity(max(0.0, 1.0 - max(0, sign * (sx - x_cur)) * 4.0))
|
|
1076
|
+
except Exception:
|
|
1077
|
+
pass
|
|
1078
|
+
for sub in g_in:
|
|
1079
|
+
try:
|
|
1080
|
+
sx = sub.get_center()[0]
|
|
1081
|
+
sub.set_opacity(max(0.0, min(1.0, sign * (x_cur - sx) * 4.0)))
|
|
1082
|
+
except Exception:
|
|
1083
|
+
pass
|
|
1084
|
+
|
|
1085
|
+
return AnimationGroup(
|
|
1086
|
+
FadeIn(divider, run_time=0.15),
|
|
1087
|
+
UpdateFromAlphaFunc(divider, wipe_updater, run_time=run_time),
|
|
1088
|
+
FadeOut(divider, run_time=0.20),
|
|
1089
|
+
lag_ratio=0.05,
|
|
1090
|
+
)
|
|
1091
|
+
|
|
1092
|
+
|
|
1093
|
+
# ============================================================================
|
|
1094
|
+
# 5. Camera transitions
|
|
1095
|
+
# ============================================================================
|
|
1096
|
+
|
|
1097
|
+
class FocusZoom3D:
|
|
1098
|
+
"""Smoothly reposition the camera to focus on a region of interest.
|
|
1099
|
+
|
|
1100
|
+
Moves the camera to a new ``(phi, theta)`` orientation while also
|
|
1101
|
+
adjusting the focal point so a target 3D position is centred.
|
|
1102
|
+
|
|
1103
|
+
Parameters
|
|
1104
|
+
----------
|
|
1105
|
+
target_phi : float
|
|
1106
|
+
Destination polar angle (radians).
|
|
1107
|
+
target_theta : float
|
|
1108
|
+
Destination azimuthal angle (radians).
|
|
1109
|
+
target_zoom : float
|
|
1110
|
+
Destination zoom level (1.0 = default).
|
|
1111
|
+
focus_point : np.ndarray or None
|
|
1112
|
+
3D point to centre the camera on. If None, camera stays centred.
|
|
1113
|
+
run_time : float
|
|
1114
|
+
|
|
1115
|
+
Notes
|
|
1116
|
+
-----
|
|
1117
|
+
Camera moves in ThreeDScene use ``scene.move_camera``. Call
|
|
1118
|
+
``focus.run(scene)`` rather than ``scene.play(focus.build())``.
|
|
1119
|
+
"""
|
|
1120
|
+
|
|
1121
|
+
def __init__(
|
|
1122
|
+
self,
|
|
1123
|
+
target_phi: float = 65 * DEGREES,
|
|
1124
|
+
target_theta: float = -45 * DEGREES,
|
|
1125
|
+
target_zoom: float = 1.0,
|
|
1126
|
+
focus_point: Optional[np.ndarray] = None,
|
|
1127
|
+
run_time: float = 1.5,
|
|
1128
|
+
):
|
|
1129
|
+
self.phi = target_phi
|
|
1130
|
+
self.theta = target_theta
|
|
1131
|
+
self.zoom = target_zoom
|
|
1132
|
+
self.focus = focus_point
|
|
1133
|
+
self.run_time = run_time
|
|
1134
|
+
|
|
1135
|
+
def run(self, scene: Scene, added_anims: Optional[List] = None) -> None:
|
|
1136
|
+
"""Reposition the camera directly on *scene*.
|
|
1137
|
+
|
|
1138
|
+
Parameters
|
|
1139
|
+
----------
|
|
1140
|
+
added_anims : list or None
|
|
1141
|
+
Additional animations to play simultaneously with the camera move.
|
|
1142
|
+
"""
|
|
1143
|
+
move_kwargs: Dict = dict(
|
|
1144
|
+
phi=self.phi,
|
|
1145
|
+
theta=self.theta,
|
|
1146
|
+
zoom=self.zoom,
|
|
1147
|
+
run_time=self.run_time,
|
|
1148
|
+
)
|
|
1149
|
+
if self.focus is not None:
|
|
1150
|
+
move_kwargs["frame_center"] = self.focus
|
|
1151
|
+
|
|
1152
|
+
scene.move_camera(
|
|
1153
|
+
added_anims=added_anims or [],
|
|
1154
|
+
**move_kwargs,
|
|
1155
|
+
)
|
|
1156
|
+
|
|
1157
|
+
|
|
1158
|
+
class OrbitTransition:
|
|
1159
|
+
"""Orbit the camera around the scene by a given angular displacement.
|
|
1160
|
+
|
|
1161
|
+
Parameters
|
|
1162
|
+
----------
|
|
1163
|
+
delta_theta : float
|
|
1164
|
+
Change in azimuthal angle (radians). Positive = orbit right.
|
|
1165
|
+
delta_phi : float
|
|
1166
|
+
Change in polar angle (radians). Positive = tilt down.
|
|
1167
|
+
run_time : float
|
|
1168
|
+
rate_func : Callable
|
|
1169
|
+
"""
|
|
1170
|
+
|
|
1171
|
+
def __init__(
|
|
1172
|
+
self,
|
|
1173
|
+
delta_theta: float = PI / 4,
|
|
1174
|
+
delta_phi: float = 0.0,
|
|
1175
|
+
run_time: float = 2.0,
|
|
1176
|
+
rate_func: Callable = rate_functions.ease_in_out_sine,
|
|
1177
|
+
):
|
|
1178
|
+
self.d_theta = delta_theta
|
|
1179
|
+
self.d_phi = delta_phi
|
|
1180
|
+
self.run_time = run_time
|
|
1181
|
+
self.rate_func = rate_func
|
|
1182
|
+
|
|
1183
|
+
def run(
|
|
1184
|
+
self,
|
|
1185
|
+
scene: Scene,
|
|
1186
|
+
added_anims: Optional[List] = None,
|
|
1187
|
+
) -> None:
|
|
1188
|
+
"""Orbit directly on *scene*, optionally with other simultaneous anims."""
|
|
1189
|
+
try:
|
|
1190
|
+
cur_phi = scene.camera.phi
|
|
1191
|
+
cur_theta = scene.camera.theta
|
|
1192
|
+
except AttributeError:
|
|
1193
|
+
cur_phi = 65 * DEGREES
|
|
1194
|
+
cur_theta = -45 * DEGREES
|
|
1195
|
+
|
|
1196
|
+
scene.move_camera(
|
|
1197
|
+
phi=cur_phi + self.d_phi,
|
|
1198
|
+
theta=cur_theta + self.d_theta,
|
|
1199
|
+
run_time=self.run_time,
|
|
1200
|
+
rate_func=self.rate_func,
|
|
1201
|
+
added_anims=added_anims or [],
|
|
1202
|
+
)
|
|
1203
|
+
|
|
1204
|
+
|
|
1205
|
+
# ============================================================================
|
|
1206
|
+
# 6. Statistical reveals
|
|
1207
|
+
# ============================================================================
|
|
1208
|
+
|
|
1209
|
+
class CIBuild3D:
|
|
1210
|
+
"""Animate a confidence interval extending symmetrically from the mean.
|
|
1211
|
+
|
|
1212
|
+
Builds a CI visualisation in three beats:
|
|
1213
|
+
Beat 1 – Point estimate appears (dot at x̄).
|
|
1214
|
+
Beat 2 – Error bars extend outward left and right.
|
|
1215
|
+
Beat 3 – Shaded interval band fades in between the bars.
|
|
1216
|
+
|
|
1217
|
+
Parameters
|
|
1218
|
+
----------
|
|
1219
|
+
mean_pos : np.ndarray
|
|
1220
|
+
3D position of the point estimate (x̄).
|
|
1221
|
+
half_width : float
|
|
1222
|
+
Half-width of the CI in scene units.
|
|
1223
|
+
y_pos : float
|
|
1224
|
+
Scene y-coordinate.
|
|
1225
|
+
ci_color : ManimColor
|
|
1226
|
+
point_color : ManimColor
|
|
1227
|
+
bar_stroke_width : float
|
|
1228
|
+
band_opacity : float
|
|
1229
|
+
ci_label : str or None
|
|
1230
|
+
Text label (e.g. ``"95% CI"``).
|
|
1231
|
+
scene : ThreeDScene or None
|
|
1232
|
+
"""
|
|
1233
|
+
|
|
1234
|
+
def __init__(
|
|
1235
|
+
self,
|
|
1236
|
+
mean_pos: np.ndarray,
|
|
1237
|
+
half_width: float,
|
|
1238
|
+
y_pos: float = 0.0,
|
|
1239
|
+
ci_color: ManimColor = ManimColor("#2DAA6E"),
|
|
1240
|
+
point_color: ManimColor = WHITE,
|
|
1241
|
+
bar_stroke_width: float = 2.5,
|
|
1242
|
+
band_opacity: float = 0.20,
|
|
1243
|
+
ci_label: Optional[str] = None,
|
|
1244
|
+
scene: Optional[Scene] = None,
|
|
1245
|
+
):
|
|
1246
|
+
self.mean_pos = np.array(mean_pos, dtype=float)
|
|
1247
|
+
self.half_width = half_width
|
|
1248
|
+
self.y_pos = y_pos
|
|
1249
|
+
self.ci_color = ci_color
|
|
1250
|
+
self.point_color = point_color
|
|
1251
|
+
self.bar_sw = bar_stroke_width
|
|
1252
|
+
self.band_opacity = band_opacity
|
|
1253
|
+
self.ci_label = ci_label
|
|
1254
|
+
self._scene = scene
|
|
1255
|
+
|
|
1256
|
+
mx, my, mz = self.mean_pos
|
|
1257
|
+
col = _with_opacity(ci_color, 0.85)
|
|
1258
|
+
col_band = _with_opacity(ci_color, band_opacity)
|
|
1259
|
+
|
|
1260
|
+
# Point estimate dot
|
|
1261
|
+
self.dot = Dot3D(
|
|
1262
|
+
point=self.mean_pos, radius=0.10,
|
|
1263
|
+
color=_with_opacity(point_color, 0.95),
|
|
1264
|
+
)
|
|
1265
|
+
self.dot_glow = Dot3D(
|
|
1266
|
+
point=self.mean_pos, radius=0.25,
|
|
1267
|
+
color=_with_opacity(point_color, 0.15),
|
|
1268
|
+
)
|
|
1269
|
+
|
|
1270
|
+
# Left error bar: vertical cap + horizontal arm
|
|
1271
|
+
left_x = mx - half_width
|
|
1272
|
+
right_x = mx + half_width
|
|
1273
|
+
cap_h = 0.18
|
|
1274
|
+
|
|
1275
|
+
self.left_cap = Line(
|
|
1276
|
+
np.array([left_x, y_pos, mz - cap_h]),
|
|
1277
|
+
np.array([left_x, y_pos, mz + cap_h]),
|
|
1278
|
+
color=col, stroke_width=bar_stroke_width,
|
|
1279
|
+
)
|
|
1280
|
+
self.right_cap = Line(
|
|
1281
|
+
np.array([right_x, y_pos, mz - cap_h]),
|
|
1282
|
+
np.array([right_x, y_pos, mz + cap_h]),
|
|
1283
|
+
color=col, stroke_width=bar_stroke_width,
|
|
1284
|
+
)
|
|
1285
|
+
self.arm = Line(
|
|
1286
|
+
np.array([left_x, y_pos, mz]),
|
|
1287
|
+
np.array([right_x, y_pos, mz]),
|
|
1288
|
+
color=col, stroke_width=bar_stroke_width * 0.7,
|
|
1289
|
+
)
|
|
1290
|
+
|
|
1291
|
+
# Shaded band
|
|
1292
|
+
hd = 0.30
|
|
1293
|
+
self.band = Polygon(
|
|
1294
|
+
np.array([left_x, y_pos - hd, mz - cap_h]),
|
|
1295
|
+
np.array([right_x, y_pos - hd, mz - cap_h]),
|
|
1296
|
+
np.array([right_x, y_pos - hd, mz + cap_h]),
|
|
1297
|
+
np.array([left_x, y_pos - hd, mz + cap_h]),
|
|
1298
|
+
fill_color=col_band,
|
|
1299
|
+
fill_opacity=1.0,
|
|
1300
|
+
stroke_width=0,
|
|
1301
|
+
)
|
|
1302
|
+
|
|
1303
|
+
# Label
|
|
1304
|
+
self.label = VGroup()
|
|
1305
|
+
if ci_label is not None:
|
|
1306
|
+
lbl = Text(ci_label, font_size=18, color=ci_color)
|
|
1307
|
+
lbl.move_to(np.array([mx, y_pos, mz + cap_h + 0.28]))
|
|
1308
|
+
self.label.add(lbl)
|
|
1309
|
+
if scene is not None:
|
|
1310
|
+
scene.add_fixed_orientation_mobjects(lbl)
|
|
1311
|
+
|
|
1312
|
+
def build(self, run_time: float = 1.2) -> Succession:
|
|
1313
|
+
"""Return a three-beat CI build animation.
|
|
1314
|
+
|
|
1315
|
+
Returns
|
|
1316
|
+
-------
|
|
1317
|
+
Succession
|
|
1318
|
+
"""
|
|
1319
|
+
rt = run_time / 3
|
|
1320
|
+
|
|
1321
|
+
# Beat 1: point estimate
|
|
1322
|
+
beat1 = AnimationGroup(
|
|
1323
|
+
FadeIn(self.dot_glow, scale=0.3, run_time=rt * 0.6),
|
|
1324
|
+
FadeIn(self.dot, scale=0.3, run_time=rt * 0.6),
|
|
1325
|
+
)
|
|
1326
|
+
|
|
1327
|
+
# Beat 2: arms extend
|
|
1328
|
+
left_arm = Line(
|
|
1329
|
+
self.mean_pos,
|
|
1330
|
+
np.array([self.mean_pos[0] - self.half_width,
|
|
1331
|
+
self.y_pos, self.mean_pos[2]]),
|
|
1332
|
+
color=_with_opacity(self.ci_color, 0.85),
|
|
1333
|
+
stroke_width=self.bar_sw * 0.7,
|
|
1334
|
+
)
|
|
1335
|
+
right_arm = Line(
|
|
1336
|
+
self.mean_pos,
|
|
1337
|
+
np.array([self.mean_pos[0] + self.half_width,
|
|
1338
|
+
self.y_pos, self.mean_pos[2]]),
|
|
1339
|
+
color=_with_opacity(self.ci_color, 0.85),
|
|
1340
|
+
stroke_width=self.bar_sw * 0.7,
|
|
1341
|
+
)
|
|
1342
|
+
|
|
1343
|
+
beat2 = AnimationGroup(
|
|
1344
|
+
Create(left_arm, run_time=rt * 0.8),
|
|
1345
|
+
Create(right_arm, run_time=rt * 0.8),
|
|
1346
|
+
Create(self.left_cap, run_time=rt),
|
|
1347
|
+
Create(self.right_cap, run_time=rt),
|
|
1348
|
+
lag_ratio=0.3,
|
|
1349
|
+
)
|
|
1350
|
+
|
|
1351
|
+
# Beat 3: band fades in + label
|
|
1352
|
+
beat3 = AnimationGroup(
|
|
1353
|
+
FadeIn(self.band, run_time=rt),
|
|
1354
|
+
FadeIn(self.label, run_time=rt * 0.7),
|
|
1355
|
+
)
|
|
1356
|
+
|
|
1357
|
+
return Succession(beat1, beat2, beat3)
|
|
1358
|
+
|
|
1359
|
+
|
|
1360
|
+
class RippleUpdate3D:
|
|
1361
|
+
"""When a bar or curve point is updated, emit a ripple through neighbours.
|
|
1362
|
+
|
|
1363
|
+
A colour pulse radiates outward from the updated bar, dimming as it
|
|
1364
|
+
travels. Used to visually signal which part of a distribution changed.
|
|
1365
|
+
|
|
1366
|
+
Parameters
|
|
1367
|
+
----------
|
|
1368
|
+
bar_group : VGroup
|
|
1369
|
+
Bar chart's ``bar_group``.
|
|
1370
|
+
center_index : int
|
|
1371
|
+
Index of the bar that was updated (ripple origin).
|
|
1372
|
+
ripple_color : ManimColor
|
|
1373
|
+
ripple_width : int
|
|
1374
|
+
Number of bars the ripple reaches.
|
|
1375
|
+
"""
|
|
1376
|
+
|
|
1377
|
+
def __init__(
|
|
1378
|
+
self,
|
|
1379
|
+
bar_group: VGroup,
|
|
1380
|
+
center_index: int,
|
|
1381
|
+
ripple_color: ManimColor = ManimColor("#FFD700"),
|
|
1382
|
+
ripple_width: int = 5,
|
|
1383
|
+
):
|
|
1384
|
+
self.bar_group = bar_group
|
|
1385
|
+
self.center = center_index
|
|
1386
|
+
self.ripple_color = ripple_color
|
|
1387
|
+
self.ripple_width = ripple_width
|
|
1388
|
+
|
|
1389
|
+
def build(self, run_time: float = 0.8) -> UpdateFromAlphaFunc:
|
|
1390
|
+
"""Return the ripple animation."""
|
|
1391
|
+
bg = self.bar_group
|
|
1392
|
+
c = self.center
|
|
1393
|
+
rw = self.ripple_width
|
|
1394
|
+
col = self.ripple_color
|
|
1395
|
+
n_bars = len(bg)
|
|
1396
|
+
|
|
1397
|
+
# Pre-compute original face colours
|
|
1398
|
+
original_fills: List[Optional[ManimColor]] = []
|
|
1399
|
+
for bar in bg:
|
|
1400
|
+
try:
|
|
1401
|
+
original_fills.append(bar[0].get_fill_color())
|
|
1402
|
+
except Exception:
|
|
1403
|
+
original_fills.append(None)
|
|
1404
|
+
|
|
1405
|
+
def updater(mob: VGroup, alpha: float) -> None:
|
|
1406
|
+
# Ripple front position (fractional bar index)
|
|
1407
|
+
ripple_front = alpha * (rw + 1)
|
|
1408
|
+
for i, bar in enumerate(mob):
|
|
1409
|
+
dist = abs(i - c)
|
|
1410
|
+
# Pulse intensity: peaks as front passes, then decays
|
|
1411
|
+
diff = ripple_front - dist
|
|
1412
|
+
if 0 <= diff <= 1.0:
|
|
1413
|
+
intensity = smooth(diff) * (1.0 - alpha * 0.5)
|
|
1414
|
+
else:
|
|
1415
|
+
intensity = 0.0
|
|
1416
|
+
try:
|
|
1417
|
+
if intensity > 0.02:
|
|
1418
|
+
bar[0].set_fill(
|
|
1419
|
+
color=_lerp_color(
|
|
1420
|
+
original_fills[i] or WHITE,
|
|
1421
|
+
col,
|
|
1422
|
+
intensity,
|
|
1423
|
+
)
|
|
1424
|
+
)
|
|
1425
|
+
else:
|
|
1426
|
+
if original_fills[i] is not None:
|
|
1427
|
+
bar[0].set_fill(color=original_fills[i])
|
|
1428
|
+
except Exception:
|
|
1429
|
+
pass
|
|
1430
|
+
|
|
1431
|
+
return UpdateFromAlphaFunc(bg, updater, run_time=run_time)
|
|
1432
|
+
|
|
1433
|
+
|
|
1434
|
+
# ============================================================================
|
|
1435
|
+
# 7. Cloud / scatter transitions
|
|
1436
|
+
# ============================================================================
|
|
1437
|
+
|
|
1438
|
+
class CollapseToMean3D:
|
|
1439
|
+
"""Animate all dots in a cloud converging to the mean position.
|
|
1440
|
+
|
|
1441
|
+
Each dot travels along a Bézier arc toward the mean. Used to
|
|
1442
|
+
illustrate E[X] visually — all individual values collapse to a
|
|
1443
|
+
single expected value.
|
|
1444
|
+
|
|
1445
|
+
Parameters
|
|
1446
|
+
----------
|
|
1447
|
+
dots : list of VGroup or Dot3D
|
|
1448
|
+
Each element must respond to ``move_to`` and ``get_center``.
|
|
1449
|
+
mean_position : np.ndarray
|
|
1450
|
+
3D position to converge toward.
|
|
1451
|
+
arc_height : float
|
|
1452
|
+
Arc height above the direct path to the mean.
|
|
1453
|
+
trail_color : ManimColor
|
|
1454
|
+
Colour of the arc trail (optional ghosting effect).
|
|
1455
|
+
"""
|
|
1456
|
+
|
|
1457
|
+
def __init__(
|
|
1458
|
+
self,
|
|
1459
|
+
dots: List,
|
|
1460
|
+
mean_position: np.ndarray,
|
|
1461
|
+
arc_height: float = 0.6,
|
|
1462
|
+
trail_color: ManimColor = ManimColor("#4A90D9"),
|
|
1463
|
+
):
|
|
1464
|
+
self.dots = dots
|
|
1465
|
+
self.mean = np.array(mean_position, dtype=float)
|
|
1466
|
+
self.arc_height = arc_height
|
|
1467
|
+
self.trail_color = trail_color
|
|
1468
|
+
|
|
1469
|
+
# Pre-compute arc control points
|
|
1470
|
+
self._arcs: List[Tuple[np.ndarray, np.ndarray, np.ndarray]] = []
|
|
1471
|
+
for dot in dots:
|
|
1472
|
+
try:
|
|
1473
|
+
start = dot.get_center().copy()
|
|
1474
|
+
except AttributeError:
|
|
1475
|
+
start = ORIGIN.copy()
|
|
1476
|
+
end = self.mean.copy()
|
|
1477
|
+
mid = (start + end) / 2 + np.array([0, 0, arc_height])
|
|
1478
|
+
self._arcs.append((start, mid, end))
|
|
1479
|
+
|
|
1480
|
+
def build(
|
|
1481
|
+
self,
|
|
1482
|
+
run_time: float = 1.5,
|
|
1483
|
+
lag: float = 0.02,
|
|
1484
|
+
) -> LaggedStart:
|
|
1485
|
+
"""Return a staggered animation of all dots converging.
|
|
1486
|
+
|
|
1487
|
+
Parameters
|
|
1488
|
+
----------
|
|
1489
|
+
run_time : float
|
|
1490
|
+
Duration of each individual dot's animation.
|
|
1491
|
+
lag : float
|
|
1492
|
+
Time between dot start times.
|
|
1493
|
+
|
|
1494
|
+
Returns
|
|
1495
|
+
-------
|
|
1496
|
+
LaggedStart
|
|
1497
|
+
"""
|
|
1498
|
+
anims = []
|
|
1499
|
+
for dot, (start, mid, end) in zip(self.dots, self._arcs):
|
|
1500
|
+
def make_updater(s, m, e, d):
|
|
1501
|
+
def updater(mob, alpha: float) -> None:
|
|
1502
|
+
t = smooth(alpha)
|
|
1503
|
+
pos = (1 - t)**2 * s + 2 * (1 - t) * t * m + t**2 * e
|
|
1504
|
+
mob.move_to(pos)
|
|
1505
|
+
return updater
|
|
1506
|
+
|
|
1507
|
+
anims.append(UpdateFromAlphaFunc(dot, make_updater(start, mid, end, dot),
|
|
1508
|
+
run_time=run_time))
|
|
1509
|
+
|
|
1510
|
+
return LaggedStart(*anims, lag_ratio=lag)
|
|
1511
|
+
|
|
1512
|
+
def build_flash_at_mean(
|
|
1513
|
+
self,
|
|
1514
|
+
color: Optional[ManimColor] = None,
|
|
1515
|
+
) -> Flash:
|
|
1516
|
+
"""Return a Flash at the mean position for use after collapse."""
|
|
1517
|
+
col = color if color is not None else self.trail_color
|
|
1518
|
+
dot = Dot3D(point=self.mean, radius=0.12, color=col)
|
|
1519
|
+
return Flash(dot, color=col, flash_radius=0.5, run_time=0.5)
|
|
1520
|
+
|
|
1521
|
+
|
|
1522
|
+
class ScatterToRegression3D:
|
|
1523
|
+
"""Animate scatter-plot dots rearranging to lie on a regression line.
|
|
1524
|
+
|
|
1525
|
+
Each dot travels from its current position to its predicted value
|
|
1526
|
+
on the regression line y_hat = a + b*x. The residual distance is
|
|
1527
|
+
preserved visually before and after.
|
|
1528
|
+
|
|
1529
|
+
Parameters
|
|
1530
|
+
----------
|
|
1531
|
+
dots : list of VGroup or Dot3D
|
|
1532
|
+
One dot per data point. Each must respond to ``move_to``.
|
|
1533
|
+
x_values : np.ndarray
|
|
1534
|
+
X coordinate of each data point (parallel to *dots*).
|
|
1535
|
+
y_values : np.ndarray
|
|
1536
|
+
Actual Y (z in scene) coordinate of each data point.
|
|
1537
|
+
a : float
|
|
1538
|
+
Regression intercept.
|
|
1539
|
+
b : float
|
|
1540
|
+
Regression slope.
|
|
1541
|
+
x_to_scene : Callable[[float], float]
|
|
1542
|
+
Map data-x to scene-x.
|
|
1543
|
+
z_to_scene : Callable[[float], float]
|
|
1544
|
+
Map data-y to scene-z.
|
|
1545
|
+
y_pos : float
|
|
1546
|
+
Scene y-coordinate.
|
|
1547
|
+
residual_color : ManimColor
|
|
1548
|
+
Colour of residual lines drawn from fitted to actual position.
|
|
1549
|
+
show_residuals : bool
|
|
1550
|
+
Whether to show residual lines before collapsing.
|
|
1551
|
+
"""
|
|
1552
|
+
|
|
1553
|
+
def __init__(
|
|
1554
|
+
self,
|
|
1555
|
+
dots: List,
|
|
1556
|
+
x_values: np.ndarray,
|
|
1557
|
+
y_values: np.ndarray,
|
|
1558
|
+
a: float,
|
|
1559
|
+
b: float,
|
|
1560
|
+
x_to_scene: Callable[[float], float],
|
|
1561
|
+
z_to_scene: Callable[[float], float],
|
|
1562
|
+
y_pos: float = 0.0,
|
|
1563
|
+
residual_color: ManimColor = ManimColor("#E8593C"),
|
|
1564
|
+
show_residuals: bool = True,
|
|
1565
|
+
):
|
|
1566
|
+
self.dots = dots
|
|
1567
|
+
self.xs = np.asarray(x_values, dtype=float)
|
|
1568
|
+
self.ys = np.asarray(y_values, dtype=float)
|
|
1569
|
+
self.a = a
|
|
1570
|
+
self.b = b
|
|
1571
|
+
self.x_to_scene = x_to_scene
|
|
1572
|
+
self.z_to_scene = z_to_scene
|
|
1573
|
+
self.y_pos = y_pos
|
|
1574
|
+
self.residual_color = residual_color
|
|
1575
|
+
self.show_residuals = show_residuals
|
|
1576
|
+
|
|
1577
|
+
# Compute fitted positions
|
|
1578
|
+
y_hat = a + b * self.xs
|
|
1579
|
+
self._fitted_positions: List[np.ndarray] = [
|
|
1580
|
+
np.array([x_to_scene(xi), y_pos, z_to_scene(yhi)])
|
|
1581
|
+
for xi, yhi in zip(self.xs, y_hat)
|
|
1582
|
+
]
|
|
1583
|
+
|
|
1584
|
+
def build_residual_lines(self) -> VGroup:
|
|
1585
|
+
"""Build residual line segments (actual → fitted).
|
|
1586
|
+
|
|
1587
|
+
Returns
|
|
1588
|
+
-------
|
|
1589
|
+
VGroup of Lines — add to scene before animating.
|
|
1590
|
+
"""
|
|
1591
|
+
residuals = VGroup()
|
|
1592
|
+
col = _with_opacity(self.residual_color, 0.55)
|
|
1593
|
+
for dot, fitted_pos in zip(self.dots, self._fitted_positions):
|
|
1594
|
+
try:
|
|
1595
|
+
actual_pos = dot.get_center()
|
|
1596
|
+
except AttributeError:
|
|
1597
|
+
actual_pos = fitted_pos
|
|
1598
|
+
resid = DashedLine(
|
|
1599
|
+
actual_pos, fitted_pos,
|
|
1600
|
+
dash_length=0.06, dashed_ratio=0.5,
|
|
1601
|
+
color=col, stroke_width=1.2,
|
|
1602
|
+
)
|
|
1603
|
+
residuals.add(resid)
|
|
1604
|
+
return residuals
|
|
1605
|
+
|
|
1606
|
+
def build_collapse(
|
|
1607
|
+
self,
|
|
1608
|
+
run_time: float = 1.4,
|
|
1609
|
+
lag: float = 0.02,
|
|
1610
|
+
) -> LaggedStart:
|
|
1611
|
+
"""Animate all dots moving to their fitted positions.
|
|
1612
|
+
|
|
1613
|
+
Returns
|
|
1614
|
+
-------
|
|
1615
|
+
LaggedStart
|
|
1616
|
+
"""
|
|
1617
|
+
anims = []
|
|
1618
|
+
for dot, fitted in zip(self.dots, self._fitted_positions):
|
|
1619
|
+
try:
|
|
1620
|
+
start = dot.get_center().copy()
|
|
1621
|
+
except AttributeError:
|
|
1622
|
+
start = fitted.copy()
|
|
1623
|
+
|
|
1624
|
+
def make_updater(s, f):
|
|
1625
|
+
def updater(mob, alpha: float) -> None:
|
|
1626
|
+
t = smooth(alpha)
|
|
1627
|
+
mob.move_to(s + (f - s) * t)
|
|
1628
|
+
return updater
|
|
1629
|
+
|
|
1630
|
+
anims.append(UpdateFromAlphaFunc(dot, make_updater(start, fitted),
|
|
1631
|
+
run_time=run_time))
|
|
1632
|
+
|
|
1633
|
+
return LaggedStart(*anims, lag_ratio=lag)
|
|
1634
|
+
|
|
1635
|
+
def run(
|
|
1636
|
+
self,
|
|
1637
|
+
scene: Scene,
|
|
1638
|
+
run_time_residuals: float = 0.7,
|
|
1639
|
+
run_time_collapse: float = 1.4,
|
|
1640
|
+
hold: float = 0.5,
|
|
1641
|
+
) -> None:
|
|
1642
|
+
"""Show residuals, then collapse dots to regression line.
|
|
1643
|
+
|
|
1644
|
+
Parameters
|
|
1645
|
+
----------
|
|
1646
|
+
run_time_residuals : float
|
|
1647
|
+
Duration of residual line appearance.
|
|
1648
|
+
run_time_collapse : float
|
|
1649
|
+
Duration of dot collapse.
|
|
1650
|
+
hold : float
|
|
1651
|
+
Pause between residuals and collapse.
|
|
1652
|
+
"""
|
|
1653
|
+
if self.show_residuals:
|
|
1654
|
+
residuals = self.build_residual_lines()
|
|
1655
|
+
scene.add(residuals)
|
|
1656
|
+
scene.play(Create(residuals, run_time=run_time_residuals))
|
|
1657
|
+
scene.wait(hold)
|
|
1658
|
+
|
|
1659
|
+
scene.play(self.build_collapse(run_time=run_time_collapse))
|
|
1660
|
+
|
|
1661
|
+
if self.show_residuals:
|
|
1662
|
+
scene.play(FadeOut(residuals, run_time=0.35))
|
|
1663
|
+
|
|
1664
|
+
|
|
1665
|
+
# ============================================================================
|
|
1666
|
+
# Module-level convenience functions
|
|
1667
|
+
# ============================================================================
|
|
1668
|
+
|
|
1669
|
+
def dist_morph(
|
|
1670
|
+
source: VMobject,
|
|
1671
|
+
target: VMobject,
|
|
1672
|
+
run_time: float = 1.5,
|
|
1673
|
+
n_interp_pts: int = 300,
|
|
1674
|
+
color_start: Optional[ManimColor] = None,
|
|
1675
|
+
color_end: Optional[ManimColor] = None,
|
|
1676
|
+
rate_func: Callable = rate_functions.ease_in_out_cubic,
|
|
1677
|
+
) -> UpdateFromAlphaFunc:
|
|
1678
|
+
"""One-liner wrapper for ``DistMorph3D.build()``.
|
|
1679
|
+
|
|
1680
|
+
Parameters
|
|
1681
|
+
----------
|
|
1682
|
+
source : VMobject
|
|
1683
|
+
Curve to morph from (modified in place).
|
|
1684
|
+
target : VMobject
|
|
1685
|
+
Curve to morph to (read-only).
|
|
1686
|
+
run_time : float
|
|
1687
|
+
n_interp_pts : int
|
|
1688
|
+
color_start, color_end : ManimColor or None
|
|
1689
|
+
rate_func : Callable
|
|
1690
|
+
|
|
1691
|
+
Returns
|
|
1692
|
+
-------
|
|
1693
|
+
UpdateFromAlphaFunc — ready for ``scene.play()``.
|
|
1694
|
+
|
|
1695
|
+
Example
|
|
1696
|
+
-------
|
|
1697
|
+
::
|
|
1698
|
+
|
|
1699
|
+
scene.play(dist_morph(normal_stroke, t3_stroke, run_time=1.5))
|
|
1700
|
+
"""
|
|
1701
|
+
return DistMorph3D(
|
|
1702
|
+
source, target,
|
|
1703
|
+
n_interp_pts=n_interp_pts,
|
|
1704
|
+
color_start=color_start,
|
|
1705
|
+
color_end=color_end,
|
|
1706
|
+
rate_func=rate_func,
|
|
1707
|
+
).build(run_time=run_time)
|
|
1708
|
+
|
|
1709
|
+
|
|
1710
|
+
def hist_morph(
|
|
1711
|
+
bar_group: VGroup,
|
|
1712
|
+
new_heights: Sequence[float],
|
|
1713
|
+
run_time: float = 1.2,
|
|
1714
|
+
color_map: Optional[List[ManimColor]] = None,
|
|
1715
|
+
rate_func: Callable = rate_functions.ease_in_out_cubic,
|
|
1716
|
+
) -> UpdateFromAlphaFunc:
|
|
1717
|
+
"""One-liner wrapper for ``HistMorph3D.build()``.
|
|
1718
|
+
|
|
1719
|
+
Parameters
|
|
1720
|
+
----------
|
|
1721
|
+
bar_group : VGroup
|
|
1722
|
+
``bar_group`` of a ``BarChart3D``.
|
|
1723
|
+
new_heights : sequence of float
|
|
1724
|
+
Target heights in scene units.
|
|
1725
|
+
run_time : float
|
|
1726
|
+
color_map : list of ManimColor or None
|
|
1727
|
+
rate_func : Callable
|
|
1728
|
+
|
|
1729
|
+
Returns
|
|
1730
|
+
-------
|
|
1731
|
+
UpdateFromAlphaFunc
|
|
1732
|
+
|
|
1733
|
+
Example
|
|
1734
|
+
-------
|
|
1735
|
+
::
|
|
1736
|
+
|
|
1737
|
+
scene.play(hist_morph(chart.bar_group, [1.5, 2.8, 3.1], run_time=1.0))
|
|
1738
|
+
"""
|
|
1739
|
+
return HistMorph3D(
|
|
1740
|
+
bar_group, new_heights,
|
|
1741
|
+
color_map=color_map, rate_func=rate_func,
|
|
1742
|
+
).build(run_time=run_time)
|
|
1743
|
+
|
|
1744
|
+
|
|
1745
|
+
def cdf_build(
|
|
1746
|
+
pdf_curve: VMobject,
|
|
1747
|
+
cdf_curve: VMobject,
|
|
1748
|
+
area_fill: Optional[VMobject] = None,
|
|
1749
|
+
x_range: Tuple[float, float] = (-4.0, 4.0),
|
|
1750
|
+
run_time: float = 2.0,
|
|
1751
|
+
) -> AnimationGroup:
|
|
1752
|
+
"""One-liner wrapper for ``CDFBuild3D.build()``.
|
|
1753
|
+
|
|
1754
|
+
Example
|
|
1755
|
+
-------
|
|
1756
|
+
::
|
|
1757
|
+
|
|
1758
|
+
scene.play(cdf_build(pdf.stroke, cdf.stroke, run_time=2.0))
|
|
1759
|
+
"""
|
|
1760
|
+
return CDFBuild3D(
|
|
1761
|
+
pdf_curve, cdf_curve,
|
|
1762
|
+
area_fill=area_fill,
|
|
1763
|
+
x_range=x_range,
|
|
1764
|
+
).build(run_time=run_time)
|
|
1765
|
+
|
|
1766
|
+
|
|
1767
|
+
def parameter_sweep(
|
|
1768
|
+
scene: Scene,
|
|
1769
|
+
func: Callable[[float, float], float],
|
|
1770
|
+
x_range: Tuple[float, float, float],
|
|
1771
|
+
param_range: Tuple[float, float, float],
|
|
1772
|
+
param_name: str,
|
|
1773
|
+
existing_curve: VMobject,
|
|
1774
|
+
run_time_total: float = 3.0,
|
|
1775
|
+
**kwargs,
|
|
1776
|
+
) -> None:
|
|
1777
|
+
"""One-liner wrapper for ``ParameterSweep3D.run()``.
|
|
1778
|
+
|
|
1779
|
+
Example
|
|
1780
|
+
-------
|
|
1781
|
+
::
|
|
1782
|
+
|
|
1783
|
+
parameter_sweep(
|
|
1784
|
+
self,
|
|
1785
|
+
func=lambda x, s: np.exp(-0.5*(x/s)**2)/(s*np.sqrt(TAU)),
|
|
1786
|
+
x_range=(-4, 4, 0.05),
|
|
1787
|
+
param_range=(0.5, 2.5, 0.1),
|
|
1788
|
+
param_name="σ",
|
|
1789
|
+
existing_curve=normal_curve.stroke,
|
|
1790
|
+
run_time_total=4.0,
|
|
1791
|
+
)
|
|
1792
|
+
"""
|
|
1793
|
+
ParameterSweep3D(
|
|
1794
|
+
func=func,
|
|
1795
|
+
x_range=x_range,
|
|
1796
|
+
param_range=param_range,
|
|
1797
|
+
param_name=param_name,
|
|
1798
|
+
existing_curve=existing_curve,
|
|
1799
|
+
**kwargs,
|
|
1800
|
+
).run(scene, run_time_total=run_time_total)
|
|
1801
|
+
|
|
1802
|
+
|
|
1803
|
+
def curtain_reveal(
|
|
1804
|
+
target: VGroup,
|
|
1805
|
+
x_start: float = -4.0,
|
|
1806
|
+
x_end: float = 4.0,
|
|
1807
|
+
run_time: float = 1.5,
|
|
1808
|
+
**kwargs,
|
|
1809
|
+
) -> AnimationGroup:
|
|
1810
|
+
"""One-liner wrapper for ``CurtainReveal3D.build()``.
|
|
1811
|
+
|
|
1812
|
+
Example
|
|
1813
|
+
-------
|
|
1814
|
+
::
|
|
1815
|
+
|
|
1816
|
+
scene.play(curtain_reveal(chart.bar_group, x_start=-3.5, x_end=3.5))
|
|
1817
|
+
"""
|
|
1818
|
+
return CurtainReveal3D(
|
|
1819
|
+
target, x_start=x_start, x_end=x_end, **kwargs
|
|
1820
|
+
).build(run_time=run_time)
|
|
1821
|
+
|
|
1822
|
+
|
|
1823
|
+
def ci_build(
|
|
1824
|
+
mean_pos: np.ndarray,
|
|
1825
|
+
half_width: float,
|
|
1826
|
+
run_time: float = 1.2,
|
|
1827
|
+
**kwargs,
|
|
1828
|
+
) -> Succession:
|
|
1829
|
+
"""One-liner wrapper for ``CIBuild3D.build()``.
|
|
1830
|
+
|
|
1831
|
+
Example
|
|
1832
|
+
-------
|
|
1833
|
+
::
|
|
1834
|
+
|
|
1835
|
+
scene.play(ci_build(np.array([0, 0, 2.5]), half_width=0.8))
|
|
1836
|
+
"""
|
|
1837
|
+
return CIBuild3D(mean_pos=mean_pos, half_width=half_width, **kwargs).build(run_time=run_time)
|
|
1838
|
+
|
|
1839
|
+
|
|
1840
|
+
def collapse_to_mean(
|
|
1841
|
+
dots: List,
|
|
1842
|
+
mean_position: np.ndarray,
|
|
1843
|
+
run_time: float = 1.5,
|
|
1844
|
+
lag: float = 0.02,
|
|
1845
|
+
**kwargs,
|
|
1846
|
+
) -> LaggedStart:
|
|
1847
|
+
"""One-liner wrapper for ``CollapseToMean3D.build()``.
|
|
1848
|
+
|
|
1849
|
+
Example
|
|
1850
|
+
-------
|
|
1851
|
+
::
|
|
1852
|
+
|
|
1853
|
+
scene.play(collapse_to_mean(population.dots, mean_pos))
|
|
1854
|
+
"""
|
|
1855
|
+
return CollapseToMean3D(
|
|
1856
|
+
dots=dots, mean_position=mean_position, **kwargs
|
|
1857
|
+
).build(run_time=run_time, lag=lag)
|