pylawr.plot#

pylawr.plot.backend.BackendLoader([name])

This load is used to load dynamically a backend for the plotter.

pylawr.plot.NotPlottedError

pylawr.plot.Plotter([backend_name, ...])

Plotter is used as an easy access for the plotting interface.

pylawr.plot.Subplot([layers_list, projection])

A Subplot is a part of a plotting figure.

pylawr.plot.backend.BackendLoader#

class pylawr.plot.backend.BackendLoader(name='agg')[source]#

Bases: object

This load is used to load dynamically a backend for the plotter. The figure will be plotted on the canvas of the loaded backend.

Parameters

name (str) – The corresponding backend to this name will be loaded.

_load_backend()[source]#
property canvas#

The canvas of the loaded backend.

Returns

canvas – This canvas can be used as figure holder for object oriented matplotlib programming.

Return type

child of matplotlib.backend_bases.FigureCanvasBase

property name#

The name of the backend in it corrected form.

Returns

name – Backend’s name.

Return type

str

pylawr.plot.NotPlottedError#

exception pylawr.plot.NotPlottedError[source]#

Bases: Exception

pylawr.plot.Plotter#

class pylawr.plot.Plotter(backend_name='agg', grid_size=(14, 14), grid_slices=None, **fig_settings)[source]#

Bases: object

Plotter is used as an easy access for the plotting interface. The plotter creates a figure and canvas during plotting- These two are holding the subplots and the layers, where the plotting logic is hided. The figure settings can be set in a dict-like manner or via manipulation of fig_settings dict. After the plotter is plotted, all attributes from matplotlib.figure.Figure can be accessed as if the plotter is a figure.

Parameters
  • backend_name (str, optional) – This backend will be loaded and used for plotting. The backend is loaded dynamically and will create a canvas which is used to plot the figure. Default is agg.

  • grid_size (tuple, optional) – Figure’s grid size. This grid size is used to create a gridspec for given figure.

  • grid_slices (dict(str, tuple(slice, slice)) or None, optional) – For every grid slice a subplot is created. During creation, the key is used as name and the slices (row slice, column slice) indicates, where the subplot is located at the plot. For every key a new subplot is created.

  • **fig_settings – Variable keyword arguments dict, which is passed during plotting figure creation to Figure.

_add_grid_slices_to_subplot(grid_slices)[source]#
_get_subplot_name_from_layer(layer)[source]#

Get the corresponding subplot name from given layer.

Parameters

layer (pylawr.plot.layer.base.BaseLayer) – The subplot name to this layer is searched.

Returns

subplot_name – The found subplot name to given layer.

Return type

str

Raises

KeyError – If no corresponding subplot was found to given layer.

_plot_subplot(subplot_name)[source]#
add_layer(subplot_name, layer)[source]#

Adds a layer to given subplot name. This layer will be added to the layer list of the subplot. The subplot has to exist beforehand.

Parameters
  • subplot_name (str) – The layer is added to the layer list of the subplot corresponding to this subplot name. If the subplot does not exist yet, a KeyError will be raised.

  • layer (child of pylawr.plot.layer.base.BaseLayer) – This layer is added to given subplot name.

add_subplot(name, grid_slice=(slice(None, None, None), slice(None, None, None)), **ax_settings)[source]#

Adds a subplot to the plotter. This subplot is added to the subplots dict. Also, given slice is added to grid slice dict with given name as key.

Parameters
  • name (str) – The name of the new subplot. If there is already a subplot with this name within the subplot dict, a KeyError will be raised.

  • grid_slice (tuple(slice, slice)) – This grid slice specifies where the subplot is located on the figure. It has to be a tuple with two slices (row position, column position). This slice is added to the grid_slices dict.

  • **ax_settings – Variable keyword arguments dict, which is passed to the subplot.

property backend_name#

Get the name of the used backend.

Returns

backend_name – This backend is used for plotting purpose.

Return type

str

change_grid_slice(name, new_grid_slice)[source]#

Change the grid slice of a given subplot. The grid slice within grid slices dict is changed for given subplot name as key. If the plotter was already plotted, the subplot is replotted.

Parameters
  • name (str) – For this subplot the grid slice is changed. If this subplot name cannot be found within the grid slices dict a KeyError is raised.

  • new_grid_slice (tuple(slice, slice)) – This grid slice is set as new grid slice of given subplot. It has to be a tuple with two slices (row position, column position).

del_subplot(name)[source]#

Remove given subplot out of the plotter. The subplot will be removed from subplots and grid_slices dictionary.

Parameters

name (str) – Subplot which should be removed.

property figure#

Get the currently opened figure.

Returns

figure – The currently opened figure.

Return type

matplotlib.figure.Figure

property grid_size#

Return the size of figure’s grid.

Returns

grid_size – The grid size as tuple (n_rows, n_cols)

Return type

tuple(int)

property grid_slices#
property gridspec#

Get an initialized Gridspec with set grid size. gridspec_settings attribute is passed as kwargs to Gridspec.

Returns

gridspec – Initialized gridspec with grid_size as geometry.

Return type

matplotlib.gridspec.Gridspec

new_figure(**fig_settings)[source]#

Creates a new figure and canvas for this figure based on currently selected backend.

Parameters

**fig_settings – Variable keyword arguments dict, which is passed during figure creation to Figure.

plot()[source]#

Plot a new figure and all subplots and layers on this figure. A new figure and canvas are created. The figure is passed to all subplots such that all subplots can use this figure to plot their layers on this figure. All set figure settings are passed to the new figure.

Warning

This method creates a new figure such that the old figure and canvas are deleted.

property plotted#

Check if plot was already called.

Returns

plotted – If plot was called at least once.

Return type

bool

show()[source]#

This method is used to show this plotter as window. Our plotter is based on the object-oriented interface of matplotlib without pyplot. We therefore need to save this plotter as png image to a temporary file. This is then showed with pyplot.

Warning

This method can be a little bit slow because this plotter is saved and replotted. Further, pyplot is imported, which can be a problem for non-interactive shells and as a consequence, this method is not tested with unittests.

property subplots#
swap_layer(new_layer, old_layer=None, layer_num=None, zorder=None)[source]#

An already set layer will be swapped with a given new_layer. The layer to replace has to be specified with one of three different arguments (in descending priority):

  • old_layer : replace given old layer with given new layer

  • layer_numreplace given old layer to the corresponding layer

    number with given new layer

  • zorderreplace given old layer to the corresponding z-order with

    given new layer

Parameters
  • new_layer (child of pylawr.plot.layer.base.BaseLayer) – This layer will replace the specified old layer. If the plotter was already plotted this layer will be also plotted.

  • old_layer (child of pylawr.plot.layer.base.BaseLayer or) – None If this old layer is given, it will be searched within the subplots and replaced by the new layer. If it is not found within the subplots, a ValueError will be raised. If it is None, either layer_num or zorder has to be specified. Default is None.

  • layer_num (tuple(str, int) or None) – The i-th layer within specified subplot is replaced. The first entry of the tuple identifies the subplot, while the integer specifies the layer number within given subplot. A KeyError will be raised, if the layer number cannot be found within specified subplot. If this is None, either old_layer or zorder has to be specified. Default is None.

  • zorder (tuple(str, int) or None) – The layer with given z-order within specified subplot is replaced. The first entry of the tuple identifies the subplot, while the integer specified the z-order within given subplot. A KeyError will be raised, if the z-order cannot be found within specified subplot. If this is None, either old_layer or layer_num has to be specified. The z-order has to be Default is None.

update(*args, **kwargs)[source]#

pylawr.plot.Subplot#

class pylawr.plot.Subplot(layers_list=None, projection=None, **ax_settings)[source]#

Bases: object

A Subplot is a part of a plotting figure. Basically, it is a wrapper around matplotlib.axes.Axes. A subplot has multiple layers, which are ordered in a list. During plotting a subplot creates a Axes, on which the layers are plotted. The actual plotting logic is hided in the layers. The axes settings can be set in a dict-like manner or via manipulation of ax_settings dict.

Parameters
  • layers_list (list(child of pylawr.plot.layer.base.BaseLayer) or) – None, optional This list of layers are the basic layers of this subplot. Additional layers can be added with add_layer() and layers can be deleted with del_layer(). If the given list of layers is None, an empty list will be initialized. If the list of layers cannot be casted into a list type a TypeError is raised. Default is None.

  • projection (child of cartopy.crs.Projection or None) – The projection of this subplot. This cartopy projection is used to project given geo-referenced data into another coordinate system. If the projection is None, the data is not reprojected. Default is None.

  • **ax_settings – Variable keyword arguments dict, which is passed during axes creation to Axes.

ax#

GeoAxes or None The axes of this subplot. If this None, the subplot was not plotted yet.

Type

Axes,

plotted#

If the subplot was already plotted once.

Type

boolean

layers#

The layers of this subplot. Plotting logic of these layers is called if plot() is called.

Type

list(child of pylawr.plot.layer.base.BaseLayer)

projection#

The projection of this subplot. The projection is used during initialisation of Axes. If a projection is set, the axes will be a GeoAxes with given projection.

Type

child of cartopy.crs.Projection or None

ax_settings#

Variable keyword arguments dict and is passed during axes creation to __init__ of Axes.

Type

dict

extent_settings#

Settings of the axes extent. This is used if the axes is a GeoAxes, because cartopy disturbs the aspect ratio of the axes. The extent settings has auto, lon_min, lon_max, lat_min, lat_max and projection as keys.

Type

dict

extent#

The extent of this axes in degrees, which sets the geographic border of this subplot. If auto_extent is set to True, this extent is modified for plotting purpose, but the original extent is returned. Extent is a dictionary with lon_min, lon_max, lat_min and lat_max as entries.

Type

dict(str, float)

auto_extent#

Indicates if the axes extent should be extended automatically. If an extent is set, auto_extent will modify this extent so that the subplot has an aspect ratio as defined within grid slices. Default is True.

Type

bool

_calc_auto_extent(spec)[source]#

This method automatically calculate the extent based on current subplot position and size and current extent.

Returns

extent_tuple – The automatically calculated extent as tuple (lon_min, lon_max, lat_min and lat_max).

Return type

tuple(float)

_get_subplot_aspect(spec)[source]#
property _layers#
add_layer(layer)[source]#

Add a given layer to the layers of this subplot.

Parameters

layer (child of pylawr.plot.layer.base.BaseLayer) – This layer will be added to the layers list of this subplot.

property auto_extent#

Get boolean if the geographic extent of this subplot is automatically extended.

Returns

auto_extent – Indicates if the axes extent should be extended automatically. If an extent is set, auto_extent will modify this extent so that the subplot has an aspect ratio as defined within grid slices. Default is True.

Return type

bool

property ax#

Get the axes for this subplot. This ax is created if this subplot was plotted once.

Returns

  • ax (Axes,)

  • GeoAxes or None – The axes of this subplot. If this None, the subplot was not plotted yet.

property extent#

Get the geographic latitude and longitude extent for this subplot.

Returns

extent – The extent of this axes in degrees, which sets the geographic border of this subplot. If auto_extent is set to True, this extent is modified for plotting purpose, but the original extent is returned. Extent is a dictionary with lon_min, lon_max, lat_min and lat_max as entries.

Return type

dict(str, float)

property extent_settings#

Get the current settings for the geographic extent of this subplot.

Returns

extent_settings – Settings of the axes extent. This is used if the axes is a GeoAxes, because cartopy disturbs the aspect ratio of axes. The extent settings has auto, lon_min, lon_max, lat_min, lat_max and projection as keys.

Return type

dict

property layers#

Layers of this subplot. These layer will be plotted on this subplot.

Returns

layers – The layers of this subplot. Plotting logic of these layers is called if plot() is called.

Return type

list(child of pylawr.plot.layer.base.BaseLayer)

new_axes(fig, spec=None, **ax_settings)[source]#

Create a new axes for this subplot.

Parameters
  • fig (matplotlib.figure.Figure) – The axes will be created on this figure. The figure needs a valid canvas to be drawn.

  • spec (any, optional) – This argument determines the location of the subplot on given figure. Default is None.

  • **ax_settings – Variable keyword arguments dict, which is passed during axes creation to Axes.

plot(fig, spec=None)[source]#

Plot this subplot. A new axes is created on given figure with given subplot spec. All layers of this subplot are plot on this axes. ax_settings are passed to add_subplot() as additional arguments. The extent isa djusted according to extent_settings of this subplot.

Parameters
  • fig (matplotlib.figure.Figure) – The axes is created on this figure.

  • spec (any, optional) – This argument determines the location of the subplot on given figure. Default is None.

plot_layer_on_ax(layer)[source]#

Plot given layer on this subplot. If the subplot was not plotted yet a ValueError will be raised. This method can be used to switch a layer of this subplot.

Parameters

layer (child of pylawr.plot.layer.base.BaseLayer) – This layer will be plotted on this subplot. This layer does not need to be a layer within this subplot. If the layer is not a valid pylawr plotting layer a TypeError will be raised.

Warning

To call this method plot has to be called beforehand.

property plotted#

Checks if plot method was already called once.

Returns

plotted – If the subplot was already plotted once.

Return type

boolean

property projection#

Get the projection of this subplot. This cartopy projection is used to project given geo-referenced data into another coordinate system. The projection is used during intialization of Axes. If the projection is None, the data is not reprojected.

Returns

projection – The projection of this subplot. The projection is used during initialisation of Axes. If a projection is set, the axes will be a GeoAxes with given projection.

Return type

child of cartopy.crs.Projection or None

swap_layer(new_layer, old_layer)[source]#

Swap an old layer for a new layer. This removes the old layer from this subplot and inserts the new layer at its place (including the zorder, if set). If this subplot was already plotted, the new layer will be plotted automatically on this subplot.

Parameters
update(*args, **kwargs)[source]#
update_extent(spec=None)[source]#

Update the extent of this subplot, if this has a valid GeoAxes. The extent is updated based on pylawr.plot.subplot.Subplot.extent_settings.

Parameters

spec (SubplotSpec or None) – This spec is needed to adjust the extent automatically. Based on this extent, the aspect of this subplot is calculated.

Raises

TypeError – A TypeError is raised if the axes is not a valid GeoAxes or the subplot was not plotted yet.