emsarray.plot#

add_coast(axes, **kwargs)#

Add coastlines to an Axes. Some default styles are applied: the land polygons are light grey and semi-transparent, and the coastlines are opaque dark grey.

Parameters:
  • axes (matplotlib.axes.Axes) – The axes to add the coastline to

  • **kwargs – Passed to Axes.add_feature().

add_gridlines(axes, **kwargs)#

Add some gridlines to the axes.

Parameters:

axes (matplotlib.axes.Axes) – The axes to add the gridlines to.

Returns:

cartopy.mpl.gridliner.Gridliner

add_landmarks(axes, landmarks, color='black', outline_color='white', outline_width=2)#

Place some named landmarks on a plot.

Parameters:
  • axes (matplotlib.axes.Axes) – The axes to add the landmarks to.

  • landmarks (list of landmarks) – The landmarks to add. These are tuples of (name, point).

  • color (str, default 'black') – The color for the landmark marker and labels.

  • outline_color (str, default 'white') – The color for the outline. Both the marker and the labels are outlined.

  • outline_width (ind, default 2) – The linewidth of the outline.

Examples

Draw a plot of a specific area with some landmarks:

import emsarray.plot
import shapely
from matplotlib import pyplot

dataset = emsarray.tutorial.open_dataset('gbr4')

# Set up the figure
figure = pyplot.figure()
axes = figure.add_subplot(projection=dataset.ems.data_crs)
axes.set_title("Sea surface temperature around Mackay")
axes.set_aspect('equal', adjustable='datalim')
emsarray.plot.add_coast(axes, zorder=1)

# Focus on the area of interest
axes.set_extent((148.245710, 151.544167, -19.870197, -21.986412))

# Plot the temperature
temperature = dataset.ems.make_poly_collection(
    dataset['temp'].isel(time=0, k=-1),
    cmap='jet', edgecolor='face', zorder=0)
axes.add_collection(temperature)
figure.colorbar(temperature, label='°C')

# Name key locations
emsarray.plot.add_landmarks(axes, [
    ('The Percy Group', shapely.Point(150.270579, -21.658269)),
    ('Whitsundays', shapely.Point(148.955319, -20.169076)),
    ('Mackay', shapely.Point(149.192671, -21.146719)),
])

figure.show()
bounds_to_extent(bounds)#

Convert a Shapely bounds tuple to a matplotlib extents.

A Shapely bounds is a tuple of (min x, min y, max x, max y), while a Matplotlib extent is a list of (min x, max x, min y, max y).

Example

import cartopy.crs as ccrs
import matplotlib.pyplot as plt
from emsarray.plot import bounds_to_extent
from shapely.geometry import Polygon

polygon = Polygon([
    (148.30, -40.75), (146.47, -41.19), (144.67, -40.62),
    (146.05, -43.53), (146.87, -43.60), (148.30, -40.75),
])
figure = plt.figure(figsize=(10, 8), dpi=100)
axes = plt.subplot(projection=ccrs.PlateCarree())
axes.set_extent(bounds_to_extent(polygon.buffer(0.1).bounds))
polygons_to_collection(polygons, **kwargs)#

Convert a list of shapely.Polygon instances to a matplotlib PolyCollection.

Parameters:
  • polygons (iterable of shapely.Polygon) – The polygons for the poly collection

  • **kwargs (Any) – Keyword arguments to pass to the PolyCollection constructor.

Returns:

matplotlib.collections.PolyCollection – A PolyCollection made up of the polygons passed in.

make_plot_title(dataset, data_array)#

Make a suitable plot title for a variable. This will attempt to find a name for the variable by looking through the attributes. If the variable has a time coordinate, and the time coordinate has a single value, the time step is appended after the title.

plot_on_figure(figure, convention, *, scalar=None, vector=None, title=None, projection=None, landmarks=None, gridlines=True, coast=True)#

Plot a DataArray on a matplotlib Figure.

Parameters:
animate_on_figure(figure, convention, *, coordinate, scalar=None, vector=None, title=None, projection=None, landmarks=None, gridlines=True, coast=True, interval=1000, repeat=True)#

Plot a xarray.DataArray on a matplotlib Figure as a FuncAnimation.

Parameters:
  • figure (matplotlib.figure.Figure) – The Figure instace to plot on.

  • convention – The Convention instance of the dataset. This is used to build the polygons and vector quivers.

  • coordinate (xarray.DataArray) – The coordinate values to vary across frames in the animation.

  • scalar (xarray.DataArray, optional) – The data to plot as an xarray.DataArray. This will be passed to Convention.make_poly_collection(). It should have horizontal dimensions appropriate for this convention, and a dimension matching the coordinate parameter.

  • vector (tuple of numpy.ndarray, optional) – The u and v components of a vector field as a tuple of xarray.DataArray. These will be passed to Convention.make_quiver(). These should have horizontal dimensions appropriate for this convention, and a dimension matching the coordinate parameter.

  • title (str or callable, optional) – The title for each frame of animation. Optional, will default to the coordinate value for each frame.

    If this is a string, str.format() will be called with the coordinate value for each frame.

    If this is a callable, it will be called with the coordinate value for each frame, and it should return a string for the title.

  • projection (Projection) – The projection to use when plotting. Optional, defaults to PlateCarree. This is different to the coordinate reference system for the data, which is defined in Convention.data_crs.

  • landmarks (list of landmarks, optional) – Landmarks to add to the plot. These are tuples of (name, point).

  • gridlines (bool, default True) – Whether to add gridlines to the plot using add_gridlines().

  • coast (bool, default True) – Whether to add coastlines to the plot using add_coast().

  • interval (int) – The interval between frames of animation

  • repeat ({True, False, 'cycle', 'bounce'}) – Whether to repeat the animation or not. True and 'cycle' will play the animation on loop forever. 'bounce' will play the animation forward, then backward, then repeat. False will play the animation once then stop.

Returns:

matplotlib.animation.Animation