show#

Plotting helper functions.

hsl_to_rgb(H, S, L)#

Convert color from HSL to RGB.

vector_to_rgb(x, y, z)#

Map vector (with norm ≤ 1) to RGB.

Note

The saturation does not depend on the z-component, so the color sphere is continuous.

get_rgb(field_quantity, OoP_axis_idx=2, layer=None, geometry=None)#

Get rgb values of given field quantity along a given layer.

Notes

There is also a CUDA version of this function: mumaxplus.FieldQuantity.get_rgb(), but the RGB components are on the first axis, like with FieldQuantity evaluations. Here, RGB values are on the last axis to work nicely with plotting libraries, such as imshow in matplotlib.

The final color scheme is different from mumax³. In this case, the saturation does not depend on the z-component anymore, meaning the z=0 plane remains unchanged, but other colors will appear slightly less saturated. This ensures that the color sphere is continuous everywhere, particularly when crossing the xz- or yz-plane with a normalized length less than 1, where the colors will fade through gray.

Parameters:
  • field_quantity (mumaxplus.FieldQuantity or numpy.ndarray) – The vector field_quantity of which to get the rgb representation. It can have any number of axes, but the first axis must contain the 3 vector components. It needs to have the shape (3, nz, ny, nx) in order to index along a given layer.

  • OoP_axis_idx (int, optional, default=2) – The index of the axis pointing out of plane: 0, 1 and 2 correspond to x, y and z respectively. The other two are aranged according to a right-handed coordinate system. If None, both OoP_axis_idx and layer are ignored and rgb values are calculated for all cells. This cannot be interpreted if field_quantity does not have 4 dimensions.

  • layer (int, optional) – Layer index of the out-of-plane axis. Calculates rgb for all layers if None (default). This cannot be interpreted if field_quantity does not have 4 dimensions.

  • geometry (numpy.ndarray of booleans, optional) – Cells where geometry is False are not used in normalization and are set to gray. The shape of the geometry should correspond to the shape of the field_quantity, except for the first component axis. The given geometry is ignored if field_quantity is a FieldQuantity.

Returns:

rgb – It has shape (n_vertical, n_horizontal, 3) if OoP_axis_idx and layer are given, otherwize (*field_quantity.shape[1:], 3).

Return type:

ndarray

get_rgba(field_quantity, OoP_axis_idx=2, layer=None, geometry=None)#

Get rgba values of given field_quantity.

See docstring of get_rgb() for an explanation of the parameters.

Additionally, the geometry is used to set alpha value to 0 where geometry is False.

See also

get_rgb

Parameters:
  • field_quantity (FieldQuantity | ndarray)

  • OoP_axis_idx (Literal[0, 1, 2] | None)

  • layer (int | None)

  • geometry (ndarray | None)

Return type:

ndarray

slice_field_right_handed(field, OoP_axis_idx=2, layer=0)#

Return a right-handed two dimensional slice of the given field with shape (ncomp, n_vertical, n_horizontal) or (n_vertical, n_horizontal) by taking the layer index of the out-of-plane axis given by OoP_axis_idx.

Parameters:
  • field (numpy.ndarray with shape ([ncomp,] nz, ny, nx)) – Field array to slice

  • OoP_axis_idx (int, default=2) – Index of the out of plane axis. 0, 1 and 2 represent x, y and z respectively.

  • layer (int, default=0) – Chosen index at which to slice the out-of-plane axis.

Returns:

field_2D – Right-handed two dimensional slice of given field.

Return type:

numpy.ndarray with shape ([ncomp,] n_vertical, n_horizontal)

downsample(field, new_shape, intrinsic=True)#

Downsample the field to a new shape. If the given and returned fields are imagined to be overlayed on top of one another with their edges aligned, then the cells of the returned array contain the average (or sum, if not intrinsic) of all cells they (partially) cover, weighted by the fraction of the covered area/volume.

Parameters:
  • field (numpy.ndarray) – The field to downsample.

  • new_shape (tuple of integers) – The new shape, which must have the same number of dimensions as the given field, with smaller or equal length per dimension.

  • intrinsic (bool, True) – Whether to take the average or the total sum, so whether to divide the resulting sum by the number of old cells contained within the new cells or not.

Returns:

new_field – The downsampled field with a smaller size.

Return type:

numpy.ndarray

SIprefix_to_mul(unit)#
Return type:

float

appropriate_SIprefix(n, unit_prefix='', only_thousands=True)#

Converts n with old SI prefix unit_prefix to a reasonable number with a new SI prefix.

Parameters:
  • n (float or numpy.ndarray) – Number or array of numbers to convert.

  • unit_prefix (str, default='') – Previous SI prefix of the unit of n.

  • only_thousands (bool, default=True) – If True (default), then centi, deci, deca and hecto are not used.

Returns:

The new scalar value(s) and the new appropriate SI prefix.

Return type:

tuple[float, str]

Examples

>>> appropriate_SIprefix(0.0000238, 'm')
(23.8, 'n')

Converting 0.0000238 ms returns 23.8 ns.

class UnitScalarFormatter#

Bases: ScalarFormatter

An extension of the ScalarFormatter to take units into account. matplotlib/matplotlib

__init__(SIprefix, unit, useOffset=None, useMathText=None, useLocale=None, usetex=None)#
Parameters:
  • SIprefix (str)

  • unit (str)

set_locs(locs)#

Use rescaled locs according to preferred unit multiplier for all math, then save original locs for proper drawing in data coordinates.

format_data(value)#

Return the full string representation of the value with the position unspecified.

format_data_short(value)#

This is exactly the same implementation as with ScalarFormatter. Value is only divided by self.SImultiplier at the end and a unit is added.

plot_field(field_quantity, out_of_plane_axis='z', layer=0, component=None, geometry=None, field=None, file_name=None, show=None, ax=None, figsize=None, title=None, xlabel=None, ylabel=None, imshow_symmetric_clim=False, imshow_kwargs={}, enable_colorbar=True, colorbar_kwargs={}, enable_quiver=None, arrow_size=8.0, quiver_cmap=None, quiver_symmetric_clim=True, quiver_kwargs={})#

Plot a mumaxplus.FieldQuantity() or numpy.ndarray with 1 component as a scalar field, with 3 components as a vector field or plot one selected component as a a scalar field. Vector fields are plotted using an HSL colorscheme, with optionally added arrows. This differs slightly from mumax³, see get_rgb().

Parameters:
  • field_quantity (mumaxplus.FieldQuantity or numpy.ndarray) – The field_quantity needs to have 4 dimensions with the shape (ncomp, nx, ny, nz). Additional dressing of the Axes is done if given a mumaxplus.FieldQuantity.

  • out_of_plane_axis (string, default="z") –

    The axis pointing out of plane: “x”, “y” or “z”. The other two are plotted according to a right-handed coordinate system.

    • ”x”: z over y

    • ”y”: x over z

    • ”z”: y over x

  • layer (int, default=0) – Chosen index at which to slice the out-of-plane axis.

  • component (int, optional) –

    The component of the field_quantity to plot as an image. If set to an integer, that component is plotted as a scalar field. If None (default), a field_quantity with

    • 1 component is plotted as a scalar field

    • 3 components is plotted as a vector field with an HSL colorscheme.

    • a different number of components can’t be plotted.

  • geometry (numpy.ndarray, optional) – The geometry of the field_quantity with shape (nz, ny, nx) to mask scalar field plots where geometry is False.

  • field (numpy.ndarray, optional) – If given and if field_quantity is a mumaxplus.FieldQuantity, this field will be plotted instead of evaluating field_quantity (again). field_quantity will still be used for dressing the plot (extent, name, unit, …). If field_quantity is a numpy.ndarray then field is ignored.

  • file_name (string, optional) – If given, the resulting figure will be saved with the given file name.

  • show (bool, optional) – Whether to call matplotlib.pyplot.show at the end. If None (default), matplotlib.pyplot.show will be called only if no ax and no file_name have been provided.

  • ax (matplotlib.axes.Axes, optional) – The Axes instance to plot onto. If None (default), new Figure and Axes instances will be created.

  • figsize (tuple[float] of size 2, optional) – The size of the figure, if a new figure has to be created.

  • title (str, optional) – The title of the Axes. None will generate a default title, while an empty string won’t set any title.

  • xlabel (str, optional) – The label of the x/y-axis. None will generate a default label, while an empty string won’t set any label.

  • ylabel (str, optional) – The label of the x/y-axis. None will generate a default label, while an empty string won’t set any label.

  • imshow_symmetric_clim (bool, default=False) – Whether to map zero to the central color if a scalar field is plotted in the image. This is ignored if vmin or vmax is given in imshow_kwargs. This is best used with diverging colormaps, like “bwr”.

  • imshow_kwargs (dict, default={}) – Keyword arguments to pass to matplotlib.axes.Axes.imshow, e.g. “cmap”.

  • enable_colorbar (bool, default=True) – Whether to automatically add a colorbar to the figure of the Axes when relevant.

  • colorbar_kwargs (dict, default={}) – Keyword arguments to pass to matplotlib.figure.Figure.colorbar. Relevant formatting and labeling with units is done automatically, unless the keyword arguments “label” or “format” are specified.

  • enable_quiver (bool, optional) – Whether to plot arrows on top of the colored image. If None (default), arrows are only added if no specific component for the image has been given. This is only relevant for field quantities with 3 components.

  • arrow_size (float, default=8) – Length of an arrow as a number of cells, so one arrow is designated to an area of arrow_size by arrow_size. This is only relevant for field quantities with 3 components.

  • quiver_cmap (string, optional) – A colormap to use for the quiver. By default, no colormap is used, so the arrows are a solid color. If set to “HSL”, the 3D vector data is used for an HSL colorscheme, where the x- and y-components control the hue and saturation and the z-component controls lightness, regardless of the out_of_plane_axis. Any matplotlib colormap can also be given to color the arrows according to the out-of-plane component. This is only relevant for field quantities with 3 components.

  • quiver_symmetric_clim (bool, default=True) – Whether to map zero to the central color if the arrows are colored according to the out-of-plane component of the vector field. This is ignored if clim is given in quiver_kwargs. This is best used with diverging colormaps, like “bwr”. This is only relevant for field quantities with 3 components.

  • quiver_kwargs (dict, default={}) – Keyword arguments to pass to matplotlib.axes.Axes.quiver. This is only relevant for field quantities with 3 components.

Returns:

ax – The resulting Axes on which is plotted.

Return type:

matplotlib.axes.Axes

See also

get_rgb, get_rgba

inspect_field(field_quantity, out_of_plane_axis='z', layer=0, geometry=None, field=None, file_name=None, show=None, figsize=None, nrows=None, ncols=None, suptitle=None, xlabel=None, ylabel=None, symmetric_clim=False, imshow_kwargs={}, enable_colorbar=True, shared_colorbar=False, colorbar_kwargs={})#

Plot every component of a mumaxplus.FieldQuantity() or numpy.ndarray as a scalar field subplot on one figure.

Parameters:
  • field_quantity (mumaxplus.FieldQuantity or numpy.ndarray) – The field_quantity needs to have 4 dimensions with the shape (ncomp, nx, ny, nz). Additional dressing of the Axes is done if given a mumaxplus.FieldQuantity.

  • out_of_plane_axis (string, default="z") –

    The axis pointing out of plane: “x”, “y” or “z”. The other two are plotted according to a right-handed coordinate system.

    • ”x”: z over y

    • ”y”: x over z

    • ”z”: y over x

  • layer (int, default=0) – Chosen index at which to slice the out-of-plane axis.

  • geometry (numpy.ndarray, optional) – The geometry of the field_quantity with shape (nz, ny, nx) to mask plots where geometry is False.

  • field (numpy.ndarray, optional) – If given and if field_quantity is a mumaxplus.FieldQuantity, this field will be plotted instead of evaluating field_quantity (again). field_quantity will still be used for dressing the plot (extent, name, unit, …). If field_quantity is a numpy.ndarray then field is ignored.

  • file_name (string, optional) – If given, the resulting figure will be saved with the given file name.

  • show (bool, optional) – Whether to call matplotlib.pyplot.show at the end. If None (default), matplotlib.pyplot.show will be called only if no file_name has been provided.

  • figsize (tuple[float] of size 2, optional) – The size of the new figure created. Automatically tries to find suitable figsize if not given.

  • nrows (int, optional) – The number of rows/columns of the subplot grid. If only one is defined, the other will be made large enough to accommodate all components of the field_quantity. If both are None (default), a suitable arrangement will be sought.

  • ncols (int, optional) – The number of rows/columns of the subplot grid. If only one is defined, the other will be made large enough to accommodate all components of the field_quantity. If both are None (default), a suitable arrangement will be sought.

  • suptitle (str, optional) – The suptitle of the Figure. None will generate a default title, while an empty string won’t set any title.

  • xlabel (str, optional) – The label of the x/y-axes of all lowest/leftmost Axes respectively. None will generate a default label, while an empty string won’t set any label.

  • ylabel (str, optional) – The label of the x/y-axes of all lowest/leftmost Axes respectively. None will generate a default label, while an empty string won’t set any label.

  • symmetric_clim (bool, default=False) – Whether to map zero to the central color. This is ignored if vmin or vmax is given in imshow_kwargs. This is best used with diverging colormaps, like “bwr”.

  • imshow_kwargs (dict, default={}) – Keyword arguments to pass to matplotlib.axes.Axes.imshow, e.g. “cmap”.

  • enable_colorbar (bool, default=True) – Whether to add (a) colorbar(s) to the figure of the Axes.

  • shared_colorbar (bool, default=False) – Whether to share one colorbar with the color limits (vmin, vmax) between all plots.

  • colorbar_kwargs (dict, default={}) – Keyword arguments to pass to matplotlib.figure.Figure.colorbar. Relevant formatting and labeling with units is done automatically, unless the keyword arguments “label” or “format” are specified.

Returns:

  • fig (matplotlib.figure.Figure) – The created figure.

  • axs (matplotlib.axes.Axes or array of Axes) – All the resulting Axes on which is plotted.

show_regions(magnet, out_of_plane_axis='z', layer=0, component=None, geometry=None, field=None, file_name=None, show=None, ax=None, figsize=None, title=None, xlabel=None, ylabel=None, imshow_symmetric_clim=False, imshow_kwargs={})#

Plot the boundaries between regions of the given magnet. For details about the function parameters, see plot_field().

Parameters:
  • out_of_plane_axis (Literal['x', 'y', 'z'])

  • layer (int)

  • component (int | None)

  • geometry (ndarray | None)

  • field (ndarray | None)

  • file_name (str | None)

  • show (bool | None)

  • ax (Axes | None)

  • figsize (tuple[float, float] | None)

  • title (str | None)

  • xlabel (str | None)

  • ylabel (str | None)

  • imshow_symmetric_clim (bool)

  • imshow_kwargs (dict)

Return type:

Axes

show_magnet_geometry(magnet)#

Show the geometry of a mumaxplus.Ferromagnet().

show_field_3D(quantity, cmap='HSL', enable_quiver=True, symmetric_clim=True)#

Plot a mumaxplus.FieldQuantity() with 3 components as a vectorfield.

Parameters:
  • quantity (mumaxplus.FieldQuantity (3 components)) – The FieldQuantity to plot as a vectorfield.

  • cmap (string, optional, default: "HSL") – A colormap to use. By default an HSL colormap is used. This differs slightly from mumax³, see get_rgb(). Any matplotlib colormap can also be given to color the vectors according to their z-component. It’s best to use diverging colormaps, like “bwr”.

  • enable_quiver (boolean, optional, default: True) – If set to True, a cone is placed at each cell indicating the direction. If False, colored voxels are used instead.

  • symmetric_clim (bool, default=True) – Whether to have symmetric color limits if the given cmap is not “HSL”.

See also

get_rgb