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

40 Ansichten (letzte 30 Tage)
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.

Akzeptierte Antwort

Voss
Voss am 9 Mär. 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 Kommentare
C7
C7 am 9 Mär. 2023
Verschoben: Voss am 9 Mär. 2023
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 am 9 Mär. 2023
Glad to hear you got it working like you wanted!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Sulaymon Eshkabilov
Sulaymon Eshkabilov am 8 Mär. 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])

Kategorien

Mehr zu Images finden Sie in Help Center und File Exchange

Produkte


Version

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by