Skip to content

Commit 9053746

Browse files
committed
update gui to allow more custom drawing
1 parent 64b92c0 commit 9053746

File tree

1 file changed

+78
-53
lines changed
  • src/py_eddy_tracker/appli

1 file changed

+78
-53
lines changed

src/py_eddy_tracker/appli/gui.py

Lines changed: 78 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
from matplotlib import pyplot
1111
from matplotlib.animation import FuncAnimation
12+
from matplotlib.axes import Axes
1213
from matplotlib.collections import LineCollection
1314
from numpy import arange, where
1415

@@ -36,6 +37,8 @@ def __init__(
3637
self.field_color = None
3738
self.field_txt = None
3839
self.time_field = False
40+
self.txt = None
41+
self.ax = None
3942
self.setup(**kwargs)
4043

4144
def setup(
@@ -47,41 +50,55 @@ def setup(
4750
range_color=(None, None),
4851
nb_step=25,
4952
figsize=(8, 6),
53+
position=(0.05, 0.05, 0.9, 0.9),
5054
**kwargs,
5155
):
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)
6572
self.nb_step = nb_step
6673

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
7174
# 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)
7379
t0, t1 = self.period
7480
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+
)
8195
self.segs = list()
8296
self.t_segs = list()
8397
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)
85102
self.ax.add_collection(self.contour)
86103

87104
self.fig.canvas.draw()
@@ -148,29 +165,33 @@ def func_animation(self, frame):
148165

149166
def update(self):
150167
m = self.t == self.now
168+
color = self.field_color is not None
151169
if m.sum():
152170
segs = list()
153171
t = list()
154172
c = list()
155173
for i in where(m)[0]:
156174
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])
158177
t.append(self.now)
159178
self.segs.append(segs)
160-
self.c_segs.append(c)
179+
if color:
180+
self.c_segs.append(c)
161181
self.t_segs.append(t)
162182
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+
)
170192
)
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))))
174195
# linewidth will be link to time delay
175196
self.contour.set_lw(
176197
[
@@ -179,28 +200,32 @@ def update(self):
179200
]
180201
)
181202
# 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)
186209
# 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)
197221
self.ax.draw_artist(self.contour)
198-
self.ax.draw_artist(self.txt)
222+
199223
# Remove first segment to keep only T contour
200224
if len(self.segs) > self.nb_step:
201225
self.segs.pop(0)
202226
self.t_segs.pop(0)
203-
self.c_segs.pop(0)
227+
if color:
228+
self.c_segs.pop(0)
204229

205230
def draw_contour(self):
206231
# select contour for this time step

0 commit comments

Comments
 (0)