- Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathanimation.py
97 lines (78 loc) · 2.05 KB
/
animation.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
"""
================
An animated line
================
An animated line using a custom container class,
:class:`.wrappers.LineWrapper`, and :class:`.wrappers.FormattedText`.
"""
importtime
fromtypingimportDict, Tuple, Any, Union
fromfunctoolsimportpartial
importnumpyasnp
importmatplotlib.pyplotasplt
frommatplotlib.animationimportFuncAnimation
fromdata_prototype.conversion_edgeimportGraph
fromdata_prototype.descriptionimportDesc
fromdata_prototype.artistimportCompatibilityAxes
fromdata_prototype.lineimportLine
fromdata_prototype.textimportText
fromdata_prototype.conversion_edgeimportFuncEdge
classSinOfTime:
N=1024
# cycles per minutes
scale=10
defdescribe(self):
return {
"x": Desc((self.N,)),
"y": Desc((self.N,)),
"phase": Desc(()),
"time": Desc(()),
}
defquery(
self,
graph: Graph,
parent_coordinates: str="axes",
) ->Tuple[Dict[str, Any], Union[str, int]]:
th=np.linspace(0, 2*np.pi, self.N)
cur_time=time.time()
phase=2*np.pi* (self.scale*cur_time%60) /60
return {
"x": th,
"y": np.sin(th+phase),
"phase": phase,
"time": cur_time,
}, hash(cur_time)
defupdate(frame, art):
returnart
sot_c=SinOfTime()
lw=Line(sot_c, linewidth=5, color="green", label="sin(time)")
fc=Text(
sot_c,
[
FuncEdge.from_func(
"text",
lambdaphase: f"ϕ={phase:.2f}",
{"phase": Desc((), "auto")},
{"text": Desc((), "display")},
),
],
x=2*np.pi,
y=1,
ha="right",
)
fig, nax=plt.subplots()
ax=CompatibilityAxes(nax)
nax.add_artist(ax)
ax.add_artist(lw)
ax.add_artist(fc)
ax.set_xlim(0, 2*np.pi)
ax.set_ylim(-1.1, 1.1)
ani=FuncAnimation(
fig,
partial(update, art=(lw, fc)),
frames=25,
interval=1000/60,
# TODO: blitting does not work because wrappers do not inherent from Artist
# blit=True,
)
plt.show()