Scatter Plots and Lines
2 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
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 comentarios
Walter Roberson
el 3 de Jul. de 2015
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?
Respuestas (1)
Walter Roberson
el 3 de Jul. de 2015
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.
0 comentarios
Ver también
Categorías
Más información sobre Line Plots en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!