Add fixed colorbar to volshow

15 views (last 30 days)
Lara
Lara on 30 Apr 2025
Commented: Lara on 1 May 2025
I have my code which is plotting results in a loop. I am visualizing them on the volumeviewer in 3D. for every frame in my loop there is a new figure and it updates accordingly.
my code is this:
%% Initialize volume visualization
% Define global color scale limits
globalMin = -1.5; % Minimum value across all frames
globalMax = 1.5; % Maximum value across all frames
% Normalize your volume for consistent color mapping
normalizedData = (compressed_hbt - globalMin) / (globalMax - globalMin);
normalizedData(normalizedData < 0) = 0; % Clamp values below 0
normalizedData(normalizedData > 1) = 1; % Clamp values above 1
% Use 256-color colormap
cmap = jet(256);
% Create volume viewer only once
if i == 1
% Create volume viewer with white background
vol_hbt = volshow(zeros(size(compressed_hbt)), ...
'Colormap', cmap, ...
'Alphamap', (linspace(0,0.7,256).^2));
end
% Update Volshow with normalized data
vol_hbt.Data = normalizedData;
% Keep the colorbar visible by forcing a refresh in the figure
drawnow;
but I want to insert a colorbar with values fixed into the volume viewer. I tried different approaches, but it won't show in the window. the axis can just be a fixed jet colorbar iwth constant ticks & a title. How can i realize this?

Accepted Answer

Adam Danz
Adam Danz on 30 Apr 2025
Showing a colorbar with volshow
Adding a colorbar directly to the figure produced by volshow is not supported. Here's a workaround that can be summarized by these steps:
  1. The object produced by volshow can be parented to a viewer object which can be parented to a figure, panel, GridLayout, or a Tab object. This demo uses a uipanel.
  2. The uipanel is positioned with space on the right for a colorbar.
  3. An invisible axes is added to the chart which is required to host a colorbar. The axes' colormap and color limit (CLim) is set to the same colormap used in the object produced by volshow.
  4. A colorbar is added and positioned to the right, next to the uipanel.
  5. The figure and uipanel colors are set to the same color as the viewer to give the figure a unified appearance.
% Create a figure that hosts a viewer3d object in a uipanel, leaving room
% for a colorbar.
fig = uifigure();
uip = uipanel(fig,Units="normalized",Position=[0 0 .90 1],BorderType="none");
viewer = viewer3d(uip);
% Show volshow within the viewer3d
imcube = rand(30, 30, 100) .^(permute(linspace(0, 15, 100), [3, 1, 2]));
vol = volshow(imcube, Colormap=cool,RenderingStyle="Isosurface",Parent=viewer);
viewer.BackgroundColor = [0 0 0];
viewer.GradientColor = [0.2 0.2 0.2];
% Add an invisible axes so that a colorbar can be added and placed on the
% right
ax = axes(fig, Visible="off", HandleVisibility="off",HitTest="off",PickableParts="none");
ax.Toolbar.Visible = 'off';
% Assign desired colormap and create a colorbar\
ax.Colormap = vol.Colormap;
ax.CLim = [min(vol.Data,[],'all'),max(vol.Data,[],'all')];
cb = colorbar(ax,"Units","normalized","Color",[.8 .8 .8]);
cb.Position(1) = 0.90;
% Set figure and uipanel color to the same color as the viewer
fig.Color = viewer.BackgroundColor;
uip.BackgroundColor = viewer.BackgroundColor;

More Answers (1)

Walter Roberson
Walter Roberson on 30 Apr 2025
You cannot do that. volshow() creates a standalone chart object.
There is an internal vol_hbt.Parent.Axes property but it is a images.ui.graphics.internal.Axes not a standard axes matlab.graphics.axis.Axes and cannot serve as the parent for a colorbar.
  2 Comments
Lara
Lara on 30 Apr 2025
is ther no way to input the colorbar as a dummy image into the window? It doesnt have to be interactive, I can construct it. It is only for visualization
Walter Roberson
Walter Roberson on 30 Apr 2025
No, there is not. The vol_hbt.Parent.Axes object does not even have a Children property

Sign in to comment.

Categories

Find more on Color and Styling in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!