-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlotting.py
124 lines (82 loc) · 2.52 KB
/
Plotting.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#!/usr/bin/env python
# coding: utf-8
# In[ ]:
import plotly
from plotly.subplots import make_subplots
import plotly.graph_objects as go
# In[ ]:
import matplotlib.pyplot as plt
# In[ ]:
def visualize_normal_mode(geometry, eigenvector, scale=10):
vec = eigenvector.reshape(-1, 3)
trace2 = go.Cone(
x=geometry[:, 0],
y=geometry[:, 1],
z=geometry[:, 2],
u=vec[:, 0] * scale,
v=vec[:, 1] * scale,
w=vec[:, 2] * scale,
sizemode="absolute", # "absolute"
sizeref=2,
anchor="tail"
)
return [trace2]
# In[ ]:
def visualize_normal_modes(geometry, eigenvectors, scale=10, cols=3):
N3, N3 = eigenvectors.shape
rows = int(N3/cols)
if N3 % cols > 0:
rows += 1
specs = [[{'is_3d': True} for i in range(cols)]
for j in range(rows)]
fig = make_subplots(rows=rows, cols=cols, specs=specs)
for row in range(rows):
for col in range(cols):
i = row * cols + col
if i >= N3:
continue
traces = visualize_normal_mode(geometry, eigenvectors[:, i], scale)
fig.add_trace(traces[0], row=row + 1, col=col + 1)
fig.update_layout(scene_aspectmode='data')
return fig
# In[ ]:
from math import ceil
# In[ ]:
def plot2d(R, v, scale=.5):
numCols = ceil(len(v)/3)
plt.subplots(numCols, 3, sharex=True, sharey=True, figsize=(15, 4))
for i in range(len(v)):
plt.subplot(numCols, 3, i+1)
if np.allclose(v[i], np.zeros(v.shape[1])): continue
plt.xlim((-1.5,1.5))
plt.ylim((-1.5,1.5))
for j in range(len(R)):
x, y, = R[j]
plt.arrow(x,y, v[i,j*2]*scale, v[i,j*2+1]*scale, width=scale*.1)
plt.show()
# In[ ]:
def plot(R, v, *args):
if R.shape[1]==2:
plot2d(R, v, *args)
else:
return visualize_normal_modes(R, v.T, *args)
# In[ ]:
from ipywidgets import interactive
import matplotlib.pyplot as plt
import numpy as np
# In[ ]:
def sumBasis(R, D, scale=.5):
def plotC(**c):
vSum = np.zeros(D.shape[1])
for key, value in c.items():
vSum += value*D[int(key)]
plt.figure(2)
for i in range(len(R)):
plt.arrow(R[i,0],R[i,1],vSum[i*2]*scale,vSum[i*2+1]*scale, width=scale*.1)
plt.xlim(-2,2)
plt.ylim(-2,2)
args = {}
for i in range(len(D)):
args[str(i)] = (-1.5, 1.5, .05)
interactive_plot = interactive(plotC, **args)
return interactive_plot