Change colour of line graph once a value is reached

3 views (last 30 days)
Hello guys,
What I am trying to do is plot a line graph but when a certain value is reached I want the line to change colour just for the segment after the point was reached. The only way I found to do it is with an animation, the problem is that it is really slow and I don't care for the animation, I just want the graph to show the change in colour.
close all
clear
clc
format long g
figure('Name', 'Relative RMSD of alpha and beta structures');
clf();
hold on;
ylim([0 1]);
a = load(['12to17_alpha4.dat']);
b = load(['12to17_beta4.dat']);
title('\fontsize{16}Relative RMSD');
rel_rmsd = a./(a+b);
xseg = [a(1:end-1,1),a(2:end,1)];
yseg = [rel_rmsd(1:end-1,2),rel_rmsd(2:end,2)];
c='black';
for i=1:(length(rel_rmsd)-1)
h = plot(xseg(i,:),yseg(i,:),'-','LineWidth',1, 'color',c);
if(rel_rmsd(i,2)<0.25)
c='red';
end
if(rel_rmsd(i,2)>0.75)
c='blue';
end
drawnow;
end
This results in what I want, but like I said it is extremely slow. Especially if I have more datasets, and wanted to know if anyone knows what else I could do.
Thank you.

Accepted Answer

Chunru
Chunru on 19 Jun 2021
The program is slow because it draws and updates the line segment (of two points) by segment .
To speed up in the simplest way, remove "drawnow" so that you only see the last plot, which is what you want.
If you need to further speed up the program, consider to find begining and ending of each segment (many points) of color changing and then use the plot command to plot each multipoints segments.

More Answers (0)

Categories

Find more on Animation in Help Center and File Exchange

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!