Skip to content

Commit a92c87b

Browse files
committed
Anim example simplify
1 parent c46880c commit a92c87b

File tree

3 files changed

+21
-74
lines changed

3 files changed

+21
-74
lines changed

examples/08_tracking_manipulation/pet_track_anim.py

Lines changed: 14 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,8 @@
55
Run in a terminal this script, which allow to watch eddy evolution
66
77
"""
8-
from matplotlib import pyplot as plt
9-
from matplotlib.collections import LineCollection
10-
from numpy import arange
118
from py_eddy_tracker.observations.tracking import TrackEddiesObservations
12-
from py_eddy_tracker.poly import create_vertice
9+
from py_eddy_tracker.appli import Anim
1310
import py_eddy_tracker_sample
1411

1512
# %%
@@ -18,51 +15,18 @@
1815
py_eddy_tracker_sample.get_path("eddies_med_adt_allsat_dt2018/Anticyclonic.zarr")
1916
)
2017
eddy = a.extract_ids([9672])
21-
t0, t1 = eddy.period
22-
t = eddy.time
23-
x = eddy["contour_lon_s"]
24-
y = eddy["contour_lat_s"]
2518

2619
# %%
27-
# General value
28-
T = 25.0
29-
cmap = plt.get_cmap("viridis")
30-
COLORS = cmap(arange(T + 1) / T)
31-
32-
33-
# %%
34-
# plot
35-
fig = plt.figure(figsize=(12, 5))
36-
ax = fig.add_axes((0.05, 0.05, 0.9, 0.9))
37-
ax.set_xlim(16.5, 23)
38-
ax.set_ylim(34.5, 37)
39-
ax.set_aspect("equal")
40-
ax.grid()
41-
# init mappable
42-
txt = ax.text(16.6, 36.8, "", zorder=10)
43-
segs = list()
44-
c = LineCollection([], zorder=1)
45-
ax.add_collection(c)
46-
47-
fig.canvas.draw()
48-
plt.show(block=False)
49-
# save background for future bliting
50-
bg_cache = fig.canvas.copy_from_bbox(ax.bbox)
51-
# display contour every 2 day
52-
for t_ in range(t0, t1 + 1, 2):
53-
fig.canvas.restore_region(bg_cache)
54-
# select contour for this time step
55-
m = t == t_
56-
segs.append(create_vertice(x[m][0], y[m][0]))
57-
c.set_paths(segs)
58-
c.set_color(COLORS[-len(segs) :])
59-
txt.set_text(f"{t0} -> {t_} -> {t1}")
60-
ax.draw_artist(c)
61-
ax.draw_artist(txt)
62-
# Remove first segment to keep only T contour
63-
if len(segs) > T:
64-
segs.pop(0)
65-
# paint updated artist
66-
fig.canvas.blit(ax.bbox)
67-
68-
fig.canvas.start_event_loop(1e-10)
20+
# Run animation
21+
# Key shortcut
22+
# Escape => exit
23+
# SpaceBar => pause
24+
# left arrow => t - 1
25+
# right arrow => t + 1
26+
# + => speed increase of 10 %
27+
# - => speed decrease of 10 %
28+
a = Anim(eddy, sleep_event=1e-10, intern=True, figsize=(8, 3.5), cmap="viridis")
29+
a.txt.set_position((17, 34.6))
30+
a.ax.set_xlim(16.5, 23)
31+
a.ax.set_ylim(34.5, 37)
32+
a.show(infinity_loop=False)

notebooks/python_module/08_tracking_manipulation/pet_track_anim.ipynb

Lines changed: 4 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
},
2727
"outputs": [],
2828
"source": [
29-
"from matplotlib import pyplot as plt\nfrom matplotlib.collections import LineCollection\nfrom numpy import arange\nfrom py_eddy_tracker.observations.tracking import TrackEddiesObservations\nfrom py_eddy_tracker.poly import create_vertice\nimport py_eddy_tracker_sample"
29+
"from py_eddy_tracker.observations.tracking import TrackEddiesObservations\nfrom py_eddy_tracker.appli import Anim\nimport py_eddy_tracker_sample"
3030
]
3131
},
3232
{
@@ -44,14 +44,14 @@
4444
},
4545
"outputs": [],
4646
"source": [
47-
"a = TrackEddiesObservations.load_file(\n py_eddy_tracker_sample.get_path(\"eddies_med_adt_allsat_dt2018/Anticyclonic.zarr\")\n)\neddy = a.extract_ids([9672])\nt0, t1 = eddy.period\nt = eddy.time\nx = eddy[\"contour_lon_s\"]\ny = eddy[\"contour_lat_s\"]"
47+
"a = TrackEddiesObservations.load_file(\n py_eddy_tracker_sample.get_path(\"eddies_med_adt_allsat_dt2018/Anticyclonic.zarr\")\n)\neddy = a.extract_ids([9672])"
4848
]
4949
},
5050
{
5151
"cell_type": "markdown",
5252
"metadata": {},
5353
"source": [
54-
"General value\n\n"
54+
"Run animation\nKey shortcut\n Escape => exit\n SpaceBar => pause\n left arrow => t - 1\n right arrow => t + 1\n + => speed increase of 10 %\n - => speed decrease of 10 %\n\n"
5555
]
5656
},
5757
{
@@ -62,25 +62,7 @@
6262
},
6363
"outputs": [],
6464
"source": [
65-
"T = 25.0\ncmap = plt.get_cmap(\"viridis\")\nCOLORS = cmap(arange(T + 1) / T)"
66-
]
67-
},
68-
{
69-
"cell_type": "markdown",
70-
"metadata": {},
71-
"source": [
72-
"plot\n\n"
73-
]
74-
},
75-
{
76-
"cell_type": "code",
77-
"execution_count": null,
78-
"metadata": {
79-
"collapsed": false
80-
},
81-
"outputs": [],
82-
"source": [
83-
"fig = plt.figure(figsize=(12, 5))\nax = fig.add_axes((0.05, 0.05, 0.9, 0.9))\nax.set_xlim(16.5, 23)\nax.set_ylim(34.5, 37)\nax.set_aspect(\"equal\")\nax.grid()\n# init mappable\ntxt = ax.text(16.6, 36.8, \"\", zorder=10)\nsegs = list()\nc = LineCollection([], zorder=1)\nax.add_collection(c)\n\nfig.canvas.draw()\nplt.show(block=False)\n# save background for future bliting\nbg_cache = fig.canvas.copy_from_bbox(ax.bbox)\n# display contour every 2 day\nfor t_ in range(t0, t1 + 1, 2):\n fig.canvas.restore_region(bg_cache)\n # select contour for this time step\n m = t == t_\n segs.append(create_vertice(x[m][0], y[m][0]))\n c.set_paths(segs)\n c.set_color(COLORS[-len(segs) :])\n txt.set_text(f\"{t0} -> {t_} -> {t1}\")\n ax.draw_artist(c)\n ax.draw_artist(txt)\n # Remove first segment to keep only T contour\n if len(segs) > T:\n segs.pop(0)\n # paint updated artist\n fig.canvas.blit(ax.bbox)\n\n fig.canvas.start_event_loop(1e-10)"
65+
"a = Anim(eddy, sleep_event=1e-10, intern=True, figsize=(8, 3.5), cmap=\"viridis\")\na.txt.set_position((17, 34.6))\na.ax.set_xlim(16.5, 23)\na.ax.set_ylim(34.5, 37)\na.show(infinity_loop=False)"
8466
]
8567
}
8668
],

src/py_eddy_tracker/appli.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ def __init__(self, eddy, intern=False, sleep_event=0.1, **kwargs):
7373
self.sleep_event = sleep_event
7474
self.setup(**kwargs)
7575

76-
def setup(self, cmap="jet", nb_step=25):
76+
def setup(self, cmap="jet", nb_step=25, figsize=(8, 6)):
7777
cmap = pyplot.get_cmap(cmap)
7878
self.colors = cmap(arange(nb_step + 1) / nb_step)
7979
self.nb_step = nb_step
@@ -88,7 +88,7 @@ def setup(self, cmap="jet", nb_step=25):
8888
y_max += 0.05 * d_y
8989

9090
# plot
91-
self.fig = pyplot.figure()
91+
self.fig = pyplot.figure(figsize=figsize)
9292
self.ax = self.fig.add_axes((0.05, 0.05, 0.9, 0.9))
9393
self.ax.set_xlim(x_min, x_max), self.ax.set_ylim(y_min, y_max)
9494
self.ax.set_aspect("equal")
@@ -113,6 +113,7 @@ def setup(self, cmap="jet", nb_step=25):
113113
def show(self, infinity_loop=False):
114114
pyplot.show(block=False)
115115
# save background for future bliting
116+
self.fig.canvas.draw()
116117
self.bg_cache = self.fig.canvas.copy_from_bbox(self.ax.bbox)
117118
loop = True
118119
t0, t1 = self.period

0 commit comments

Comments
 (0)