Skip to content

Commit 0f80506

Browse files
committed
Add new method of observation display
1 parent e47637f commit 0f80506

File tree

5 files changed

+133
-9
lines changed

5 files changed

+133
-9
lines changed

doc/conf.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -326,4 +326,8 @@
326326

327327

328328
# Example configuration for intersphinx: refer to the Python standard library.
329-
intersphinx_mapping = {"https://docs.python.org/": None}
329+
intersphinx_mapping = {
330+
"numpy": ("https://numpy.org/doc/stable/", None),
331+
"python": ("https://docs.python.org/3/", None),
332+
"matplotlib": ("https://matplotlib.org/", None),
333+
}

examples/02_eddy_identification/pet_display_id.py

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,29 @@
1414
c = EddiesObservations.load_file(data.get_path("Cyclonic_20190223.nc"))
1515

1616
# %%
17-
# Plot
17+
# draw contour
1818
fig = plt.figure(figsize=(15, 8))
19-
ax = fig.add_subplot(111)
19+
ax = fig.add_axes([0.03, 0.03, 0.94, 0.94])
2020
ax.set_aspect("equal")
2121
ax.set_xlim(0, 360)
2222
ax.set_ylim(-80, 80)
2323
a.display(ax, label="Anticyclonic", color="r", lw=1)
2424
c.display(ax, label="Cyclonic", color="b", lw=1)
2525
ax.legend(loc="upper right")
26+
27+
# %%
28+
# filled contour
29+
fig = plt.figure(figsize=(15, 8))
30+
ax = fig.add_axes([0.03, 0.03, 0.90, 0.94])
31+
ax.set_aspect("equal")
32+
ax.set_xlim(0, 140)
33+
ax.set_ylim(-80, 0)
34+
a.display(ax, label="Anticyclonic", color="r", lw=1)
35+
m = a.filled(ax, "amplitude", cmap="magma_r", vmin=0, vmax=.5)
36+
plt.colorbar(m, cax=ax.figure.add_axes([0.95, 0.05, 0.01, 0.9]))
37+
plt.show()
38+
# %%
39+
# Get general informations
40+
print(a)
41+
# %%
42+
print(c)

notebooks/python_module/02_eddy_identification/pet_display_id.ipynb

Lines changed: 50 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
"cell_type": "markdown",
1616
"metadata": {},
1717
"source": [
18-
"\nDisplay identification\n======================\n"
18+
"\n# Display identification\n"
1919
]
2020
},
2121
{
@@ -51,7 +51,7 @@
5151
"cell_type": "markdown",
5252
"metadata": {},
5353
"source": [
54-
"Plot\n\n"
54+
"draw contour\n\n"
5555
]
5656
},
5757
{
@@ -62,7 +62,54 @@
6262
},
6363
"outputs": [],
6464
"source": [
65-
"fig = plt.figure(figsize=(15, 8))\nax = fig.add_subplot(111)\nax.set_aspect(\"equal\")\nax.set_xlim(0, 360)\nax.set_ylim(-80, 80)\na.display(ax, label=\"Anticyclonic\", color=\"r\", lw=1)\nc.display(ax, label=\"Cyclonic\", color=\"b\", lw=1)\nax.legend(loc=\"upper right\")"
65+
"fig = plt.figure(figsize=(15, 8))\nax = fig.add_axes([0.03, 0.03, 0.94, 0.94])\nax.set_aspect(\"equal\")\nax.set_xlim(0, 360)\nax.set_ylim(-80, 80)\na.display(ax, label=\"Anticyclonic\", color=\"r\", lw=1)\nc.display(ax, label=\"Cyclonic\", color=\"b\", lw=1)\nax.legend(loc=\"upper right\")"
66+
]
67+
},
68+
{
69+
"cell_type": "markdown",
70+
"metadata": {},
71+
"source": [
72+
"filled contour\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=(15, 8))\nax = fig.add_axes([0.03, 0.03, 0.90, 0.94])\nax.set_aspect(\"equal\")\nax.set_xlim(0, 140)\nax.set_ylim(-80, 0)\na.display(ax, label=\"Anticyclonic\", color=\"r\", lw=1)\nm = a.filled(ax, \"amplitude\", cmap=\"magma_r\", vmin=0, vmax=.5)\nplt.colorbar(m, cax=ax.figure.add_axes([0.95, 0.05, 0.01, 0.9]))\nplt.show()"
84+
]
85+
},
86+
{
87+
"cell_type": "markdown",
88+
"metadata": {},
89+
"source": [
90+
"Get general informations\n\n"
91+
]
92+
},
93+
{
94+
"cell_type": "code",
95+
"execution_count": null,
96+
"metadata": {
97+
"collapsed": false
98+
},
99+
"outputs": [],
100+
"source": [
101+
"print(a)"
102+
]
103+
},
104+
{
105+
"cell_type": "code",
106+
"execution_count": null,
107+
"metadata": {
108+
"collapsed": false
109+
},
110+
"outputs": [],
111+
"source": [
112+
"print(c)"
66113
]
67114
}
68115
],

src/py_eddy_tracker/observations/observation.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,9 @@
6060
from tokenize import TokenError
6161
from tarfile import ExFileObject
6262
from matplotlib.path import Path as BasePath
63+
from matplotlib.collections import PolyCollection
64+
from matplotlib.cm import get_cmap
65+
from matplotlib.colors import Normalize
6366
from .. import VAR_DESCR, VAR_DESCR_inv, __version__
6467
from ..generic import (
6568
distance_grid,
@@ -1342,9 +1345,62 @@ def scatter(self, ax, name, ref=None, factor=1, **kwargs):
13421345
x = (x - ref) % 360 + ref
13431346
return ax.scatter(x, self.latitude, c=self[name] * factor, **kwargs)
13441347

1348+
def filled(
1349+
self,
1350+
ax,
1351+
varname,
1352+
intern=False,
1353+
cmap="magma_r",
1354+
lut=10,
1355+
vmin=None,
1356+
vmax=None,
1357+
**kwargs,
1358+
):
1359+
"""
1360+
:param matplotlib.axes.Axes ax: matplotlib axes use to draw
1361+
:param str varname: var which will be use to fill contour
1362+
:param bool intern: if True draw speed contour instead of effective contour
1363+
:param str cmap: matplotlib colormap name
1364+
:param int,None lut: Number of division of colormaps
1365+
:param float,None vmin:
1366+
:param float,None vmax:
1367+
:return: Collection drawed
1368+
:rtype: matplotlib.collections.PolyCollection
1369+
1370+
.. minigallery:: py_eddy_tracker.EddiesObservations.filled
1371+
"""
1372+
cmap = get_cmap(cmap, lut)
1373+
x_name, y_name = self.intern(intern)
1374+
x, y, v = self[x_name], self[y_name], self[varname]
1375+
if vmin is None:
1376+
vmin = v.min()
1377+
if vmax is None:
1378+
vmax = v.max()
1379+
v = (v - vmin) / (vmax - vmin)
1380+
verts = list()
1381+
colors = list()
1382+
for x_, y_, v_ in zip(x, y, v):
1383+
verts.append(create_vertice(x_, y_))
1384+
colors.append(cmap(v_))
1385+
c = PolyCollection(verts, facecolors=colors, **kwargs)
1386+
ax.add_collection(c)
1387+
c.cmap = cmap
1388+
c.norm = Normalize(vmin=vmin, vmax=vmax)
1389+
return c
1390+
13451391
def display(
13461392
self, ax, ref=None, extern_only=False, intern_only=False, nobs=True, **kwargs
13471393
):
1394+
"""
1395+
:param matplotlib.axes.Axes ax: matplotlib axes use to draw
1396+
:param float,None ref: if define use like west bound
1397+
:param bool extern_only: if True draw effective contour only
1398+
:param bool intern_only: if True draw speed contour only
1399+
:param bool nobs: if True add number of eddies in label
1400+
:param dict kwargs: look at :py:meth:`matplotlib.axes.Axes.plot`
1401+
1402+
.. minigallery:: py_eddy_tracker.EddiesObservations.display
1403+
"""
13481404
if not extern_only:
13491405
lon_s = flatten_line_matrix(self.obs["contour_lon_s"])
13501406
lat_s = flatten_line_matrix(self.obs["contour_lat_s"])

src/py_eddy_tracker/observations/tracking.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ def extract_with_area(self, area, **kwargs):
237237
:return: Return all eddy tracks which are in bounds
238238
:rtype: TrackEddiesObservations
239239
240-
.. minigallery:: py_eddy_tracker.observations.tracking.TrackEddiesObservations.extract_with_area
240+
.. minigallery:: py_eddy_tracker.TrackEddiesObservations.extract_with_area
241241
"""
242242
mask = (self.latitude > area["llcrnrlat"]) * (self.latitude < area["urcrnrlat"])
243243
lon0 = area["llcrnrlon"]
@@ -254,7 +254,7 @@ def extract_with_period(self, period, **kwargs):
254254
:return: Return all eddy tracks which are in bounds
255255
:rtype: TrackEddiesObservations
256256
257-
.. minigallery:: py_eddy_tracker.observations.tracking.TrackEddiesObservations.extract_with_period
257+
.. minigallery:: py_eddy_tracker.TrackEddiesObservations.extract_with_period
258258
"""
259259
dataset_period = self.period
260260
p_min, p_max = period
@@ -336,7 +336,7 @@ def extract_with_length(self, bounds):
336336
:return: Return all eddy tracks which have length between bounds
337337
:rtype: TrackEddiesObservations
338338
339-
.. minigallery:: py_eddy_tracker.observations.tracking.TrackEddiesObservations.extract_with_length
339+
.. minigallery:: py_eddy_tracker.TrackEddiesObservations.extract_with_length
340340
"""
341341
b0, b1 = bounds
342342
if b0 >= 0 and b1 >= 0:

0 commit comments

Comments
 (0)