How to create 2d plots

Hello everybody,
I had trouble with regard to animating multiple functions in a 2d plot. Could someone please help me out?

3 comentarios

Adam Danz
Adam Danz el 1 de Sept. de 2019
You'll need to describe the trouble you're having, what you've tried, why it's not working, etc. Providing small sections of relevant code is also helpful.
Rik
Rik el 1 de Sept. de 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 el 2 de Sept. de 2019
Editada: Adam Danz el 2 de Sept. de 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

Iniciar sesión para comentar.

Respuestas (3)

Rik
Rik el 2 de Sept. de 2019

2 votos

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 el 2 de Sept. de 2019
Editada: Adam Danz el 4 de Sept. de 2019

2 votos

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 comentarios

Adam Danz
Adam Danz el 2 de Sept. de 2019
Note, I just saw Rik's answer after posting this one (even though he apparently posted his 18 min prior). It's the same approach (which tells you it's a good one since Rik definitely knows what he's doing).
Rik
Rik el 2 de Sept. de 2019
Editada: Rik el 2 de Sept. de 2019
Great minds think alike ;) have a +1 from me. And double posts happen all the time. I often delete my answers if there is a better one, so you only see my good ones. (don't delete yours, it's similar but shows a slightly different approach)
I think it is funny to see the subtle differences. You initialize the objects outside of the loop with NaN and use object notation, while I just hijack the first iteration and use the HG1-compatible notation.
Did you put the same pause interval as me in your original post? It is a good interval to slow the thing down without having to wait too much.
Adam Danz
Adam Danz el 3 de Sept. de 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 el 3 de Sept. de 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?

Iniciar sesión para comentar.

KALYAN ACHARJYA
KALYAN ACHARJYA el 2 de Sept. de 2019
Editada: KALYAN ACHARJYA el 2 de Sept. de 2019

0 votos

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

Categorías

Más información sobre Graphics Performance en Centro de ayuda y File Exchange.

Preguntada:

el 1 de Sept. de 2019

Editada:

el 4 de Sept. de 2019

Community Treasure Hunt

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

Start Hunting!

Translated by