2121===========================================================================
2222"""
2323
24- from numpy import arange , empty
24+ from numpy import arange , empty , where
2525from matplotlib import pyplot
2626from matplotlib .collections import LineCollection
2727from datetime import datetime
@@ -36,10 +36,11 @@ def __init__(self, eddy, intern=False, sleep_event=0.1, **kwargs):
3636 self .eddy = eddy
3737 x_name , y_name = eddy .intern (intern )
3838 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 " ]
4040 self .pause = False
4141 self .period = self .eddy .period
4242 self .sleep_event = sleep_event
43+ self .mappables = list ()
4344 self .setup (** kwargs )
4445
4546 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)):
6667
6768 self .fig .canvas .draw ()
6869 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 )
6983
7084 def show (self , infinity_loop = False ):
7185 pyplot .show (block = False )
@@ -107,6 +121,8 @@ def prev(self):
107121 def draw_contour (self ):
108122 # t0, t1 = self.period
109123 # select contour for this time step
124+ while self .mappables :
125+ self .mappables .pop ().remove ()
110126 m = self .t == self .now
111127 self .ax .figure .canvas .restore_region (self .bg_cache )
112128 if m .sum ():
@@ -121,6 +137,12 @@ def draw_contour(self):
121137 self .contour .set_color (self .colors [- len (self .segs ) :])
122138 self .contour .set_lw (arange (len (self .segs )) / len (self .segs ) * 2.5 )
123139 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 )
124146 self .ax .draw_artist (self .contour )
125147 self .ax .draw_artist (self .txt )
126148 # Remove first segment to keep only T contour
@@ -162,7 +184,7 @@ def anim():
162184 "--keep_step" , default = 25 , help = "number maximal of step displayed" , type = int
163185 )
164186 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" )
166188 parser .add_argument (
167189 "--time_sleep" ,
168190 type = float ,
@@ -179,7 +201,9 @@ def anim():
179201 eddies = TrackEddiesObservations .load_file (args .filename , include_vars = variables )
180202 if not args .all :
181203 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+ )
183207 eddies = eddies .extract_ids (args .id )
184208 a = Anim (
185209 eddies ,
0 commit comments