Plotting

This module contains functions for plotting various important metrics

All the plotting functions here can accept torch input or numpy input, to facilitate their use both for live inspection of running reconstructions and for after-the-fact analysis. Utilities for plotting complex valued images exist, as well as plotting scan patterns and nanomaps

cdtools.tools.plotting.colorize(z)

Returns RGB values for a complex color plot given a complex array This function returns a set of RGB values that can be used directly in a call to imshow based on an input complex numpy array (not a torch tensor representing a complex field)

Parameters:

z (array) – A complex-valued array

Returns:

rgb – A list of arrays for the R,G, and B channels of an image

Return type:

list(array)

cdtools.tools.plotting.plot_amplitude(im, fig=None, basis=None, units='$\\mu$m', cmap='viridis', cmap_label='Amplitude (a.u.)', **kwargs)

Plots the amplitude of a complex array with dimensions NxM

If a figure is given explicitly, it will clear that existing figure and plot over it. Otherwise, it will generate a new figure.

If a basis is explicitly passed, the image will be plotted in real-space coordinates.

Parameters:
  • im (array) – An complex array with dimensions NxM

  • fig (matplotlib.figure.Figure) – Default is a new figure, a matplotlib figure to use to plot

  • basis (np.array) – Optional, the 3x2 probe basis

  • units (str) – The length units to mark on the plot, default is um

  • cmap (str) – Default is ‘viridis’, the colormap to plot with

  • cmap_label (str) – What to label the colorbar when plotting

  • **kwargs – All other args are passed to fig.add_subplot(111, **kwargs)

Returns:

used_fig – The figure object that was actually plotted to.

Return type:

matplotlib.figure.Figure

cdtools.tools.plotting.plot_phase(im, fig=None, basis=None, units='$\\mu$m', cmap='cividis', cmap_label='Phase (rad)', vmin=None, vmax=None, **kwargs)

Plots the phase of a complex array with dimensions NxM

If a figure is given explicitly, it will clear that existing figure and plot over it. Otherwise, it will generate a new figure.

If a basis is explicitly passed, the image will be plotted in real-space coordinates

If the cmap is entered as ‘phase’, it will plot the cmocean phase colormap, and by default set the limits to [-pi,pi].

Parameters:
  • im (array) – An complex array with dimensions NxM

  • fig (matplotlib.figure.Figure) – Default is a new figure, a matplotlib figure to use to plot

  • basis (np.array) – Optional, the 3x2 probe basis

  • units (str) – The length units to mark on the plot, default is um

  • cmap (str) – Default is ‘cividis’, the colormap to plot with.

  • cmap_label (str) – What to label the colorbar when plotting

  • vmin (int) – Default is min(angle(im)), the minimum value for the colormap

  • vmax (int) – Default is max(angle(im)), the maximum value for the colormap

  • **kwargs – All other args are passed to fig.add_subplot(111, **kwargs)

Returns:

used_fig – The figure object that was actually plotted to.

Return type:

matplotlib.figure.Figure

cdtools.tools.plotting.plot_colorized(im, fig=None, basis=None, units='$\\mu$m', **kwargs)

Plots the colorized version of a complex array with dimensions NxM

The darkness corresponds to the intensity of the image, and the color corresponds to the phase.

If a figure is given explicitly, it will clear that existing figure and plot over it. Otherwise, it will generate a new figure.

If a basis is explicitly passed, the image will be plotted in real-space coordinates

Parameters:
  • im (array) – An complex array with dimensions NxM

  • fig (matplotlib.figure.Figure) – Default is a new figure, a matplotlib figure to use to plot

  • basis (np.array) – Optional, the 3x2 probe basis

  • units (str) – The length units to mark on the plot, default is um

  • **kwargs – All other args are passed to fig.add_subplot(111, **kwargs)

Returns:

used_fig – The figure object that was actually plotted to.

Return type:

matplotlib.figure.Figure

cdtools.tools.plotting.plot_translations(translations, fig=None, units='$\\mu$m', lines=True, invert_xaxis=True, **kwargs)

Plots a set of probe translations in a nicely formatted way

Parameters:
  • translations (array) – An Nx2 or Nx3 set of translations in real space

  • fig (matplotlib.figure.Figure) – Default is a new figure, a matplotlib figure to use to plot

  • units (str) – Default is um, units to report in (assuming input in m)

  • lines (bool) – Whether to plot lines indicating the path taken

  • invert_xaxis (bool) – Default is True. This flips the x axis to match the convention from .cxi files of viewing the image from the beam’s perspective

  • **kwargs – All other args are passed to fig.add_subplot(111, **kwargs)

Returns:

used_fig – The figure object that was actually plotted to.

Return type:

matplotlib.figure.Figure

cdtools.tools.plotting.get_units_factor(units)

Gets the multiplicative factor associated with a length unit

Parameters:

units (str) – The abbreviation for the unit type

Returns:

factor – The factor meters / (unit)

Return type:

float

cdtools.tools.plotting.plot_nanomap(translations, values, fig=None, units='$\\mu$m', convention='probe', invert_xaxis=True)

Plots a set of nanomap data in a flexible way

Parameters:
  • translations (array) – An Nx2 or Nx3 set of translations in real space

  • values (array) – A length-N object of values associated with the translations

  • fig (matplotlib.figure.Figure) – Default is a new figure, a matplotlib figure to use to plot

  • units (str) – Default is um, units to report in (assuming input in m)

  • convention (str) – Default is ‘probe’, alternative is ‘obj’. Whether the translations refer to the probe or object.

  • invert_xaxis (bool) – Default is True. This flips the x axis to match the convention from .cxi files of viewing the image from the beam’s perspective

Returns:

used_fig – The figure object that was actually plotted to.

Return type:

matplotlib.figure.Figure

cdtools.tools.plotting.plot_real(im, fig=None, basis=None, units='$\\mu$m', cmap='viridis', cmap_label='Real Part (a.u.)', **kwargs)

Plots the real part of a complex array with dimensions NxM

If a figure is given explicitly, it will clear that existing figure and plot over it. Otherwise, it will generate a new figure.

If a basis is explicitly passed, the image will be plotted in real-space coordinates

Parameters:
  • im (array) – An complex array with dimensions NxM

  • fig (matplotlib.figure.Figure) – Default is a new figure, a matplotlib figure to use to plot

  • basis (np.array) – Optional, the 3x2 probe basis

  • units (str) – The length units to mark on the plot, default is um

  • cmap (str) – Default is ‘viridis’, the colormap to plot with

  • cmap_label (str) – What to label the colorbar when plotting

  • **kwargs – All other args are passed to fig.add_subplot(111, **kwargs)

Returns:

used_fig – The figure object that was actually plotted to.

Return type:

matplotlib.figure.Figure

cdtools.tools.plotting.plot_imag(im, fig=None, basis=None, units='$\\mu$m', cmap='viridis', cmap_label='Imaginary Part (a.u.)', **kwargs)

Plots the imaginary part of a complex array with dimensions NxM

If a figure is given explicitly, it will clear that existing figure and plot over it. Otherwise, it will generate a new figure.

If a basis is explicitly passed, the image will be plotted in real-space coordinates

Parameters:
  • im (array) – An complex array with dimensions NxM

  • fig (matplotlib.figure.Figure) – Default is a new figure, a matplotlib figure to use to plot

  • basis (np.array) – Optional, the 3x2 probe basis

  • units (str) – The length units to mark on the plot, default is um

  • cmap (str) – Default is ‘viridis’, the colormap to plot with

  • cmap_label (str) – What to label the colorbar when plotting

  • **kwargs – All other args are passed to fig.add_subplot(111, **kwargs)

Returns:

used_fig – The figure object that was actually plotted to.

Return type:

matplotlib.figure.Figure

cdtools.tools.plotting.plot_nanomap_with_images(translations, get_image_func, values=None, mask=None, basis=None, fig=None, nanomap_units='$\\mu$m', image_units='$\\mu$m', convention='probe', image_title='Image', image_colorbar_title='Image Amplitude', nanomap_colorbar_title='Integrated Intensity', cmap='viridis', **kwargs)

Plots a nanomap, with an image or stack of images for each point

In many situations, ptychography data or the output of ptychography reconstructions is formatted as a set of images associated with various points in real space. This function is designed to allow for browsing through this kind of data, by making it possible to visualize a