This repository has been archived by the owner on Apr 5, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplot_func.py
78 lines (66 loc) · 2.18 KB
/
plot_func.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# -*- coding: utf-8 -*-
"""Plotting functions."""
from pathlib import Path
import iris
import matplotlib.pyplot as plt
import numpy as np
from aeolus.calc import spatial_mean
from aeolus.model import um
# Locations of grid lines on maps
XLOCS = np.arange(-180, 181, 90)
YLOCS = np.arange(-90, 91, 30)
def cube_stats_string(cube, sep=" | ", fmt="auto", model=um):
"""Return min, mean and max of an `iris.cube.Cube` as a string."""
# Compute the stats
_min = cube.collapsed([model.y, model.x], iris.analysis.MIN)
_mean = spatial_mean(cube, model=model)
_max = cube.collapsed([model.y, model.x], iris.analysis.MAX)
_min = float(_min.data)
_mean = float(_mean.data)
_max = float(_max.data)
# Assemble a string
txts = []
if fmt != "auto":
txts.append(f"min={_min:{fmt}}")
txts.append(f"mean={_mean:{fmt}}")
txts.append(f"max={_max:{fmt}}")
else:
if (np.log10(abs(_mean)) < 0) or (np.log10(abs(_mean)) > 5):
txts.append(f"min={_min:.0e}")
txts.append(f"mean={_mean:.0e}")
txts.append(f"max={_max:.0e}")
else:
txts.append(f"min={np.round(_min):.0f}")
txts.append(f"mean={np.round(_mean):.0f}")
txts.append(f"max={np.round(_max):.0f}")
return sep.join(txts)
def figsave(fig, imgname, stamp=True, **kw_savefig):
"""Save figure and print relative path to it."""
if stamp:
fig.suptitle(
imgname.name,
x=0.5,
y=0.05,
ha="center",
fontsize="xx-small",
color="tab:grey",
alpha=0.5,
)
save_dir = imgname.absolute().parent
save_dir.mkdir(parents=True, exist_ok=True)
fig.savefig(imgname, **kw_savefig)
pth = Path.cwd()
rel_path = None
pref = ""
for par in pth.parents:
pref += ".." + pth.anchor
try:
rel_path = f"{pref}{imgname.relative_to(par)}"
break
except ValueError:
pass
if rel_path is not None:
print(f"Saved to {rel_path}.{plt.rcParams['savefig.format']}")
def use_style():
"""Load custom matplotlib style sheet."""
plt.style.use("simple.mplstyle")