MATLAB Answers

How can I change the line color in an 'animatedline' plot in a manner that scales with one of the variables?

50 views (last 30 days)
Steven Markus
Steven Markus on 24 Jan 2021
Commented: Adam Danz on 25 Jan 2021
I'm using the code below to animate a line for the purpose of a presentation. It seems that the 'animatedline' function doesn't support the option of changing color over the course of the plot (e.g., in a manner that scales with X, Y or Z). It seems that the 'patch' function supports this, and I'm wondering if there's a way to do something similar with 'animatedline'? Any help from the Matlab experts?
%X, Y and Z were emperically determined from a separate analysis, and are pre-assigned
view([-12 34])
curve = animatedline('Color','r','LineWidth',3);
numpoints = size(x)
ax = gca;
axTickDir = 'out'
ax.FontSize = 11;
ax.XLim = [0 11];
ax.YLim = [1 5];
ax.ZLim = [1.5 3]
for k=1:numpoints
addpoints(curve,x(k),y(k),z(k));
pause(0.05)
M(k) = getframe(gcf);
end
drawnow

Accepted Answer

Adam Danz
Adam Danz on 24 Jan 2021
There are other ways to create animation.
You can segement the line and conrol the color of each segment (see "segmentation demo" in this answer).
To animate it, plot it within a loop that iteratively adds each segment.
hold on
for i = 1:number_of_segments
plot(x_segment,y_segment,'-','color', RGB_segment)
drawnow()
pause(.1) % to slow it down, if needed
end
If you get stuck, show what you tried and I'd be glad to work out the kinks.
Alternatively, you could use some undocumented approaches to changing line color of a line object. See Yair's blog post below. https://undocumentedmatlab.com/matlab/wp-content/cache/all/articles/plot-line-transparency-and-color-gradient/index.html
  4 Comments
Adam Danz
Adam Danz on 25 Jan 2021
Happy to hear that it works for you.
Please accept answers that were helpful by pressing the blue 'accpet this answer' button.
You could do either of our options. Animation in 3D requires a very easy change to the existing code. Use plot3() instead of plot() and include the z-coordinate.
To use the z-value to define color, you need to make a colormap and use the z values to select the color. That will requiring mapping the z values on to the row indicies of a colormap.
For example,
cmap = jet(256); % number of rows doesnt' matter
% assuming z is a vector
zScaled = round((z-min(z))/range(z)*(size(cmap,1)-1))+1;
% zScaled are integers that span from 1 to the number of colors
% in your colormpa (cmap). They can be used to define the color
% of each segment.
% Example:
for i = 1:n
plot(x, y, '-','color', cmap(i,:))
end

Sign in to comment.

More Answers (1)

VBBV
VBBV on 24 Jan 2021
Edited: VBBV on 24 Jan 2021
Put drawnow inside the loop
  4 Comments
Adam Danz
Adam Danz on 24 Jan 2021
@Steven Markus, drawnow belongs within the loop so that the plot is updated after each iteration. You'll also notice this in the Matlab documentation for animatedline.
I don't think VBBV's advice regarding setting colors is correct or complete.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!