Colorbar with background image

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...

2 commentaires

Image Analyst
Image Analyst il y a environ une heure
How about a screenshot of what you got and what you want?
dpb
dpb il y a environ une heure
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

Connectez-vous pour commenter.

Réponses (2)

Matt J
Matt J il y a environ 2 heures
Modifié(e) : Matt J il y a environ 2 heures
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
Voss il y a environ une heure

0 votes

tmp = arrayfun(@(x)x.time(:),tracks,'UniformOutput',false);
tmp = vertcat(tmp{:});
[z_min,z_max] = bounds(tmp);
clim([z_min,z_max])

Question posée :

le 21 Avr 2026 à 21:47

Réponse apportée :

le 22 Avr 2026 à 4:50

Community Treasure Hunt

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

Start Hunting!

Translated by