Skip to content

Commit aa5f136

Browse files
committed
Updated to set_up_map: if provided ax for plotting is not a cartopy axes, it is removed and replaced with a cartopy axes with same subplot position. Note that handles must be updated. Figure title can be provided to set_up_map and plotting methods.
1 parent 564c88a commit aa5f136

1 file changed

Lines changed: 26 additions & 16 deletions

File tree

trajan/plot/__init__.py

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -67,21 +67,31 @@ def set_up_map(self, kwargs_d=None, **kwargs):
6767

6868
ax = kwargs_d.pop('ax', None)
6969
crs = kwargs_d.pop('crs', None)
70+
assert crs is None or ax is None, "Only one of `ax` and `crs` may be specified."
71+
crs = crs if crs is not None else ccrs.Mercator()
72+
self.gcrs = ccrs.PlateCarree(globe=crs.globe)
73+
74+
if ax is not None:
75+
if isinstance(ax, cartopy.mpl.geoaxes.GeoAxes):
76+
if ax.has_data() is True:
77+
logger.debug('Plotting to existing Cartopy axes')
78+
return ax
79+
else:
80+
logger.warning('Provided axes are not cartopy, replacing. Make sure to update ax handle provided as input')
81+
subplotspec = ax.get_subplotspec()
82+
fig = ax.figure
83+
ax.remove()
84+
ax = fig.add_subplot(subplotspec, projection=crs)
85+
7086
margin = kwargs_d.pop('margin', .1)
7187
corners = kwargs_d.pop('corners', None)
7288
land = kwargs_d.pop('land', 'auto')
7389
figsize = kwargs_d.pop('figsize', 11)
74-
75-
assert crs is None or ax is None, "Only one of `ax` and `crs` may be specified."
76-
77-
if ax is not None:
78-
logger.debug('axes already set up')
79-
return ax
90+
title = kwargs_d.pop('title', None)
8091

8192
# It is not possible to change the projection of existing axes. The type of axes object returned
8293
# by `plt.axes` depends on the input projection.
8394

84-
# Create a new figure if none exists.
8595
if corners is None:
8696
lonmin = self.ds.traj.tlon.min() - margin
8797
lonmax = self.ds.traj.tlon.max() + margin
@@ -93,7 +103,8 @@ def set_up_map(self, kwargs_d=None, **kwargs):
93103
latmin = corners[2]
94104
latmax = corners[3]
95105

96-
if len(plt.get_fignums()) == 0:
106+
# Create a new figure if none exists.
107+
if ax is None and len(plt.get_fignums()) == 0:
97108
logger.debug('Creating new figure and axes..')
98109
meanlat = (latmin + latmax) / 2
99110
aspect_ratio = float(latmax - latmin) / (float(lonmax - lonmin))
@@ -103,19 +114,15 @@ def set_up_map(self, kwargs_d=None, **kwargs):
103114
fig = plt.figure(figsize=(figsize / aspect_ratio, figsize))
104115
else:
105116
fig = plt.figure(figsize=(figsize, figsize * aspect_ratio))
106-
107-
else:
117+
elif ax is None:
108118
fig = plt.gcf()
109119
if len(fig.axes) > 0:
110-
logger.debug('Axes already exist on existing figure.')
120+
logger.debug('Axes already exist on existing figure')
111121
return fig.gca()
112-
else:
113-
logger.debug('Figure exists, setting up axes.')
114122

115-
crs = crs if crs is not None else ccrs.Mercator()
116-
self.gcrs = ccrs.PlateCarree(globe=crs.globe)
123+
if ax is None:
124+
ax = fig.add_subplot(111, projection=crs)
117125

118-
ax = fig.add_subplot(111, projection=crs)
119126
ax.set_extent([lonmin, lonmax, latmin, latmax], crs=self.gcrs)
120127
ax.gridlines(self.gcrs, draw_labels=['left', 'bottom'])
121128

@@ -129,6 +136,9 @@ def set_up_map(self, kwargs_d=None, **kwargs):
129136
lscale=land,
130137
globe=crs.globe)
131138

139+
if title is not None:
140+
ax.set_title(title)
141+
132142
return ax
133143

134144
def __call__(self, *args, **kwargs):

0 commit comments

Comments
 (0)