colored Line plot according to a third variable
47 views (last 30 days)
Show older comments
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
Answers (2)
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
cm = parula(n);
You can also choose another colormap e.g. jet nad take about 2/3 colors
% 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
See Also
Categories
Find more on Colormaps in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!