forked from SebastianPeitz/QuaSiModO
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvisualization.py
221 lines (173 loc) · 6.09 KB
/
visualization.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
def plot(showPlot=True, fOut=None, **kwargs):
"""
multiPlotLines2D
Function for 2D plotting of data versus time. Each data set needs to be specified within kwargs (e.g., y=data.y)
"""
class structPlot:
flagRef = False
markerSize = 0
marker = None
iplot = 0
type = 'Lines'
x = []
phase = False
flagLegend = False
def __init__(self, value_, key_):
self.t = value_['t']
self.y = value_[key_]
self.label = key_
if 'markerSize' in value_.keys():
self.markerSize = value_['markerSize']
self.marker = 'o'
if 'reference' in value_.keys():
self.ref = value_['reference']
self.flagRef = True
if 'iplot' in value_.keys():
self.iplot = value_['iplot']
if 'type' in value_.keys():
self.type = value_['type']
if 'x' in value_.keys():
self.x = value_['x']
if 'phase' in value_.keys():
self.phase = value_['phase']
if 'legend' in value_.keys():
self.flagLegend = value_['legend']
nPlots = 1
plots = []
for key, value in kwargs.items():
plots.append(structPlot(value, key))
if plots[-1].iplot + 1 > nPlots:
nPlots = plots[-1].iplot + 1
if nPlots == 1:
ix = 1
iy = 1
elif nPlots == 2:
ix = 2
iy = 1
elif nPlots == 3:
ix = 2
iy = 2
elif nPlots == 4:
ix = 2
iy = 2
elif nPlots == 5:
ix = 3
iy = 2
elif nPlots == 6:
ix = 3
iy = 2
elif nPlots == 7:
ix = 3
iy = 3
elif nPlots == 8:
ix = 3
iy = 3
elif nPlots == 9:
ix = 3
iy = 3
fig = plt.figure(figsize=[12.80, 7.68])
axes, labels = list(), list()
for i in range(nPlots):
axes.append([])
labels.append('')
ni = np.zeros([nPlots], dtype=int)
for s in range(len(plots)):
ip = plots[s].iplot
if ni[ip] == 0:
if plots[s].type == 'Surface':
axes[ip] = fig.add_subplot(ix, iy, ip + 1, projection='3d')
axes[ip].set_zlabel(plots[ip].label)
else:
axes[ip] = fig.add_subplot(ix, iy, ip + 1)
# else:
# axes[ip] = plt.subplot(ix, iy, ip + 1)
# plt.subplot(axes[ip])
if ni[ip] == 0:
lineStyle = 'solid'
elif ni[ip] == 1:
lineStyle = 'dashed'
elif ni[ip] == 2:
lineStyle = 'dotted'
elif ni[ip] == 3:
lineStyle = 'dashdot'
ni[ip] += 1
if len(plots[s].y.shape) == 1:
y = np.zeros([plots[s].y.shape[0], 1], dtype=float)
y[:, 0] = plots[s].y
plots[s].y = y
for i in range(plots[s].y.shape[1]):
if plots[s].type == 'Lines':
iCol = float(i) / float(np.maximum(plots[s].y.shape[1] - 1, 1))
if plots[s].y.shape[1] > 1:
labelString = plots[s].label + str(i)
else:
labelString = plots[s].label
if plots[s].phase:
axes[ip].plot(plots[s].y[:, 0], plots[s].y[:, 1], color=plt.cm.RdYlBu(iCol), linestyle=lineStyle,
linewidth=2, marker=plots[s].marker, markersize=plots[ip].markerSize,
label=labelString)
else:
axes[ip].plot(plots[s].t, plots[s].y[:, i], color=plt.cm.RdYlBu(iCol), linestyle=lineStyle,
linewidth=2, marker=plots[s].marker, markersize=plots[ip].markerSize,
label=labelString)
if plots[s].flagRef:
for j in range(len(plots[s].ref.iRef)):
iCol = float(plots[s].ref.iRef[j]) / float(np.maximum(plots[s].y.shape[1] - 1, 1))
# axes[ip].plot(plots[s].t, plots[s].ref.z[:len(plots[s].t), plots[s].ref.iRef[j]].toarray(),
axes[ip].plot(plots[s].t, plots[s].ref.z[:len(plots[s].t), plots[s].ref.iRef[j]],
color=plt.cm.RdYlBu(iCol), linestyle=(0, (3, 5, 1, 5, 1, 5)), linewidth=2,
label=labelString)
elif plots[s].type == 'Surface':
X = np.outer(plots[s].x, np.ones(len(plots[s].t)))
T = np.outer(np.ones(len(plots[s].x)), plots[s].t)
axes[ip].plot_surface(T, X, plots[s].y.T, cmap='viridis', edgecolor='none')
plt.grid(True)
labels[ip] = labels[ip] + plots[s].label + ' / '
for ip in range(nPlots):
axes[ip].set_xlabel('t')
if plots[ip].type == 'Lines':
axes[ip].set_ylabel(labels[ip][:-2])
if plots[ip].flagLegend:
axes[ip].legend()
elif plots[ip].type == 'Surface':
axes[ip].set_ylabel('x')
if fOut is not None:
plt.savefig(fOut)
if showPlot:
plt.show()
return fig
def plotPhase2D(y, fOut=None, showPlot=True):
fig = plt.figure()
plt.plot(y[:, 0], y[:, 1])
plt.grid(True)
if fOut is not None:
plt.savefig(fOut)
if showPlot:
plt.show()
return fig
def surfacePlot(x, t, y, fOut=None, showPlot=True):
X = np.outer(x, np.ones(len(t)))
T = np.outer(np.ones(len(x)), t)
fig = plt.figure()
ax = plt.axes(projection='3d')
ax.plot_surface(T, X, y, cmap='viridis', edgecolor='none')
# ax.set_title('Surface plot')
plt.grid(True)
plt.set_xlabel('t')
plt.set_ylabel('x')
plt.set_zlabel('y')
if fOut is not None:
plt.savefig(fOut)
if showPlot:
plt.show()
return fig
def plotLines3D():
pass
def plotPhase3D(x, y, z):
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.plot(x, y, z)
plt.show()