MATLAB Answers

0

colored Line plot according to a third variable

Asked by Michel tawil on 14 Sep 2019
Latest activity Commented on by darova
on 14 Sep 2019
Hello,
I already asked this question before but it is really important to have an answer since the last answer i got wasn't helpful.
i will post the whole code:
"hold on
load(Filename.mat')
low=20;
high=30;
for i=1:235
f=find(vr(:,1)==i); vr indexes are from 1 to 235 but vr is 11000 rows and 0 columns
indexfmax=f(1,1);
indexfmin=f(end,end);
lamda=(vr(f,3)./vr(f,2));% range between 20 and 30
index1=find(lamda>=low & lamda<=high);
maxindex=max(index1);
minindex=min(index1);
x(1,i)=sqrt((vr(f(1,1),8)-vr(f(1,1),6))^2 + (vr(f(1,1),9)-vr(f(1,1),7))^2);%x here is the length for each path
reference(1,i)=sqrt((vr(f(1,1),6))^2 + (vr(f(1,1),7)^2));
if isempty(index1)
averageV(1,i)=NaN;
else
averageV(1,i)=mean(vr(minindex:maxindex,3));
end
end
path1=find(x>=59 & x<=61); path2=find(x>=79 & x<=81);
% For low=20
if low==20
for j=1:47 %path1
if low==20
if rem(j,2)==0
y(1,j:47)=20;
else
y(1,j:47)=21;
end
p1(:,j)=[reference(1,path1(1,j)) y(1,j)];
p2(:,j)=[reference(1,path1(1,j))+x(1,path1(1,j)) y(1,j)];
end
end
averageV=round(averageV);
normalized=averageV/max(averageV);
for j=1:47
if (~isnan(normalized(1,path1(1,j))))
% plot([ p1(1,j) p2(1,j)], [p1(2,j) p2(2,j)])
plot3([ p1(1,j) p2(1,j)], [p1(2,j) p2(2,j)],[normalized(1,path1(1,j)),normalized(1,path1(1,j))],'Linewidth',1);
else
continue;
end
end
colorbar
xlim([0 2000]);
ylim([0 100])"
how can i specify the color of the line plotted by joining the 2 points p1 and p2 according to its corresponding velocity from matrix 'normalized' OR 'averageV'
the averageV could contain velocity as NaN or between 400 and 1200 and after knowing the min and max, the color should be distributed between them (yellow to blue for example) and each velocity corresponds to a color. is that possible ? because it's been 3 months and it didnt work so maybe it's not possible to do that in matlab?

  0 Comments

Sign in to comment.

2 Answers

Answer by darova
on 14 Sep 2019

clc,clear
% generate some data
x = linspace(0,10,10);
y = x.^2;
x = [ x nan ]; % i added nan for 'patch' function
y = [ y nan ];
n = length(x);
vel = 10*rand(1,n); % generate some velocities
img1.png img2.png
Then i was HERE to choose correct colormap. parula looks ok
cm = parula(n);
You can also choose another colormap e.g. jet nad take about 2/3 colors
111Untitled.png
% cm = jet( round(3/2*n) ); % generate more colors
% cm = cm(1:n,:); % take only 2/3
You can interpolate your velocities to indificate correct colors
ind = interp1([min(vel) max(vel)], [1 n], vel); % get indices of colors for each velocity
ind = round(ind); % indices have to be integer
cm = cm(ind,:); % choose colors from colormap
You can plot data using plot(). Just draw each segment seprately and assign appropriate color
hold on
for i = 1:length(x)-1
j = i:i+1;
plot(x(j),y(j),'o-r','color',cm(i,:))
end
hold off
Or you can use patch(). Pay attention that patch has interpolation property
patch(x,y,'', 'EdgeColor','interp', ...
'FaceVertexCdata',cm, ...
'Linewidth', 2);
hold on
plot(x,y,'or')
hold off

  2 Comments

I added these, it didn't work, it gave this error first:
Subscript indices must either be real positive integers or logicals.
Error in trial2 (line 56)
cm=cm(ind,:);
"n=length(p1);
cm=parula(n);
ind=interp1([min(averageV) max(averageV)],[1 n],averageV);
ind=round(ind);
cm=cm(ind,:);
for j=1:47
if (~isnan(normalized(1,path1(1,j))))
plot([ p1(1,j) p2(1,j)], [p1(2,j) p2(2,j)],'o-r','color',cm(j,:));
else
continue;
end
end"
Why?

Sign in to comment.


Answer by Michel tawil on 14 Sep 2019

the ind generated is 235 column and 1 row ( same as averageV) but withing the averageV, some of the velocities are NaN which means that ind not only contains values of the averageV but also NaNs

  3 Comments

Is it possible to solve this?
yes, i tried to remove the nan and i have obtained ind matrix of 27 columns and 1 row. now i guess i need to work with the index j for cm because it goes from 1 to 47 and ind has 27
What should i do?

Sign in to comment.