how to place an image under my line plot in same figure - both same size?
Afficher commentaires plus anciens
hi,
i have created a series of x,y line plots using 'plot' where x,y = longitude,latitude (then add scale bars, patch color, etc.)
figure(1);
plot(x2W, w2,'linewidth',3)
i also have created a similar set of images of the planet surface in the same locations and dimensions. these i was successfully able to make the exact same size Figure and image dimensions as the line 'plot' above (using gca, gcf, ylim, xlim etc.), for the purpose of overlaying the plot on the image graphic directly.
figure(2);
stryng =[FRUMo,'-limb-base.jpg'];
rgbImage = imread(stryng);
imshow(rgbImage)
set(gcf,'position',[580,377,850,500])
set(gca, 'position', [positioning(1) positioning(2) positioning(3) positioning(4)])
goal is to lay the plot directly on top of the image (i now have both appearing as exactly the same Figure and Plot dimensions for that purpose). i have tried a dozen proposed solutions to this based on posts in the Community but unable to get any to work. currently i save them as external jpegs and use graphic converter to lay one on the other, but want to do it all internal in matlab.
is there a way to simply paste the line Figure onto the image Figure? or stack them (was unable to get uistack to function proper)?
thanks
paul
1 commentaire
dpb
le 4 Fév 2026
Attach the data and complete code to create the figures
Also showing what you have tried specifically would never be bad
Réponses (3)
The easiest way is to use imshow first and then layer on the line objects created by plot (don't forget to set hold to 'on').
You can also first call plot and then image (which is the base object created by imshow).
[im,map] = imread("corn.tif");
imshow(im,map)
hold on
x=rand(1,10)*size(im,2);
y=rand(1,10)*size(im,1);
plot(x,y,'w*--')
axis on
Note that the direction is inverted for the y-axis, as is convention for images.
4 commentaires
Paul Schenk
le 4 Fév 2026
Modifié(e) : Rik
le 4 Fév 2026
Rik
le 4 Fév 2026
Well, as you can see in my example, this code works if you want to show an image and a plot in the same axes.
If this setup doesn't work for you, be sure to include all data and code required to reproduce what you have (check with the green triangle to make sure everything runs). That way you make sure we see the same thing you do. If your data is confidential you should create an example image/data without confidential information.
dpb
le 4 Fév 2026
As @ Rik says, create one figure and the axes for one first. The axes of the line plot is shrunk by some internally computed amount to leave room for the colorbar so it is likely you'll want to use it as the reference for the image axes. You'll have to scale the coordinates of the image to the x/ylim range of the plot to have them both on the same scale.
Alternatively, you can use two axes setting the 'Position' property of the image to match that of the line plot.
Paul Schenk
le 4 Fév 2026
Modifié(e) : Paul Schenk
le 4 Fév 2026
Walter Roberson
le 4 Fév 2026
0 votes
Start by using image() or imshow() with the XData and YData options to draw the image in an axes. Those options allow you to set exact data coordinates for drawing the image. Note that XData nd YData give the coordinates for the centers of the pixels for the bottom left and top right corners, so for pixel-exact placement you will need to fudge XData and YData slightly.
Once you have drawn the image, use hold on and then plot() your data. The plots will go on top of the image in this configuration.
9 commentaires
Paul Schenk
le 4 Fév 2026
Modifié(e) : dpb
le 5 Fév 2026
Paul Schenk
le 5 Fév 2026
Modifié(e) : Paul Schenk
le 5 Fév 2026
If you want real answers, agan, attach the data so somebody can actually do something specific for your case.
One minor nit -- in the above, the vector 'positioning' is undefined but
set(gca, 'position', [positioning(1) positioning(2) positioning(3) positioning(4)])
could be written simply as
set(gca, 'position',positioning)
with any adjustments to particular components having been made earlier.
Walter Roberson
le 5 Fév 2026
Is the JPG image RGB or is it grayscale ?
If it is RGB then colormap manipulations in the same axes do not affect it.
If it is grayscale, and you want it to stay grayscale, then
imshow(rgbImage(:,:,[1 1 1]),'XData',xl6,'YData',yl6)
The variable name you are using suggests that it is an RGB image; colormap(parula) on an RGB image will not affect the RGB image.
IM = imread('peppers.png');
imshow(IM)
IM = imread('peppers.png');
imshow(IM)
hold on
plot([1 200], [100 90], 'linewidth', 2)
colormap(parula)
colorbar()
Notice that the image is not affected by the colormap() and notice that the line is colored as expected by parula with the default color mappings.
Walter Roberson
le 5 Fév 2026
There is a difference between splitting the axes and drawing everything on the same axes. When you split the axes, then if you patch() into an axes that does not have hold on turned on, then by default the view is set to 3D for the patch(). When you draw in the axes first and hold on, then the patch() leaves the viewpoint as-is, defaulting to a 2D view.
Paul Schenk
le 5 Fév 2026
Modifié(e) : Paul Schenk
le 6 Fév 2026
Paul Schenk
le 6 Fév 2026
Image Analyst
le 7 Fév 2026 à 14:41
0 votes
Attached are some simple demo scripts that insert/inset/overlay/place/composite/superimpose (whatever you want to call it) images and plots on other images or plots in an axes.
1 commentaire
Paul Schenk
le 7 Fév 2026 à 14:52
Catégories
En savoir plus sur Red dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!





