필터 지우기
필터 지우기

How to normalize an image position in a subplot?

조회 수: 7 (최근 30일)
Darcy Cordell
Darcy Cordell 2024년 5월 17일
답변: DGM 2024년 5월 18일
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?

답변 (1개)

DGM
DGM 2024년 5월 18일
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.

카테고리

Help CenterFile Exchange에서 Histograms에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by