cdiutils.interactive.Plotter#
- class cdiutils.interactive.Plotter(data, plot='slices', log=False, cmap='turbo', figsize=(10, 10), fontsize=15, title=None, layers_kwargs=None)[source]#
Class to plot data from files, NumPy arrays, or layered 3D datasets.
This class provides a unified interface for visualizing data commonly produced in BCDI experiments, supporting both static matplotlib-based plots and interactive multi-layer 3D visualization.
- Parameters:
data (str | ndarray | dict[str, ndarray]) – Data to plot. One of: - str: path to a file (.npy, .npz, .cxi, .h5) - np.ndarray: array to plot - dict[str, np.ndarray]: required when plot=’layers’
plot (Literal['2D', 'slices', 'phase_slices', 'contour_slices', 'sum_slices', 'sum_contour_slices', '3D', '1D', 'layers']) –
Plot type. One of: - ‘2D’ - ‘1D’ - ‘slices’ - ‘phase_slices’ - ‘contour_slices’ - ‘sum_slices’ - ‘sum_contour_slices’ - ‘3D’ - ‘layers’ (interactive multi-layer 3D viewer)
Default is ‘slices’.
log (bool) – Display data in logarithmic scale (static plots only).
cmap (str) – Colormap name (used for static plots only).
figsize (tuple[int, int]) – Figure size in inches.
fontsize (int) – Base font size for labels, ticks, and titles.
title (str | None) – Optional plot title.
layers_kwargs (dict | None) –
Keyword arguments forwarded to MultiVolumeViewer (only used when plot=’layers’).
- Supported keys:
- voxel_size: tuple[float, float, float]
Physical voxel size along each axis. Used to convert voxel indices to physical coordinates for slices, planes, and clips.
- unit: str | None
Optional unit label for spatial axes. If provided, axis titles are displayed as “X (unit)”, “Y (unit)”, “Z (unit)”. If None (default), axes are shown without units.
- PLOT_ORDER: Literal[‘xyz’, ‘zyx’]
Axis ordering convention used for visualization.
- CBAR_LEN: float
Relative colorbar length.
- render_workers: int | None
Number of parallel render workers (used for animation export).
- render_in_flight: int | None
Maximum number of frames rendered concurrently.
- rendering_mode: Literal[‘safe’, ‘fast’, ‘process’]
Rendering backend strategy used during animation export.
Ignored for all other plot modes.
- data_array#
NumPy array containing the loaded data (static plot modes).
- data_dict#
Dictionary of named 3D arrays (used when plot=’layers’).
- plot#
Selected plot type.
- log#
Whether logarithmic scaling is enabled.
- cmap#
Selected colormap.
- figsize#
Figure size in inches.
- fontsize#
Base font size.
- title#
Plot title.
- layers_kwargs#
Keyword arguments forwarded to the multi-layer viewer.
- filename#
Name of the loaded file when data is provided as a path.
- __init__(data, plot='slices', log=False, cmap='turbo', figsize=(10, 10), fontsize=15, title=None, layers_kwargs=None)[source]#
Initialise the Plotter class.
- Parameters:
data (str | ndarray | dict[str, ndarray]) – Data to plot. One of: - str: path to a file (.npy, .npz, .cxi, .h5) - np.ndarray: array to plot - dict[str, np.ndarray]: required when plot=’layers’
plot (Literal['2D', 'slices', 'phase_slices', 'contour_slices', 'sum_slices', 'sum_contour_slices', '3D', '1D', 'layers']) – Plot type. One of: - ‘2D’ - ‘1D’ - ‘slices’ - ‘phase_slices’ - ‘contour_slices’ - ‘sum_slices’ - ‘sum_contour_slices’ - ‘3D’ - ‘layers’ (multi-layer 3D viewer)
log (bool) – Display data in logarithmic scale.
cmap (str) – Colormap name. (for static plot)
figsize (tuple[int, int]) – Figure size in inches.
fontsize (int) – Base font size for labels, ticks, and titles.
title (str | None) – Optional plot title.
layers_kwargs (dict | None) –
Keyword arguments forwarded to MultiVolumeViewer (only used when plot=’layers’).
- Supported keys:
- voxel_size: tuple[float, float, float]
Physical voxel size along each axis.
- PLOT_ORDER: Literal[‘xyz’, ‘zyx’]
Axis ordering convention.
- CBAR_LEN: float
Relative colorbar length.
- render_workers: int | None
Number of parallel render workers (for animation).
- render_in_flight: int | None
Maximum number of in-flight render tasks.
- rendering_mode: Literal[‘safe’, ‘fast’, ‘process’]
Rendering backend strategy (for animation).
- init_plot()[source]#
Initialise a plot of the data stored in the data_array attribute.
The type of plot and the parameters are specified in the class constructor. The plot can be a 2D plot, 3D slices, contour plots of slices, sum of slices, sum of contour plots of slices, or a 3D plot. The specific plot type is determined by the value of the plot attribute. If the number of dimensions of the data_array is not compatible with the specified plot type, the function simply prints the number of dimensions and shape of the data_array.
- data_array#
An array containing the data to be plotted.
- plot#
The type of plot to be generated, which can be one of the following: “2D”, “slices”, “phase_slices”, “contour_slices”, “sum_slices”, “sum_contour_slices”, “3D” or “layers”.
- figsize#
The size of the plot in inches.
- fontsize#
The font size of the plot.
- log#
If True, plot the data in logarithmic scale.
- cmap#
The colourmap to be used for the plot.
- title#
The title of the plot.
- get_data_array()[source]#
Return the data array stored in the class instance by reading the specified file.
The file must have a .npy, .cxi, .h5, or .npz extension. If the file is a .npy or .h5 file, the data array is directly loaded. If the file is a .cxi file, the data array is loaded from f.root.entry_1.data_1.data[:] or f.root.entry_1.image_1.data[:], following cxi conventions. If the file is a .npz file, the user is prompted to select the data array from a dropdown list of arrays stored in the .npz file.
If the file extension is supported and the data array is successfully loaded, the init_plot function is called.
- Returns:
A Numpy array representing the data stored in the class, or None if the file could not be loaded.
- Raises:
KeyError – If the file is a .cxi or .h5 file, and the data
could not be found in either `f.root.entry_1.data_1.data[ –
]`
or `f.root.entry_1.image_1.data[ –
]`.