-
Notifications
You must be signed in to change notification settings - Fork 114
/
Copy path001.Creative_Animation_3D_Pythagorean_Theorem.py
95 lines (89 loc) · 3.32 KB
/
001.Creative_Animation_3D_Pythagorean_Theorem.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
import numpy as np
import pandas as pd
import plotly.express as px
import plotly.graph_objects as go
# Generate DataFrame with Values
data = []
for a in np.linspace(1, 10, 10):
for b in np.linspace(1, 10, 10):
c = np.sqrt(a**2 + b**2)
data.append({'a': a, 'b': b, 'c': c})
df = pd.DataFrame(data)
# Create Animation
frames = []
for i, row in df.iterrows():
frame = go.Frame(
data=[go.Scatter(x=[0, row['a']], y=[0, row['b']], mode='lines+markers', marker=dict(size=10)),
go.Scatter(x=[row['a']], y=[row['b']], mode='markers', marker=dict(size=15, color='red'))],
layout=go.Layout(
title=f'Pythagorean Theorem: a = {row["a"]}, b = {row["b"]}, c = {row["c"]:.2f}',
showlegend=False,
xaxis=dict(range=[0, 12], title='a'),
yaxis=dict(range=[0, 12], title='b'),
annotations=[
go.layout.Annotation(x=row['a'], y=row['b'], xref='x', yref='y',
text=f'c = {row["c"]:.2f}', showarrow=True, arrowhead=2, ax=0, ay=-40),
go.layout.Annotation(x=row['a'] / 2, y=row['b'] / 2, xref='x', yref='y',
text=f'a = {row["a"]:.2f}', showarrow=False, font=dict(color='blue')),
go.layout.Annotation(x=row['a'] / 2 + 0.2, y=row['b'] / 2 + 0.2, xref='x', yref='y',
text=f'b = {row["b"]:.2f}', showarrow=False, font=dict(color='blue'))
]
)
)
frames.append(frame)
# Create Animation Figure
fig = go.Figure(
data=[go.Scatter(x=[0], y=[0], mode='markers', marker=dict(size=1, color='white'))], # Invisible point for axes scaling
frames=frames
)
# Add Start/End Frames
start_frame = go.Frame(data=[go.Scatter(x=[0], y=[0], mode='markers', marker=dict(size=1, color='white'))])
end_frame = go.Frame(data=[go.Scatter(x=[df['a'].max()], y=[df['b'].max()], mode='markers', marker=dict(size=1, color='white'))])
frames_list = list(fig.frames)
frames_list = [start_frame] + frames_list + [end_frame]
fig.frames = frames_list
# Set Layout and Animation Settings
fig.update_layout(
updatemenus=[
{
'buttons': [
{
'args': [None, {'frame': {'duration': 200, 'redraw': True}, 'fromcurrent': True}],
'label': 'Play',
'method': 'animate',
},
{
'args': [[None], {'frame': {'duration': 0, 'redraw': True}, 'mode': 'immediate', 'transition': {'duration': 0}}],
'label': 'Pause',
'method': 'animate',
},
],
'direction': 'left',
'pad': {'r': 10, 't': 87},
'showactive': False,
'type': 'buttons',
'x': 0.1,
'xanchor': 'right',
'y': 0,
'yanchor': 'top',
},
],
sliders=[{
'active': 0,
'yanchor': 'top',
'xanchor': 'left',
'currentvalue': {
'font': {'size': 20},
'prefix': 'Iteration:',
'visible': True,
'xanchor': 'right',
},
'transition': {'duration': 300, 'easing': 'cubic-in-out'},
'pad': {'b': 10, 't': 50},
'len': 0.9,
'x': 0.1,
'y': 0,
}],
)
# Show Animation
fig.show()