Matthias Pospiech
Matthias Pospiech

Reputation: 3494

Matlab: Plot a line over an image plot

I want to plot a line plot on top of an image plot in Matlab

First I plot the image data

figure(1); clf;
imagesc(t); colorbar
hold on;
axis tight

and then the line plot

line(ysum,y,'Color','red')
hold off;

The line plot however deletes the image and sets the background to white. How can I plot on top of the image?

Upvotes: 1

Views: 860

Answers (1)

jerlich
jerlich

Reputation: 362

Your code isn't wrong, but it is not a minimal reproducible example, since you haven't defined t, y, ysum. When you call imagesc(t) the rows and columns will be the indices of t. In other words, it is the same as calling imagesc([1, size(t,2)], [1, size(t,1)], t). If t is small (say 10 x 10) but the elements of y,ysum are large (e.g. > 1000) then the 10 x 10 image will still be there, but it will be squished into the corner. Almost invisible.

So you need to make sure that the range of y, ysum, t line up. A quick work-around:

xidx = [min(ysum), max(ysum)];
yidx = [min(y), max(y)];
imagesc(xidx, yidx, t);

Upvotes: 1

Related Questions