differt.plotting module

differt.plotting module#

Plotting utilities for DiffeRT objects.

Tip

Unlike in other modules, plotting utilities also work with NumPy arrays (np.ndarray)

Note

Backend names are case-sensitive.

Backend-specific notes#

Each backend comes with its own configuration options and outputs that the user should be aware of.

VisPy#

VisPy uses SceneCanvas objects to display contents, on which a view is attached. The view (ViewBox) is what contains the data to be plotted.

To reuse an existing canvas object, just pass it as a keyword argument to any of the draw_* functions in this module, i.e., with draw_*(..., canvas=canvas). In turn, each of those functions returns a figure on which you can later add data.

It is also possible to pass an existing view on which data will be plotted: draw_*(..., view=view).

If the jupyter_rfb module is installed, VisPy’s canvas integrate nicely within Jupyter notebooks.

Matplotlib#

Matplotlib uses Figure objects to display contents, on which multiple axes can be attached. In turn, each axis can contain data to be plotted.

To reuse an existing figure object, just pass it as a keyword argument to any of the draw_* functions in this module, i.e., with draw_*(..., figure=figure). In turn, each of those functions returns a figure on which you can later add data.

It is also possible to pass an existing axis (Axes) on which data will be plotted: draw_*(..., ax=ax).

Warning

By default, Matplotlib instantiates 2D axes, but this module extensively uses 3D plot methods. If an axis is provided, it is your responsibility to ensure that it can plot 3D data when needed.

By default, Matpotlib displays static images in Jupyter notebooks. To make them interactive, install ipympl and load the corresponding extension with %matplotlib widget.

Plotly#

Plotly is a dictionary-oriented library that produces HTML-based outputs. Hence, Plotly is a very good choice for publishing nice interactive plots on webpages.

Plots are fully contained inside Figure objects, and can be nicely displayed within Jupyter notebooks without further configuration.

To reuse an existing figure object, you can do the same as with the Matplotlib backend.

Common utils

The following utilities allow you to modify the default behavior of plotting functions, either permanently or inside a given scope.

reuse([backend, pass_all_kwargs])

Create a context manager that will automatically reuse the current canvas / figure.

set_backend(backend)

Set default backend for future plotting utilities.

set_defaults([backend])

Set default backend and keyword arguments for future plotting utilities.

update_defaults([backend])

Update default backend and keyword arguments for future plotting utilities.

use([backend])

Create a context manager that updates plotting defaults and returns the current default backend.

Drawing functions

List of all drawing functions provided by this module.

Note

Some functions might not support all three backends. In such cases, it is indicated in their documentation.

draw_contour(data[, x, y, z0, levels, fill])

Plot a 2D contour on a 3D canvas, at a fixed z-coordinate.

draw_image(data[, x, y, z0])

Plot a 2D image on a 3D canvas, at a fixed z-coordinate.

draw_markers(markers[, labels, text_kwargs])

Plot markers and, optionally, their label.

draw_mesh(vertices, triangles, **kwargs)

Plot a 3D mesh made of triangles.

draw_paths(paths, **kwargs)

Plot a batch of paths of the same length.

draw_rays(ray_origins, ray_directions, *[, ...])

Plot a batch of rays.

draw_surface([x, y, colors])

Plot a 3D surface.

Extending this module

If you want to add new drawing functions, either locally or by contributing to this package, the following utilities might become handy.

dispatch(fun)

Transform a function into a backend dispatcher for plot functions.

get_backend([backend])

Return the name of the backend to use.

process_kwargs(kwargs[, backend])

Process keyword arguments passed to some plotting utility.

process_matplotlib_kwargs(kwargs)

Process keyword arguments passed to some Matplotlib plotting utility.

process_plotly_kwargs(kwargs)

Process keyword arguments passed to some Plotly plotting utility.

process_vispy_kwargs(kwargs)

Process keyword arguments passed to some VisPy plotting utility.

view_from_canvas(canvas)

Return the view from the specified canvas.

Note that all higher-level plotting functions, i.e., created outside of this module, should have the following return type.

PlotOutput[source]#

alias of Any