Inverting curves colors when the distance of their points is 0
3 views (last 30 days)
Show older comments
Hi all,
I have a set of colored curves as follows:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/700187/image.png)
let's take, for the sake of simplicity the top two curves in green and purple. Now, what I would like to do is to invert their colors (green curve should become purple and vice versa) whenever their distance is 0 for more than one point.
I have already created a distance matrix between pairs of curves as follows:
points={};
for j=1:size(y_alg_tr,2)
points{j}=[x',y_alg_tr(:,j)];
end
%v = uint16([1 2 3 2 5 6 7 8 9 10 11 12]);
%C = nchoosek(v,uint16(2));
distance={};
for j=1:N
for k=1:N
distance{j}(:,k) = sqrt(sum((points{j}-points{k}).^2,2));
end
end
threshold = 0.0001;
distance2={};
for j=1:N
distance2{j}=distance{j}<threshold;
end
%eliminating distances with itself
for j=1:N
distance2{j}(:,j)=[];
end
the distance2 cell array is basically a 1 times n_curves array made of n_points_per_curve x n_curves elements either 0 or 1 accoriding to which the point is closer than 0.0001 to the ppoint of another curve. So for instance let's take distance2{3}: it describes the distance of curve 3 to all other elevn curves as follows: it has 186 (n_points) x 12 (n_curves) values of 0 and 1. a 0 in the first column says that the y of curve 3 is not closer than 0.0001 with respect to the y of curve 2, a 1 is put otherwise.
The aim now is to say: when you have more than 2 ones in a column, then invert the colors of the two curves. Following the example, say distance2{3} has 4 ones at positions 123, 124, 125 and 126 at column 4 (which corresponds to curve 5 since we eliminated own distances): so what I would like to do is inverting the colors of the curves from that points on.
Maybe it is just a matter of inverting the ys of the matrix which I woul like to plot but am not sure about it.
Thank you,
Federico
1 Comment
dpb
on 1 Aug 2021
Probably simplest is to just plot a new set of curves of the chosen color where the logical indices are -- each present line is a single line and has only one color property so it isn't possible to change the color of a given line in sections.
Just changing the y's associated with the present lines won't make any difference; the dominant color of the line in the figure is dependent upon the order in which they're drawn.
So, to make the visible section that overlaps the desired color, just go through and plot another set of lines for those elements that are identified...this means using both the x and y for the line indexed by the logical vector to keep them in synch, of course.
If wanted somebody to play with it, attach the data in a .mat file so don't have to try to generate something on own...folks more likely to do so, then, I suspect.
Accepted Answer
dpb
on 1 Aug 2021
Probably simplest is to just plot a new set of curves of the chosen color where the logical indices are -- each present line is a single line and has only one color property so it isn't possible to change the color of a given line in sections.
Just changing the y's associated with the present lines won't make any difference; the dominant color of the line in the figure is dependent upon the order in which they're drawn.(*)
So, to make the visible section that overlaps the desired color, just go through and plot another set of lines for those elements that are identified...this means using both the x and y for the line indexed by the logical vector to keep them in synch, of course.
If wanted somebody to play with it, attach the data in a .mat file so don't have to try to generate something on own...folks more likely to do so, then, I suspect.
(*) Illustration of above point on appearance and order...a trivial example of lines with overlaying areas...
y=1:10; y(1:3)=3; y(7:end)=7; % a 45 line with flats each end
subplot(2,1,1) % first figure
hL=plot([y flip(y)],'r-'); % draw it in red first
ylim([2 12])
hold on % add another to overlay middle
hL(2)=plot(14-[y flip(y)],'b-'); % and it's different color
subplot(2,1,2) % draw same in new figure
hL(3)=plot(14-[y flip(y)],'b-'); % same data, same color but reverse order
hold on
hL(4)=plot([y flip(y)],'r-');
results in
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/700237/image.jpeg)
in which it is apparent the line drawn last on the overlying portion is the one whose color is visible.
0 Comments
More Answers (0)
See Also
Categories
Find more on 2-D and 3-D Plots 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!