Change colour of line graph once a value is reached

2 vues (au cours des 30 derniers jours)
Dussan Radonich
Dussan Radonich le 19 Juin 2021
Commenté : Dussan Radonich le 19 Juin 2021
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.

Réponse acceptée

Chunru
Chunru le 19 Juin 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.
  1 commentaire
Dussan Radonich
Dussan Radonich le 19 Juin 2021
As simple as that, amazing. Thank you

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Animation dans Help Center et File Exchange

Produits


Version

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by