This is the way to customize the plots in Matplotlib
def visualize_hyperspectral_radiographs(hyperspectral_stack: np.ndarray,
selected_indices: Optional[list[int]] = None,
figsize: tuple[int, int] = (15, 7),
cmap: str = "gray",
percentile_range: tuple[float, float] = (2, 98)) -> plt.Figure:
"""
Visualize selected radiographs from a hyperspectral stack.
Parameters:
-----------
hyperspectral_stack : numpy.ndarray
3D array with shape (height, width, n_spectral_bins)
selected_indices : list of int, optional
Indices of radiographs to visualize. If None, uses beginning, middle, and end.
figsize : tuple, optional
Figure size (width, height). Default is (15, 7).
cmap : str, optional
Colormap for displaying images. Default is "gray".
percentile_range : tuple, optional
Percentile range for intensity scaling (min, max). Default is (2, 98).
Returns:
--------
fig : matplotlib.figure.Figure
The created figure object
"""
# Default to beginning, middle, and end if no indices provided
if selected_indices is None:
n_images = hyperspectral_stack.shape[-1]
selected_indices = [0, n_images//2, n_images-1]
# Create figure with gridspec layout
fig = plt.figure(figsize=figsize)
gs = gridspec.GridSpec(2, len(selected_indices), height_ratios=[5, 0.3], hspace=0.01)
# Calculate dynamic range from selected subset
sub_selection = hyperspectral_stack[..., selected_indices]
vmin = np.nanpercentile(sub_selection, percentile_range[0])
vmax = np.nanpercentile(sub_selection, percentile_range[1])
# Create subplots for images in the top row
img = None # Will store the last image for colorbar
for i, idx in enumerate(selected_indices):
ax = fig.add_subplot(gs[0, i])
ax.set_title(f"Radiograph {idx}")
img = ax.imshow(hyperspectral_stack[..., idx], cmap=cmap, origin="lower",
vmin=vmin, vmax=vmax)
ax.axis("off")
# Create colorbar in the bottom row, spanning all columns
cbar_ax = fig.add_subplot(gs[1, :])
cbar = plt.colorbar(img, cax=cbar_ax, orientation='horizontal')
cbar.set_label('Intensity', fontsize=12)
return fig
_ = visualize_hyperspectral_radiographs(hyperspectral_stack)