plottingfuncs#

Plotting data.

class topostats.plottingfuncs.Images(data: numpy.typing.NDarray, output_dir: str | Path, filename: str, style: str | Path = None, pixel_to_nm_scaling: float = 1.0, masked_array: numpy.typing.NDarray = None, plot_coords: numpy.typing.NDArray = None, title: str = None, image_type: str = 'non-binary', image_set: str = 'core', core_set: bool = False, pixel_interpolation: str | None = None, cmap: str | None = None, mask_cmap: str = 'jet_r', region_properties: dict = None, zrange: list = None, colorbar: bool = True, axes: bool = True, num_ticks: tuple[int | None] = (None, None), save: bool = True, savefig_format: str | None = None, histogram_log_axis: bool = True, histogram_bins: int | None = None, savefig_dpi: str | float | None = None)[source]#

Plots image arrays.

Parameters:
  • data (npt.NDarray) – Numpy array to plot.

  • output_dir (str | Path) – Output directory to save the file to.

  • filename (str) – Filename to save image as.

  • style (str | Path) – Filename of matplotlibrc parameters.

  • pixel_to_nm_scaling (float) – The scaling factor showing the real length of 1 pixel in nanometers (nm).

  • masked_array (npt.NDarray) – Optional mask array to overlay onto an image.

  • plot_coords (npt.NDArray) – ??? Needs defining.

  • title (str) – Title for plot.

  • image_type (str) – The image data type, options are ‘binary’ or ‘non-binary’.

  • image_set (str) – The set of images to process, options are ‘core’ or ‘all’.

  • core_set (bool) – Flag to identify image as part of the core image set or not.

  • pixel_interpolation (str, optional) – Interpolation to use (default is ‘None’).

  • cmap (str, optional) – Colour map to use (default ‘nanoscope’, ‘afmhot’ also available).

  • mask_cmap (str) – Colour map to use for the secondary (masked) data (default ‘jet_r’, ‘blu’ provides more contrast).

  • region_properties (dict) – Dictionary of region properties, adds bounding boxes if specified.

  • zrange (list) – Lower and upper bound to clip core images to.

  • colorbar (bool) – Optionally add a colorbar to plots, default is False.

  • axes (bool) – Optionally add/remove axes from the image.

  • num_ticks (tuple[int | None]) – The number of x and y ticks to display on the iage.

  • save (bool) – Whether to save the image.

  • savefig_format (str, optional) – Format to save the image as.

  • histogram_log_axis (bool) – Optionally use a loagrithmic y-axis for the histogram plots.

  • histogram_bins (int, optional) – Number of bins for histograms to use.

  • savefig_dpi (str | float, optional) – The resolution of the saved plot (default ‘figure’).

Methods

plot_and_save()

Plot and save the image.

plot_curvatures(image, cropped_images, ...)

Plot curvature intensity and defects of grains in an image.

plot_curvatures_individual_grains(...)

Plot curvature intensity and defects of individual grains.

plot_histogram_and_save()

Plot and save a histogram of the height map.

save_figure()

Save figures as plt.savefig objects.

plot_and_save()[source]#

Plot and save the image.

Returns:

Matplotlib.pyplot figure object and Matplotlib.pyplot axes object.

Return type:

tuple

plot_curvatures(image: npt.NDArray, cropped_images: dict, grains_curvature_stats_dict: dict, all_grain_smoothed_data: dict, colourmap_normalisation_bounds: tuple[float, float]) tuple[plt.Figure | None, plt.Axes | None][source]#

Plot curvature intensity and defects of grains in an image.

Parameters:
  • image (npt.NDArray) – Image to plot.

  • cropped_images (dict) – Dictionary containing cropped images of grains and the bounding boxes and padding.

  • grains_curvature_stats_dict (dict) – Dictionary of grain curvature statistics.

  • all_grain_smoothed_data (dict) – Dictionary containing smoothed grain traces.

  • colourmap_normalisation_bounds (tuple[float, float]) – Tuple of the colour map normalisation bounds.

Returns:

Matplotlib.pyplot figure object and Matplotlib.pyplot axes object.

Return type:

tuple[plt.Figure | None, plt.Axes | None]

plot_curvatures_individual_grains(cropped_images: dict, grains_curvature_stats_dict: dict, all_grains_smoothed_data: dict, colourmap_normalisation_bounds: tuple[float, float]) None[source]#

Plot curvature intensity and defects of individual grains.

Parameters:
  • cropped_images (dict) – Dictionary of cropped images.

  • grains_curvature_stats_dict (dict) – Dictionary of grain curvature statistics.

  • all_grains_smoothed_data (dict) – Dictionary containing smoothed grain traces.

  • colourmap_normalisation_bounds (tuple) – Tuple of the colour map normalisation bounds.

plot_histogram_and_save() tuple | None[source]#

Plot and save a histogram of the height map.

Returns:

Matplotlib.pyplot figure object and Matplotlib.pyplot axes object.

Return type:

tuple | None

save_figure()[source]#

Save figures as plt.savefig objects.

Returns:

Matplotlib.pyplot figure object and Matplotlib.pyplot axes object.

Return type:

tuple

topostats.plottingfuncs.add_bounding_boxes_to_plot(fig, ax, shape: tuple, region_properties: list, pixel_to_nm_scaling: float) tuple[source]#

Add the bounding boxes to a plot.

Parameters:
  • fig (plt.figure.Figure) – Matplotlib.pyplot figure object.

  • ax (plt.axes._subplots.AxesSubplot) – Matplotlib.pyplot axes object.

  • shape (tuple) – Tuple of the image-to-be-plot’s shape.

  • region_properties (list) – Region properties to add bounding boxes from.

  • pixel_to_nm_scaling (float) – The scaling factor from px to nm.

Returns:

Matplotlib.pyplot figure object and Matplotlib.pyplot axes object.

Return type:

tuple

topostats.plottingfuncs.add_pixel_to_nm_to_plotting_config(plotting_config: dict, pixel_to_nm_scaling: float) dict[source]#

Add the pixel to nanometre scaling factor to plotting configs.

Ensures plots are in nanometres and not pixels.

Parameters:
  • plotting_config (dict) – TopoStats plotting configuration dictionary.

  • pixel_to_nm_scaling (float) – Pixel to nanometre scaling factor for the image.

Returns:

Updated plotting config with the pixel to nanometre scaling factor applied to all the image configurations.

Return type:

dict

topostats.plottingfuncs.dilate_binary_image(binary_image: numpy.typing.NDArray, dilation_iterations: int) numpy.typing.NDArray[source]#

Dilate a supplied binary image a given number of times.

Parameters:
  • binary_image (npt.NDArray) – Binary image to be dilated.

  • dilation_iterations (int) – Number of dilation iterations to be performed.

Returns:

Dilated binary image.

Return type:

npt.NDArray

topostats.plottingfuncs.load_mplstyle(style: str | Path) None[source]#

Load the Matplotlibrc parameter file.

Parameters:

style (str | Path) – Path to a Matplotlib Style file.

topostats.plottingfuncs.set_n_ticks(ax: matplotlib.pyplot.Axes.axes, n_xy: list[int | None, int | None]) None[source]#

Set the number of ticks along the y and x axes and lets matplotlib assign the values.

Parameters:
  • ax (plt.Axes.axes) – The axes to add ticks to.

  • n_xy (list[int, int]) – The number of ticks.

Returns:

The axes with the new ticks.

Return type:

plt.Axes.axes