Skip to content

Commit 93efb8c

Browse files
committed
Update: get rid of cartopy top-level imports
1 parent 13812e7 commit 93efb8c

File tree

1 file changed

+32
-22
lines changed

1 file changed

+32
-22
lines changed

kaleidoscope/val/plots.py

Lines changed: 32 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,6 @@
99

1010
import dask.array as da
1111
import numpy as np
12-
from cartopy.crs import LambertConformal
13-
from cartopy.crs import PlateCarree
14-
from cartopy.crs import Projection
15-
from cartopy.feature import NaturalEarthFeature
1612
from matplotlib import colors as plc
1713
from matplotlib import pyplot as plt
1814
from matplotlib.axes import Axes
@@ -22,14 +18,10 @@
2218

2319
from ..interface.plot import Plot
2420

25-
_LAND = NaturalEarthFeature(
26-
"physical", "land", "10m", edgecolor="face", facecolor="#e8e5db"
27-
)
2821

29-
30-
class ScenePlot(Plot):
22+
class WorldPlot(Plot):
3123
"""
32-
A scene plot, usually for plotting the values for a single time step
24+
A world plot, usually for plotting the values for a single time step
3325
of a variable in a data cube or of a quantity derived thereof.
3426
"""
3527

@@ -47,22 +39,14 @@ def plot(
4739
cbar_label: str | None = None,
4840
cmap: str = "viridis",
4941
norm: plc.Normalize | None = None,
50-
projection: Projection | None = None,
42+
projection: Any = None,
5143
xlocs: tuple[Any, ...] | None = None,
5244
ylocs: tuple[Any, ...] | None = None,
5345
vmin: Any | None = None,
5446
vmax: Any | None = None,
5547
) -> Figure:
5648
if projection is None:
57-
mid_lat = round(data.lat.mean().item(), ndigits=1)
58-
mid_lon = round(data.lon.mean().item(), ndigits=1)
59-
max_lat = np.ceil(data.lat.max()).item()
60-
min_lat = np.floor(data.lat.min()).item()
61-
projection = LambertConformal(
62-
central_latitude=mid_lat,
63-
central_longitude=mid_lon,
64-
standard_parallels=(min_lat, max_lat),
65-
)
49+
projection = self.projection
6650
fig, ax = plt.subplots(
6751
subplot_kw={"projection": projection},
6852
)
@@ -74,14 +58,14 @@ def plot(
7458
x="lon",
7559
y="lat",
7660
robust=True,
77-
transform=PlateCarree(),
61+
transform=self.transform,
7862
vmin=vmin,
7963
vmax=vmax,
8064
norm=norm,
8165
cmap=cmap,
8266
cbar_kwargs=cbar_kwargs,
8367
)
84-
ax.add_feature(_LAND)
68+
ax.add_feature(self.land)
8569
ax.autoscale_view()
8670
ax.gridlines(
8771
alpha=0.1,
@@ -100,6 +84,32 @@ def plot(
10084
plt.close()
10185
return fig
10286

87+
@property
88+
def land(self):
89+
"""Returns the cartographic land feature."""
90+
from cartopy.feature import NaturalEarthFeature
91+
92+
return NaturalEarthFeature(
93+
"physical", "land", "10m", edgecolor="face", facecolor="#e8e5db"
94+
)
95+
96+
@property
97+
def projection(self):
98+
"""Returns the default projection."""
99+
from cartopy.crs import InterruptedGoodeHomolosine
100+
101+
return InterruptedGoodeHomolosine(
102+
central_longitude=-160.0,
103+
emphasis="ocean",
104+
)
105+
106+
@property
107+
def transform(self):
108+
"""Returns the default transform."""
109+
from cartopy.crs import PlateCarree
110+
111+
return PlateCarree()
112+
103113

104114
class HistogramPlot(Plot):
105115
"""

0 commit comments

Comments
 (0)