How can I display an image on a point of the plot?

23 次查看(过去 30 天)
I have an animated plot obtained using a for cycle and drawnow. I'de like to display a small image on the last displayed point of the plot at every interation. The strange thing about this problem is that I managed to get it work in one function but it doesn't work in two other functions I made. The code that works is the following:
function [] = GroundTrackGFX(latitude, glongitude, date, Nt, inc, omegarot,...
dt, lplon, lplat)
gtf = figure('position', [25 200 800 500], 'NumberTitle', 'off',...
'Name', 'Ground track');
M = imread('Earth texture.png');
S = imread('Satellite icon.png');
imagesc('XData', [-180 180],'YData', [90 -90],'CData', M);
daspect([1 1 1]); hold on
xlabel('Longitude [deg]'); ylabel('Latitude [deg]');
axis([-180 180 -90 90]);
ax = gca; ax.TickDir = 'out';
plot(glongitude(2), latitude(2), 'g.', 'markersize', 16); hold on
title(date(2));
I = imagesc('XData', [glongitude(2)-3 glongitude(2)+3], 'YData',...
[latitude(2)-3 latitude(2)+3], 'CData', S);
for i = 3 : Nt
title(date(i));
delete(I);
I = imagesc('XData', [glongitude(i)-3 glongitude(i)+3],...
'YData', [latitude(i) - 3 latitude(i) + 3], 'CData', S);
% other stuff (plots)
drawnow
end
% more stuff
end
One that doesn't work (the animated plot works fine but S is not displayed) is this:
function [] = MGroundTrackGFX(xm, ym, RT, date, Nt, glongitude, latitude)
gtf = figure('position', [25 200 800 500], 'NumberTitle', 'off',...
'Name', 'Ground track');
M = imread('mollweide.png');
S = imread('Satellite icon.png');
plot(xm(2), ym(2), 'g.', 'markersize', 16); hold on
title(date(2));
I = imagesc('XData', [xm(2)-3 xm(2)+3], 'YData',...
[ym(2)-3 ym(2)+3], 'CData', S);
for i = 3 : Nt
title(date(i));
delete(I);
I = imagesc('XData', [xm(i)-3 xm(i)+3], 'YData',...
[ym(i)-3 ym(i)+3], 'CData', S);
% other stuff (plots)
drawnow
end
% more stuff
end
I really can't understand why the first works and not the second... To me they're pretty much identical. Can anyone tell me why this is the case and how I can do to solve the problem?

采纳的回答

Rik
Rik 2019-1-15
Instead of deleting and recreating those graphics every iteration, you could also update the XData and YData properties of the image object that is returned by imagesc. That should solve your problem, as well as speeding up your code a bit.
  3 个评论
Rik
Rik 2019-1-16
Here is a MWE:
figure(1),clf(1)
ax=image;%trigger default image
axis([0 100 0 100])
for n=1:5
Xoffset=randi(36);
Yoffset=randi(36);
ax.XData=[1 64]+Xoffset;
ax.YData=[1 64]+Yoffset;
drawnow
pause(0.5)
end

请先登录,再进行评论。

更多回答(1 个)

Image Analyst
Image Analyst 2019-1-16
Is this kind of what you're looking for? To display an image on a graph?
% Draw a small image inset in the upper right corner of a larger plot.
% Ref: https://www.mathworks.com/matlabcentral/answers/60376-how-to-make-an-inset-of-matlab-figure-inside-the-figure#comment_654093
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 18;
x = linspace(0, 1);
y1 = sin(2*pi*x);
figure(1)
% plot on large axes
plot(x, y1, 'LineWidth', 2)
grid on;
ax1 = gca; % Store handle to axes 1.
% Create smaller axes in top right, and plot on it
% Store handle to axes 2 in ax2.
ax2 = axes('Position',[.6 .6 .3 .3])
box on;
fileName = 'peppers.png';
rgbImage = imread(fileName);
imshow(rgbImage);
axis('on', 'image');
% Now draw something back on axis 1
hold(ax1, 'on'); % Don't blow away existing curve.
y2 = cos(2*pi*x/0.7);
plot(ax1, x, y2, 'r-', 'LineWidth', 2);
0000 Screenshot.png
Adapt as needed.

类别

Help CenterFile Exchange 中查找有关 Graphics Performance 的更多信息

产品


版本

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by