36 views (last 30 days)

Hello Friends!

I have created a simulation which generates plots using the animated line function. I already have two lines in one plot, but the problem is, the other

is not really visible because the values are too small compared to the other. Is it possible to create a second animated line in the same plot, but values of the

second line refer to a second y-Axis on the right side of the plot? So then the second line would be clearly visible.

Thanks a lot in advance!

Adam Danz
on 15 Oct 2019

Edited: Adam Danz
on 28 Jan 2020

Add the animatedline object after you define the 2nd axis with yyaxis. Then you can set the color of the animated object to the axis color.

Here's a functional demo that animates a line on both the left and right axes

% Create the data

x = linspace(0,10);

y = sin(3*x);

z = sin(3*x).*exp(0.5*x);

% Set up the left axis

yyaxis left

axl = gca(); % handle to left axis

h1 = animatedline('color',axl.YAxis(1).Color); % after yyaxis left; set to axis color.

ylim([-1 1]) % set limits before animation

xlim([1 10]) % set limits before animation

% set up the right axis

yyaxis right

axr = gca(); % handle to right axis

h2 = animatedline('color',axr.YAxis(2).Color); % after yyaxis right; set to axis color.

ylim([-150 150]) % set limits before animation

% Animate

for i = 1:numel(x)

addpoints(h1,x(i),y(i));

addpoints(h2,x(i),z(i));

drawnow

end

Sign in to comment.

Sign in to answer this question.

Opportunities for recent engineering grads.

Apply Today
## 3 Comments

## Direct link to this comment

https://ww2.mathworks.cn/matlabcentral/answers/484782-how-to-plot-second-graph-with-second-axis-with-animated-line#comment_755287

⋮## Direct link to this comment

https://ww2.mathworks.cn/matlabcentral/answers/484782-how-to-plot-second-graph-with-second-axis-with-animated-line#comment_755287

## Direct link to this comment

https://ww2.mathworks.cn/matlabcentral/answers/484782-how-to-plot-second-graph-with-second-axis-with-animated-line#comment_755419

⋮## Direct link to this comment

https://ww2.mathworks.cn/matlabcentral/answers/484782-how-to-plot-second-graph-with-second-axis-with-animated-line#comment_755419

## Direct link to this comment

https://ww2.mathworks.cn/matlabcentral/answers/484782-how-to-plot-second-graph-with-second-axis-with-animated-line#comment_756679

⋮## Direct link to this comment

https://ww2.mathworks.cn/matlabcentral/answers/484782-how-to-plot-second-graph-with-second-axis-with-animated-line#comment_756679

Sign in to comment.