how to place an image under my line plot in same figure - both same size?

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

Attach the data and complete code to create the figures
Also showing what you have tried specifically would never be bad

Connectez-vous pour commenter.

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
Paul Schenk le 4 Fév 2026
Modifié(e) : Rik le 4 Fév 2026
i will look at this tonite. the key here is that the image and plot are sized to be precisely identical. they are currently in two Figures showing on my viewer together but seperate.
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.
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
Paul Schenk le 4 Fév 2026
Modifié(e) : Paul Schenk le 4 Fév 2026
so i plot the lat-long data (which is a curved line) according to:
figure(3);
plot(x2W, w2,'linewidth',3)
xlim([round(minX2W)-1 round(maxX2W)+1]);
ylim([round(minW22)-10 round(maxW22)+10]);
which in current example turn out to be xlim=[0,6] and ylim=[280,305]
i then build an image map of the same xlim,ylim externally and import using:
f6 = figure;
figure(6);
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)])
this puts the image in exactly the same size Figure and in exactly the same location inside the Figure as the line plot, and in the same locations. the gca and gcf are derived from the first plot.
it does not let me change the image map axes to be lat-long, which i tried several times (and probably wrongly). it insisits on treating the axes of the image map as x=samp,y=line and when i tell it xlim and ylim are [0,6] and 280,305], it severly resmaples and crops the original image. if i could fix that to think its equal to lat-long it would help me overlay the plot on the image. or some other way to drape the plot on top of the image.

Connectez-vous pour commenter.

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

hi, that worked , sort of. it did plot the line at the correct location (to within a few pixels at least). but it will not do the following:
-it will not 'patch' in the z-colors in the way the original line plot did. (the line goes to black).
>>> i suspect this is because colormap & colorbar are both assigning colors to everything, including the pixels in the image, rather than just the data in the x,y,z axes in the plot (which are very low compared ot 0-255). have not been successful in solving this yet . . .
-it will not 'reverse' the y-axis in the way the original line plot did (latitude goes up of course). I note that the image remains in the correct orientation but the line is mirrored north-to-south)...
-it resizes the image/plot when the scale bar is added.
here is what i currently have . . .
figure(6);
stryng =[FRUMo,'-limb-base.jpg'];
rgbImage = imread(stryng);
imshow(rgbImage,'XData',xl6,'YData',yl6)
set(gcf,'position',[580,377,850,500])
set(gca, 'position', [positioning(1) positioning(2) positioning(3) positioning(4)])
hold on
axis on
plot(x2W,w2,'LineWidth',2)
set(gca, 'YDir','reverse')
%%%(i have also tried this . . . >>> gca.YDir = 'reverse';
colormap(parula)
patch(x2W,w2,z2,'EdgeColor','interp','FaceColor','none','linewidth',3)
colorbar
cb = colorbar('FontSize', 12);
ylabel(cb,'Elevation (km)','FontSize',14)
fyi: 2016b (yeah i know . . . )
Paul Schenk
Paul Schenk le 5 Fév 2026
Modifié(e) : Paul Schenk le 5 Fév 2026
or if i could reimport the saved map and plot PNGs (which i know how), and then overlay the graph plot image (setting white as transparent) onto the map image, which i dont know how to do, i could accomplish the same objective (mainly because the sizes are identical already) . . .
p
>i only have the mapping toolkit
dpb
dpb le 5 Fév 2026
Modifié(e) : dpb 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.
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)
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.
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
Paul Schenk le 5 Fév 2026
Modifié(e) : Paul Schenk le 6 Fév 2026
this is fine as far as it goes, but the patch is for x,y,z where z is elevation values assigned at each point . . .
when i insert the 'patch' after colormap in that sequence as posted, the line goes from blue to black (again because it assuming the color range of the entire image instead of just the line it just plotted?). the line is of no value without the colorized topography information . . .
the line that did not work:
plot(x2W,flip(w2), 'linewidth', 2)
colormap(parula)
>patch(x2W,flip(w2),z2,'EdgeColor','interp','FaceColor','none','linewidth',3)
colorbar()
i think i got what i needed by importing the saved versions of the two components as PNG then setting up a transparency mask:
stryng =[FRUMo,'-limb-basemap.png'];
rgbImage = imread(stryng);
stryng =[FRUMo,'-limb-LIN-Kmap.png'];
[pltImage,~,alpha] = imread(stryng);
whiteThreshold = 252;
alphaChannel = ~all(pltImage > whiteThreshold, 3);
imshow(rgbImage);
hold on;
% Overlay the foreground image with transparency
h = imshow(pltImage);
set(h, 'AlphaData', alphaChannel);

Connectez-vous pour commenter.

Image Analyst
Image Analyst le 7 Fév 2026 à 14:41
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

thanks. i think i see a few usefulthings in there to imrove what i got so far

Connectez-vous pour commenter.

Produits

Version

R2016b

Commenté :

le 7 Fév 2026 à 14:52

Community Treasure Hunt

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

Start Hunting!

Translated by