Colorbar In Canva

This demo presents the colorbar in canva scene in 2D and shows how the animation state evolves over time with lib-anim. The script defines a canva, adds the corresponding items, and updates them frame by frame to produce the final motion.

2D colorbar in canva demo

Full Code

 1'''
 22D colorbar array demo
 3'''
 4
 5import numpy as np
 6import anim
 7
 8# ═══ 2D Animation canva ═══════════════════════════════════════════════════
 9
10class Canva(anim.plane.canva):
11
12  # ────────────────────────────────────────────────────────────────────────
13  def __init__(self, window):
14
15    # Number of pixels in the image
16    self.npix = 1000
17
18    # Colormap
19    self.cmap = anim.colormap('magma', range=[-1, 1])
20
21    super().__init__(window,
22                     display_boundaries = False,
23                     pixelperunit = self.npix)
24
25    # ─── display
26
27    self.item.img = anim.plane.image(
28      position = [0.6, 0.5],
29      dimension = [0.8, 0.8],
30      array = self.phase(0),
31      colormap = self.cmap
32    )
33
34    # ─── colorbar
35
36    self.item.cbar = anim.plane.colorbar(
37      position = [0.15, 0.5],
38      dimension = [0.03, 0.4],
39      colormap = self.cmap,
40      ticks_number = 5,
41      ticks_fontsize = 0.03,
42    )
43
44  # ────────────────────────────────────────────────────────────────────────
45  def phase(self, t):
46
47    # Base field
48    x = np.linspace(-0.5, 0.5, self.npix)
49    X, Y = np.meshgrid(x, x)
50    return np.angle(np.exp(t/(X + 1j*Y)/10))/np.pi
51
52  # ────────────────────────────────────────────────────────────────────────
53  def update(self, t):
54
55    self.item.img.array = self.phase(t.step)
56
57    # Confirm update
58    super().update(t)
59
60# ═══ Main ═════════════════════════════════════════════════════════════════
61
62W = anim.window('Colorbar animation')
63
64# Add animation
65W.add(Canva)
66
67# Allow backward animation
68W.allow_backward = True
69W.allow_negative_time = True
70
71W.show()