Scatter Plots and Lines

조회 수: 2 (최근 30일)
Cody deSpain
Cody deSpain 2015년 7월 3일
답변: Walter Roberson 2015년 7월 3일
I have a scatter plot with approximately 500 points with each point being labeled either 1,2,4,8,7,or 5. I'm looking for a way to draw lines CONNECTING each 1 to each 2, each 2 to each 4, etc. Any help would be greatly appreciated.
Thanks!
  댓글 수: 2
Walter Roberson
Walter Roberson 2015년 7월 3일
So each of the 125 or so "1" would be connected to each of the 125 or so "2", which would involve about 7850 line segments?
Or each "1" should be connected to the "nearest" 2?
Or the first "1" should be connected to the "first" 2, the second "1" to the second "2" according to the ordered list of coordinates?
Cody deSpain
Cody deSpain 2015년 7월 3일
yes the first question you asked is correct. I'm wanting to connect every single 1 to every 2, and so on. Is that possible? Thanks

댓글을 달려면 로그인하십시오.

답변 (1개)

Walter Roberson
Walter Roberson 2015년 7월 3일
connectorder = [1 2 4 8 7 5];
linecolors = {'k', 'r', 'b', 'g', 'y'}; %you can use an RGB triple too
X = ... %as appropriate
Y = ... %as appropriate
classcode = ... %the list of 1, 2, 4 etc values, one per entry
ucc = unique(classcode);
for K = 1 : length(connectorder) - 1
fromcode = connectorder(K);
tocode = connectorder(K+1);
from_class = classcode == fromcode;
to_class = classcode == tocode;
from_X = X(from_class);
from_Y = Y(from_class);
to_X = X(to_class);
to_Y = Y(to_class);
[srcX, dstX] = ndgrid(from_X, to_X);
[srcY, dstY] = ndgrid(from_Y, to_Y);
X_pair = [srcX(:).'; dstX(:).'; nan(1,numel(srcX))];
Y_pair = [srcY(:).'; dstY(:).'; nan(1,numel(srcX))];
plotlines(K) = plot(X_pair(:), Y_pair(:), 'LineStyle', '-', 'Color', linecolors{K});
end
I have trouble imagining the result will be readable, but it is what you asked for.
ndgrid() can be used to create meshes, but it is also useful for generating all combinations of pairs.
The "trick" that is used here is that if you have a NaN in a stream of X or Y values that the drawing of the segment will have a break there, giving the look of independent line segments without requiring a lineseries object be created for each segment. The single combined list is, though, restricted to all being the same color. I made provision here for configuring the color of each grouping.
This code does not join the 5's to the 1's. Your "etc." was not specific about that.

카테고리

Help CenterFile Exchange에서 Line Plots에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by