How to normalize an image position in a subplot?

5 Ansichten (letzte 30 Tage)
Darcy Cordell
Darcy Cordell am 17 Mai 2024
Beantwortet: DGM am 18 Mai 2024
I have some data which I then overlay a figure on top of like this:
plot(rand(10),rand(10),'*')
[img,~,alphachannel] = imread('ImageFile.png');
axes('units','normalized', 'Position',[0.35 0.23 0.35 0.35]);
image(img,'AlphaData',alphachannel);
axis off
The position of the image on the figure is normalized, so even if the data I plot change scale, the image stays in the same position on the figure.
Now, this all works beautifully.
All I want to do is make this plot into a subplot instead of a standalone figure.
However, I can't seem to figure out how to do this because the image position always gets referenced to the whole figure rather than the subplot.
Any solutions?

Antworten (1)

DGM
DGM am 18 Mai 2024
With the exception of some particular cases (which shouldn't be relevant here), you shouldn't need to do any of that stuff with overlaid axes. Just specify the xdata and ydata parameters in the call to image()/imagesc()/imshow().
% the image
[img,~,alphachannel] = imread('peppers_rgba.png');
img = flipud(img); % might need to either flip the image or the ydir property
% specify some things
xrange = [0 1];
yrange = [0 1];
% plot the semitransparent image _atop_ the plot
plot(rand(10),rand(10),'*'); hold on;
image(xrange,yrange,img,'AlphaData',alphachannel);
xlim(xrange)
ylim(yrange)
I don't know what your image looks like or your actual goals, but it may make more sense to flip the stack order (plot atop the image), but that's for you to decide.

Community Treasure Hunt

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

Start Hunting!

Translated by