9
9
10
10
from matplotlib import pyplot
11
11
from matplotlib .animation import FuncAnimation
12
+ from matplotlib .axes import Axes
12
13
from matplotlib .collections import LineCollection
13
14
from numpy import arange , where
14
15
@@ -36,6 +37,8 @@ def __init__(
36
37
self .field_color = None
37
38
self .field_txt = None
38
39
self .time_field = False
40
+ self .txt = None
41
+ self .ax = None
39
42
self .setup (** kwargs )
40
43
41
44
def setup (
@@ -47,41 +50,55 @@ def setup(
47
50
range_color = (None , None ),
48
51
nb_step = 25 ,
49
52
figsize = (8 , 6 ),
53
+ position = (0.05 , 0.05 , 0.9 , 0.9 ),
50
54
** kwargs ,
51
55
):
52
- self .field_color = self .eddy [field_color ].astype ("f4" )
53
- self .field_txt = self .eddy [field_txt ]
54
- rg = range_color
55
- if rg [0 ] is None and rg [1 ] is None and field_color == "time" :
56
- self .time_field = True
57
- else :
58
- rg = (
59
- self .field_color .min () if rg [0 ] is None else rg [0 ],
60
- self .field_color .max () if rg [1 ] is None else rg [1 ],
61
- )
62
- self .field_color = (self .field_color - rg [0 ]) / (rg [1 ] - rg [0 ])
63
-
64
- self .colors = pyplot .get_cmap (cmap , lut = lut )
56
+ # To text each visible eddy
57
+ if field_txt :
58
+ self .field_txt = self .eddy [field_txt ]
59
+ if field_color :
60
+ # To color each visible eddy
61
+ self .field_color = self .eddy [field_color ].astype ("f4" )
62
+ rg = range_color
63
+ if rg [0 ] is None and rg [1 ] is None and field_color == "time" :
64
+ self .time_field = True
65
+ else :
66
+ rg = (
67
+ self .field_color .min () if rg [0 ] is None else rg [0 ],
68
+ self .field_color .max () if rg [1 ] is None else rg [1 ],
69
+ )
70
+ self .field_color = (self .field_color - rg [0 ]) / (rg [1 ] - rg [0 ])
71
+ self .colors = pyplot .get_cmap (cmap , lut = lut )
65
72
self .nb_step = nb_step
66
73
67
- x_min , x_max = self .x_core .min () - 2 , self .x_core .max () + 2
68
- d_x = x_max - x_min
69
- y_min , y_max = self .y_core .min () - 2 , self .y_core .max () + 2
70
- d_y = y_max - y_min
71
74
# plot
72
- self .fig = pyplot .figure (figsize = figsize , ** kwargs )
75
+ if "figure" in kwargs :
76
+ self .fig = kwargs .pop ("figure" )
77
+ else :
78
+ self .fig = pyplot .figure (figsize = figsize , ** kwargs )
73
79
t0 , t1 = self .period
74
80
self .fig .suptitle (f"{ t0 } -> { t1 } " )
75
- self .ax = self .fig .add_axes ((0.05 , 0.05 , 0.9 , 0.9 ), projection = "full_axes" )
76
- self .ax .set_xlim (x_min , x_max ), self .ax .set_ylim (y_min , y_max )
77
- self .ax .set_aspect ("equal" )
78
- self .ax .grid ()
79
- # init mappable
80
- self .txt = self .ax .text (x_min + 0.05 * d_x , y_min + 0.05 * d_y , "" , zorder = 10 )
81
+ if isinstance (position , Axes ):
82
+ self .ax = position
83
+ else :
84
+ x_min , x_max = self .x_core .min () - 2 , self .x_core .max () + 2
85
+ d_x = x_max - x_min
86
+ y_min , y_max = self .y_core .min () - 2 , self .y_core .max () + 2
87
+ d_y = y_max - y_min
88
+ self .ax = self .fig .add_axes (position , projection = "full_axes" )
89
+ self .ax .set_xlim (x_min , x_max ), self .ax .set_ylim (y_min , y_max )
90
+ self .ax .set_aspect ("equal" )
91
+ self .ax .grid ()
92
+ self .txt = self .ax .text (
93
+ x_min + 0.05 * d_x , y_min + 0.05 * d_y , "" , zorder = 10
94
+ )
81
95
self .segs = list ()
82
96
self .t_segs = list ()
83
97
self .c_segs = list ()
84
- self .contour = LineCollection ([], zorder = 1 )
98
+ if field_color is None :
99
+ self .contour = LineCollection ([], zorder = 1 , color = "gray" )
100
+ else :
101
+ self .contour = LineCollection ([], zorder = 1 )
85
102
self .ax .add_collection (self .contour )
86
103
87
104
self .fig .canvas .draw ()
@@ -148,29 +165,33 @@ def func_animation(self, frame):
148
165
149
166
def update (self ):
150
167
m = self .t == self .now
168
+ color = self .field_color is not None
151
169
if m .sum ():
152
170
segs = list ()
153
171
t = list ()
154
172
c = list ()
155
173
for i in where (m )[0 ]:
156
174
segs .append (create_vertice (self .x [i ], self .y [i ]))
157
- c .append (self .field_color [i ])
175
+ if color :
176
+ c .append (self .field_color [i ])
158
177
t .append (self .now )
159
178
self .segs .append (segs )
160
- self .c_segs .append (c )
179
+ if color :
180
+ self .c_segs .append (c )
161
181
self .t_segs .append (t )
162
182
self .contour .set_paths (chain (* self .segs ))
163
- if self .time_field :
164
- self .contour .set_color (
165
- self .colors (
166
- [
167
- (self .nb_step - self .now + i ) / self .nb_step
168
- for i in chain (* self .c_segs )
169
- ]
183
+ if color :
184
+ if self .time_field :
185
+ self .contour .set_color (
186
+ self .colors (
187
+ [
188
+ (self .nb_step - self .now + i ) / self .nb_step
189
+ for i in chain (* self .c_segs )
190
+ ]
191
+ )
170
192
)
171
- )
172
- else :
173
- self .contour .set_color (self .colors (list (chain (* self .c_segs ))))
193
+ else :
194
+ self .contour .set_color (self .colors (list (chain (* self .c_segs ))))
174
195
# linewidth will be link to time delay
175
196
self .contour .set_lw (
176
197
[
@@ -179,28 +200,32 @@ def update(self):
179
200
]
180
201
)
181
202
# Update date txt and info
182
- txt = f"{ (timedelta (int (self .now )) + datetime (1950 ,1 ,1 )).strftime ('%Y/%m/%d' )} "
183
- if self .graphic_informations :
184
- txt += f"- { 1 / self .sleep_event :.0f} frame/s"
185
- self .txt .set_text (txt )
203
+ if self .txt is not None :
204
+ txt = f"{ (timedelta (int (self .now )) + datetime (1950 ,1 ,1 )).strftime ('%Y/%m/%d' )} "
205
+ if self .graphic_informations :
206
+ txt += f"- { 1 / self .sleep_event :.0f} frame/s"
207
+ self .txt .set_text (txt )
208
+ self .ax .draw_artist (self .txt )
186
209
# Update id txt
187
- for i in where (m )[0 ]:
188
- mappable = self .ax .text (
189
- self .x_core [i ],
190
- self .y_core [i ],
191
- self .field_txt [i ],
192
- fontsize = 12 ,
193
- fontweight = "demibold" ,
194
- )
195
- self .mappables .append (mappable )
196
- self .ax .draw_artist (mappable )
210
+ if self .field_txt is not None :
211
+ for i in where (m )[0 ]:
212
+ mappable = self .ax .text (
213
+ self .x_core [i ],
214
+ self .y_core [i ],
215
+ self .field_txt [i ],
216
+ fontsize = 12 ,
217
+ fontweight = "demibold" ,
218
+ )
219
+ self .mappables .append (mappable )
220
+ self .ax .draw_artist (mappable )
197
221
self .ax .draw_artist (self .contour )
198
- self . ax . draw_artist ( self . txt )
222
+
199
223
# Remove first segment to keep only T contour
200
224
if len (self .segs ) > self .nb_step :
201
225
self .segs .pop (0 )
202
226
self .t_segs .pop (0 )
203
- self .c_segs .pop (0 )
227
+ if color :
228
+ self .c_segs .pop (0 )
204
229
205
230
def draw_contour (self ):
206
231
# select contour for this time step
0 commit comments