Change colour of line graph once a value is reached

2 次查看(过去 30 天)
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.

采纳的回答

Chunru
Chunru 2021-6-19
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.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Animation 的更多信息

产品


版本

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by