How to display an image and a plot on the same figure while controlling axes

조회 수: 26 (최근 30일)
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.

채택된 답변

Voss
Voss 2023년 3월 9일
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
C7
C7 2023년 3월 9일
이동: Voss 2023년 3월 9일
Thank you both very much. Your solution with image() worked.
To bring the y-axis back to its normal orientation I simply specified ...'YData',[max(my_plot) min(my_plot)]...
For some reason I could not identify, this did not always work, so I used set(gca,'Ydir','reverse') as described in this post.
For cosmetics if made the image fit the border with axis(), removed the labels with axis off, and had the aspect ratio be the original one using pbaspect().
My final take:
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;
hold on;
image([angles(1),angles(end)],[1,0],my_image); % displaying my_image specifying its coordinate span
colormap('gray'); % bring the colormap back to black and white
plot(angles,my_plot); % plot the sine on top of it
axis([angles(1),angles(end),0,1]); % make everything fit the border
axis off; % to be left with the figure only
pbaspect([1280 720 1]); % restore the screen's aspect ratio
Have a nice day.
Voss
Voss 2023년 3월 9일
Glad to hear you got it working like you wanted!

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Sulaymon Eshkabilov
Sulaymon Eshkabilov 2023년 3월 8일
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])

카테고리

Help CenterFile Exchange에서 Image Processing Toolbox에 대해 자세히 알아보기

제품


릴리스

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by