How to create 2d plots

4 Ansichten (letzte 30 Tage)
Akarsh Shetty
Akarsh Shetty am 1 Sep. 2019
Bearbeitet: Adam Danz am 4 Sep. 2019
Hello everybody,
I had trouble with regard to animating multiple functions in a 2d plot. Could someone please help me out?
  3 Kommentare
Rik
Rik am 1 Sep. 2019
Have a read here and here. It will greatly improve your chances of getting an answer.
Also, if you want things moving around, it is generally a good idea to update the object properties (check out the XData and YData properties of line objects). Put a pause in between iterations to force a graphics update and slow down the animation speed.
Akarsh Shetty
Akarsh Shetty am 2 Sep. 2019
Bearbeitet: Adam Danz am 2 Sep. 2019
This is the code i used. I wanted to increase the speed of the animated line
t=0:0.1:2*pi; y=sin(t); y2=cos(t);
for k=1:length(t)
%%marker plots
plot(t(k),y(k),'o');
hold on
plot(t(k),y2(k),'o');
hold on
%%line plots
plot(t(1:k),y(1:k));
hold on
plot(t(1:k),y2(1:k));
%%graph property
axis([0 2*pi -1 1]);
xlabel('t');
ylabel('y');
legend('sin(t)','cos(t)');
pause(0.01)
if k ~= length(t)
clf
end
end

Melden Sie sich an, um zu kommentieren.

Antworten (3)

Rik
Rik am 2 Sep. 2019
This code below shows that a change in the algorithm can have more speed increase than tweaking parameters. If you update the properties of the low level graphics components that will always be faster than destroying the window and recreating it every iteration.
%initialize plot
t=0:0.1:2*pi; y=sin(t); y2=cos(t);
h=cell(1);k=1;%store handles in a cell array
h{1}=plot(t(k),y(k),'o');
hold on
h{2}=plot(t(k),y2(k),'o');
h{3}=plot(t(1:k),y(1:k));
h{4}=plot(t(1:k),y2(1:k));
%%graph property
axis([0 2*pi -1 1]);
xlabel('t');
ylabel('y');
legend('sin(t)','cos(t)');
for k=2:length(t)
%update XData and YData properties of the line objets
set(h{1},'XData',t(k),'YData',y(k));
set(h{2},'XData',t(k),'YData',y2(k));
set(h{3},'XData',t(1:k),'YData',y(1:k));
set(h{4},'XData',t(1:k),'YData',y2(1:k));
drawnow%force graphics update
%if you want to slow this down, uncomment the line below
%pause(0.05)
end

Adam Danz
Adam Danz am 2 Sep. 2019
Bearbeitet: Adam Danz am 4 Sep. 2019
There are several major bottlenecks in your code that are slowing down the animation.
  • on each iteration you are destroying the figure and rebuilding it. That's a huge time killer.
  • assigning the axis limits on every iteration is unnecessary
  • assigning the x and y axis labels on each iteration is unnecessary
  • assigning the legend on each iteration is a big time killer
A much quicker solution is to plot empty line objects by using NaN inputs. Then iteratively assign XData and YData values to those line objects.
The plot below is much faster. To slow it down, insert a pause() at the end of the loop.
t=0:0.1:2*pi;
y=sin(t);
y2=cos(t);
% Set up axes
axh = axes();
hold(axh,'on')
axis([0 2*pi -1 1]);
xlabel('t');
ylabel('y');
legend();% Empty for now
% plot empty line objects (with NaN values_
colors = {'r','b'};
line1 = plot(axh,t, nan(size(t)),'-','Color',colors{1},'DisplayName','sin(t)');
line2 = plot(axh,t,nan(size(t)),'-','Color',colors{2},'DisplayName','cos(t)');
marker1 = plot(axh,nan,nan,'o','Color',colors{1},'DisplayName','sin(t)');
marker2 = plot(axh,nan,nan,'o','Color',colors{2},'DisplayName','cos(t)');
% loop through t and make each point appear
for k=1:numel(t)
%marker plots
marker1.XData = t(k);
marker1.YData = y(k);
marker2.XData = t(k);
marker2.YData = y2(k);
%Line plots
line1.YData(1:k) = y(1:k);
line2.YData(1:k) = y2(1:k);
drawnow();
% pause(0.05) %if you want to slow it down
end
  4 Kommentare
Adam Danz
Adam Danz am 3 Sep. 2019
Yours also avoids the redundant legend entries which is nice.
The 0.05 sec pause was in the original answer and it was also commented-out like yours HA!
Out of curiosity I timed 50 iterations of each of our versions and the distribution of tic/toc timing is indistinguishable. I thought maybe the pre-allocated NaN placeholders in the XData/YData in my version may have resulted in slightly faster execution but apparently not.
Rik
Rik am 3 Sep. 2019
It is probably offset by your codee requiring an extra call modification of the object. The difference are probably so small that tic/toc has difficulty getting an accurate measure. Maybe this is a situation where cputime has a benefit?

Melden Sie sich an, um zu kommentieren.


KALYAN ACHARJYA
KALYAN ACHARJYA am 2 Sep. 2019
Bearbeitet: KALYAN ACHARJYA am 2 Sep. 2019
This is the code i used. I wanted to increase the speed of the animated line
pause(0.0001); % decrease this one as per your requirements

Kategorien

Mehr zu Graphics Performance finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by