Plotting a graph on an image

7 views (last 30 days)
Abdulrehman Zia
Abdulrehman Zia on 8 Apr 2022
Commented: Image Analyst on 9 Apr 2022
I'm not sure why this code doesn't work. Whenever I run it the plot overrights the image even though hold is on. It seems to be because of the set statement but whenever I remove it the graph doesn't even show up. errors is a 1*21 column vector the image is 750*800. The plot needs to have tickmarks and labels Edit: I figured out the the image and the plot are both showing up but the scale is wrong so they never apppear in the same frame
[m,n]=size(images);
image(images)
colormap("gray")
hold on
plot([1:21],errors,'Color','red')
set(gca,'YLim',[0 max(errors)],'XLim',[0 numFrames],'YDir','normal', ...
'DataAspectRatio',[21/n,max(errors)/m,1])
hold off

Answers (1)

Image Analyst
Image Analyst on 8 Apr 2022
This is definitely going to be a problem:
image(image)
image() is a built-in function. Do not use it as the name of your variable.
This works:
grayImage = imread('cameraman.tif');
[rows, columns, numberOfColorChannels] = size(grayImage);
imshow(grayImage, []);
colormap("gray");
hold on
errors = rows * rand(21, 1); % Whatever...
plot([1:21],errors, 'r-', 'LineWidth', 2); % You might think it's upside down. If so invert it and scale it.
% set(gca,'YLim',[0 max(errors)],'XLim',[0 numFrames],'YDir','normal', ...
% 'DataAspectRatio',[21/n,max(errors)/m,1])
hold off
  2 Comments
Abdulrehman Zia
Abdulrehman Zia on 9 Apr 2022
This doesn't work for me cause I need the plot to have tickmarks and labels
Image Analyst
Image Analyst on 9 Apr 2022
So just add
axis('on', 'image');
Note that your image and your plot might have two different scales so you will have to decide which axes you want. It would probably be better to plot them in two separate axes rather than the plot on top of the image.

Sign in to comment.

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!