How to display an image and a plot on the same figure while controlling axes
25 views (last 30 days)
Show older comments
I want to display an image (a 720 lines by 1280 columns uint8 matrix) using imshow() and an usual plot using plot() on the same figure.
Example:
my_image = uint8(linspace(1,0,720)'*linspace(1,0,1280)*255); % some diagonal color gradient
angles = linspace(0,4*pi,200);
my_plot = abs(sin(angles)); % a redressed sine wave
figure;
imshow(my_image); % show this image, as if it were a background
hold on;
plot(angles,my_plot); % plot the sine on top of it
However, the plot follows the axes of the image, ie. 0<=x<=1280 and 0<=y<=720. In addition, it is reversed, the vertical y axis increasing from top (0) to bottom (720).
axis([0,4*pi,0,1]) makes the figure fit the plot, but then the image is cut.
How can I change the axes of the figure, say to 0<=x<=4pi and 0<=y<=1, allowing me to superimpose both while filling the figure frame?
What I have:

What I want, without too much hassle:

For this image I mapped the sine to match the image's axes, but I have many more functions I need to apply this too.
I don't care about labels, ticks, and tick labels.
0 Comments
Accepted Answer
Voss
on 9 Mar 2023
my_image = uint8(linspace(1,0,720)'*linspace(1,0,1280)*255); % some diagonal color gradient
angles = linspace(0,4*pi,200);
my_plot = abs(sin(angles)); % a redressed sine wave
figure;
image( ...
'XData',angles([1 end]), ...
'YData',[min(my_plot) max(my_plot)], ...
'CData',my_image); % show this image, as if it were a background
hold on;
plot(angles,my_plot); % plot the sine on top of it
colormap('gray')
2 Comments
More Answers (1)
Sulaymon Eshkabilov
on 8 Mar 2023
Here is how it can be attained:
my_image = uint8(linspace(1,0,720)'*linspace(1,0,1280)*255); % some diagonal color gradient
angles = linspace(0,4*pi,200);
my_plot = abs(sin(angles)); % a redressed sine wave
figure;
yyaxis left
image(my_image); % show this image, as if it were a background
yyaxis right
plot(angles,my_plot); % plot the sine on top of it
axis([0 4*pi 0 1])
0 Comments
See Also
Categories
Find more on Image Processing Toolbox 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!
