Zeyad Ibrahim
Zeyad Ibrahim

Reputation: 13

Matlab: plotting animation with fixed axes

When I call this function, the axes are moving with the plot. How can I stop this from happening? I tried putting xlim and ylim before the function in the command window but that didn't work.

My code is:

function h = plootwithanimation(x,y) 

    for  h = 1:length(x)
        plot(x(h),y(h),'*')
        pause(1)
        hold on
    end

Upvotes: 0

Views: 312

Answers (2)

Wolfie
Wolfie

Reputation: 30046

You can fix the bounds by using xlim and ylim as you tried, but plotting will ignore whatever you set the axes to before calling plot.

You should instead use them after the plot

function h = plotwithanimation(x, y, xlims, ylims) 
% Also pass in axis limits 
%   xlims = [x0,x1] 
%   ylims = [y0,y1]

hold on; % You only have to hold on once around plotting
for  h = 1:length(x)
    plot(x(h),y(h),'*');
    xlim(xlims);
    ylim(ylims);
    pause(1);
end
hold off; % Good habit so you don't accidentally plot over this figure later

Upvotes: 0

qbzenker
qbzenker

Reputation: 4652

Try fixing the bounds using the axis function:

function h = plootwithanimation(x,y) 


for  h = 1:length(x)
     plot(x(h),y(h),'*')
     axis([0 10 -2 100]) %or whatever you want. This sets 0<x<10 and -2<y<100
     pause(1)
     hold on

end

Upvotes: 1

Related Questions