Skip to content

Commit 0fd8308

Browse files
authored
Merge pull request #115 from csiro-coasts/plot-optional-elements
Make coasts and gridlines optional on plots
2 parents 4853266 + ccb732c commit 0fd8308

File tree

2 files changed

+45
-11
lines changed

2 files changed

+45
-11
lines changed

docs/releases/development.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,11 @@ Next release (in development)
55
* Fix transect plot title and units.
66
All attributes were being dropped accidentally in `prepare_data_array_for_transect()`.
77
(:pr:`114`).
8+
* Add `coast` and `gridlines` parameters to :func:`emsarray.plot.plot_on_figure()`,
9+
allowing users to disable these components of a plot.
10+
Currently gridlines can cause issues in interactive Jupyter notebooks
11+
and some other environments.
12+
There is no one solution to every situation.
13+
Allowing users to disable gridlines is a temporary work around
14+
while other solutions are being sought.
15+
(:pr:`115`, :issue:`SciTools/cartopy#2245`, :issue:`SciTools/cartopy#2246`, :issue:`SciTools/cartopy#2247`).

src/emsarray/plot.py

Lines changed: 37 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ def add_coast(axes: Axes, **kwargs: Any) -> None:
6262
axes.add_feature(coast, **kwargs)
6363

6464

65-
def add_gridlines(axes: Axes) -> gridliner.Gridliner:
65+
def add_gridlines(axes: Axes, **kwargs: Any) -> gridliner.Gridliner:
6666
"""
6767
Add some gridlines to the axes.
6868
@@ -75,10 +75,12 @@ def add_gridlines(axes: Axes) -> gridliner.Gridliner:
7575
-------
7676
cartopy.mpl.gridliner.Gridliner
7777
"""
78-
gridlines = axes.gridlines(draw_labels=True, auto_update=True)
79-
gridlines.top_labels = False
80-
gridlines.right_labels = False
81-
return gridlines
78+
kwargs = {
79+
'draw_labels': ['left', 'bottom'],
80+
'auto_update': True,
81+
**kwargs,
82+
}
83+
return axes.gridlines(**kwargs)
8284

8385

8486
def add_landmarks(
@@ -270,6 +272,8 @@ def plot_on_figure(
270272
title: Optional[str] = None,
271273
projection: Optional[cartopy.crs.Projection] = None,
272274
landmarks: Optional[Iterable[Landmark]] = None,
275+
gridlines: bool = True,
276+
coast: bool = True,
273277
) -> None:
274278
"""
275279
Plot a :class:`~xarray.DataArray`
@@ -298,6 +302,10 @@ def plot_on_figure(
298302
which is defined in :attr:`.Convention.data_crs`.
299303
landmarks : list of :data:`landmarks <emsarray.types.Landmark>`, optional
300304
Landmarks to add to the plot. These are tuples of (name, point).
305+
gridlines : bool, default True
306+
Whether to add gridlines to the plot using :func:`add_gridlines()`.
307+
coast : bool, default True
308+
Whether to add coastlines to the plot using :func:`add_coast()`.
301309
"""
302310
if projection is None:
303311
projection = cartopy.crs.PlateCarree()
@@ -331,10 +339,19 @@ def plot_on_figure(
331339
if landmarks:
332340
add_landmarks(axes, landmarks)
333341

334-
add_coast(axes)
335-
add_gridlines(axes)
342+
if coast:
343+
add_coast(axes)
344+
if gridlines:
345+
add_gridlines(axes)
346+
336347
axes.autoscale()
337348

349+
# Work around for gridline positioning issues
350+
# https://github.com/SciTools/cartopy/issues/2245#issuecomment-1732313921
351+
layout_engine = figure.get_layout_engine()
352+
if layout_engine is not None:
353+
layout_engine.execute(figure)
354+
338355

339356
@_requires_plot
340357
def animate_on_figure(
@@ -347,6 +364,8 @@ def animate_on_figure(
347364
title: Optional[Union[str, Callable[[Any], str]]] = None,
348365
projection: Optional[cartopy.crs.Projection] = None,
349366
landmarks: Optional[Iterable[Landmark]] = None,
367+
gridlines: bool = True,
368+
coast: bool = True,
350369
interval: int = 1000,
351370
repeat: Union[bool, Literal['cycle', 'bounce']] = True,
352371
) -> animation.FuncAnimation:
@@ -392,6 +411,10 @@ def animate_on_figure(
392411
which is defined in :attr:`.Convention.data_crs`.
393412
landmarks : list of :data:`landmarks <emsarray.types.Landmark>`, optional
394413
Landmarks to add to the plot. These are tuples of (name, point).
414+
gridlines : bool, default True
415+
Whether to add gridlines to the plot using :func:`add_gridlines()`.
416+
coast : bool, default True
417+
Whether to add coastlines to the plot using :func:`add_coast()`.
395418
interval : int
396419
The interval between frames of animation
397420
repeat : {True, False, 'cycle', 'bounce'}
@@ -442,8 +465,10 @@ def animate_on_figure(
442465
axes.add_collection(quiver)
443466

444467
# Draw a coast overlay
445-
add_coast(axes)
446-
gridlines = add_gridlines(axes)
468+
if coast:
469+
add_coast(axes)
470+
if gridlines:
471+
gridliner = add_gridlines(axes)
447472
if landmarks:
448473
add_landmarks(axes, landmarks)
449474
axes.autoscale()
@@ -470,8 +495,9 @@ def animate(index: int) -> Iterable[Artist]:
470495
coordinate_value = coordinate.values[index]
471496
axes.title.set_text(coordinate_callable(coordinate_value))
472497
changes.append(axes.title)
473-
changes.extend(gridlines.xline_artists)
474-
changes.extend(gridlines.yline_artists)
498+
if gridlines:
499+
changes.extend(gridliner.xline_artists)
500+
changes.extend(gridliner.yline_artists)
475501

476502
if collection is not None:
477503
collection.set_array(scalar_values[index])

0 commit comments

Comments
 (0)