Colorbar with background image
Afficher commentaires plus anciens
Hello!
I am trying to plot some short line segments, colored by time. This works well using surf. However, I want to also have a background binary image (here, "BW") that I am plotting these segments over.
I am running into an issue where the background image changes the scaling of the colorbar, which I want to be scaled to time. I think in theory I could use clim, but I am not sure how to isolate the axes of just my segments (and not BW) since I am using surf in a for loop.
Any help would be greatly appreciated!
Thanks,
VK
% example code
figure; hold on
BW = (imbinarize(image));
imshow(BW); % background image
for i = 1:length(tracks) % plot each segment
x = [tracks(i).x]; y = [tracks(i).y]; z = [tracks(i).time];
s = surf([x(:) x(:)],[y(:) y(:)], [z(:) z(:)],...
'FaceColor','none',...
'EdgeColor','interp',...
'LineWidth',2);
end
view(2)
colorbar % very close, just need to apply the colors to the segments only and not background image...
4 commentaires
Image Analyst
le 21 Avr 2026
How about a screenshot of what you got and what you want?
dpb
le 22 Avr 2026
I would overlay two axes and then you can put the background image in the one and the surf() components in the other -- and then pass the object handles to colorbar
Victoria
le 27 Avr 2026
Maybe there is a different way to color each segment by time instead of using surf?
figure; %hold on
BW = squeeze(imbinarize(arr_0(1,:,:)));
imshow(BW); % background image
ax1=gca;
ax2 = axes(Position=ax1.Position);
for i = 1:length(tracksToUse) % for each segment
x = [tracksToUse(i).xLoc]/99; y = [tracksToUse(i).yLoc]/99; z = [tracksToUse(i).frame];
surface(ax2,[x(:) x(:)],[y(:) y(:)], [z(:) z(:)],...
'FaceColor','none',...
'EdgeColor','interp',...
'LineWidth',2);
end
Or you can use hold on, but apply it to ax2,
figure;
BW = squeeze(imbinarize(arr_0(1,:,:)));
imshow(BW); % background image
ax1=gca;
ax2 = axes(Position=ax1.Position);
hold(ax2,'on');
for i = 1:length(tracksToUse) % for each segment
x = [tracksToUse(i).xLoc]/99; y = [tracksToUse(i).yLoc]/99; z = [tracksToUse(i).frame];
surf(ax2,[x(:) x(:)],[y(:) y(:)], [z(:) z(:)],...
'FaceColor','none',...
'EdgeColor','interp',...
'LineWidth',2);
end
All of this is incidental, however, to your original question about colorbars. To add colorbars, see my answer below.
Réponses (2)
You can use this as a template:
% Create sample data
[X,Y] = meshgrid(linspace(-3,3,200));
A = exp(-(X.^2 + Y.^2)); % smooth Gaussian
B = sin(3*X).*cos(3*Y); % oscillatory pattern
% Plot both in overlapping axes
h1 = imagesc(A);
ax1=gca;
ax2 = axes(Position=ax1.Position);
h2 = surf(ax2,X,Y, B,'FaceAlpha',0.05,'EdgeColor','none');
colormap(ax1, gray(256))
colormap(ax2, 'parula')
axis([ax1,ax2],'off');
colorbar(ax1,'Visible','off');
colorbar(ax2);
title({'Surface B overlayed on image A', 'Colorbar corresponds to B'});
set(gcf,'Position',gcf().Position.*[1,1,1,1.2])
Voss
le 22 Avr 2026
tmp = arrayfun(@(x)x.time(:),tracks,'UniformOutput',false);
tmp = vertcat(tmp{:});
[z_min,z_max] = bounds(tmp);
clim([z_min,z_max])
Catégories
En savoir plus sur Blue dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!


