21
21
===========================================================================
22
22
"""
23
23
24
- from numpy import arange , empty
24
+ from numpy import arange , empty , where
25
25
from matplotlib import pyplot
26
26
from matplotlib .collections import LineCollection
27
27
from datetime import datetime
@@ -36,10 +36,11 @@ def __init__(self, eddy, intern=False, sleep_event=0.1, **kwargs):
36
36
self .eddy = eddy
37
37
x_name , y_name = eddy .intern (intern )
38
38
self .t , self .x , self .y = eddy .time , eddy [x_name ], eddy [y_name ]
39
- self .x_core , self .y_core = eddy ["lon" ], eddy ["lat" ]
39
+ self .x_core , self .y_core , self . track = eddy ["lon" ], eddy ["lat" ], eddy [ "track " ]
40
40
self .pause = False
41
41
self .period = self .eddy .period
42
42
self .sleep_event = sleep_event
43
+ self .mappables = list ()
43
44
self .setup (** kwargs )
44
45
45
46
def setup (self , cmap = "jet" , nb_step = 25 , figsize = (8 , 6 )):
@@ -66,6 +67,19 @@ def setup(self, cmap="jet", nb_step=25, figsize=(8, 6)):
66
67
67
68
self .fig .canvas .draw ()
68
69
self .fig .canvas .mpl_connect ("key_press_event" , self .keyboard )
70
+ self .fig .canvas .mpl_connect ("resize_event" , self .reset_bliting )
71
+
72
+ def reset_bliting (self , event ):
73
+ self .contour .set_visible (False )
74
+ self .txt .set_visible (False )
75
+ for m in self .mappables :
76
+ m .set_visible (False )
77
+ self .fig .canvas .draw ()
78
+ self .bg_cache = self .fig .canvas .copy_from_bbox (self .ax .bbox )
79
+ self .contour .set_visible (True )
80
+ self .txt .set_visible (True )
81
+ for m in self .mappables :
82
+ m .set_visible (True )
69
83
70
84
def show (self , infinity_loop = False ):
71
85
pyplot .show (block = False )
@@ -107,6 +121,8 @@ def prev(self):
107
121
def draw_contour (self ):
108
122
# t0, t1 = self.period
109
123
# select contour for this time step
124
+ while self .mappables :
125
+ self .mappables .pop ().remove ()
110
126
m = self .t == self .now
111
127
self .ax .figure .canvas .restore_region (self .bg_cache )
112
128
if m .sum ():
@@ -121,6 +137,12 @@ def draw_contour(self):
121
137
self .contour .set_color (self .colors [- len (self .segs ) :])
122
138
self .contour .set_lw (arange (len (self .segs )) / len (self .segs ) * 2.5 )
123
139
self .txt .set_text (f"{ self .now } - { 1 / self .sleep_event :.0f} frame/s" )
140
+ for i in where (m )[0 ]:
141
+ mappable = self .ax .text (
142
+ self .x_core [i ], self .y_core [i ], self .track [i ], fontsize = 8
143
+ )
144
+ self .mappables .append (mappable )
145
+ self .ax .draw_artist (mappable )
124
146
self .ax .draw_artist (self .contour )
125
147
self .ax .draw_artist (self .txt )
126
148
# Remove first segment to keep only T contour
@@ -162,7 +184,7 @@ def anim():
162
184
"--keep_step" , default = 25 , help = "number maximal of step displayed" , type = int
163
185
)
164
186
parser .add_argument ("--cmap" , help = "matplotlib colormap used" )
165
- parser .add_argument ("--all" , help = "All eddies will be drawed" , action = ' store_true' )
187
+ parser .add_argument ("--all" , help = "All eddies will be drawed" , action = " store_true" )
166
188
parser .add_argument (
167
189
"--time_sleep" ,
168
190
type = float ,
@@ -179,7 +201,9 @@ def anim():
179
201
eddies = TrackEddiesObservations .load_file (args .filename , include_vars = variables )
180
202
if not args .all :
181
203
if len (args .id ) == 0 :
182
- raise Exception ('You need to specify id to display or ask explicity all with --all option' )
204
+ raise Exception (
205
+ "You need to specify id to display or ask explicity all with --all option"
206
+ )
183
207
eddies = eddies .extract_ids (args .id )
184
208
a = Anim (
185
209
eddies ,
0 commit comments