绘制所有散点之间的线,并根据点之间的距离调整线的粗细

Plot line between all the scattered points and adjust the thickness of line according to the distance between points

这是我计划绘制的坐标,文件名为 Coords:

x       y
0.0110  0.1105
-0.2730 0.2559
0.3610  0.1528
-0.0077 -0.2520
-0.2412 -0.1979
0.0444  -0.0526
0.0543  -0.0076
-0.1710 0.1170
0.12741 -0.0448
0.0949  -0.0811

这是我首先绘制散点图的代码:

Hold on
%Plot Coordinate
For i=1:10
    dot_size = 100;
    scatter ( Coords(i,1) ,Coords(i,2), dot_size, 'filled', 'MarkerEdgeColor', 'k' );
end

%Draw line distance between each points
for i=1:10
     for j=1:10
          plot( [Coords(i,1) Coords(i,2)], [Coords(j,1) Coords(j,2)] );
     end
end
Hold off

%Sets the size of the y and x axis
xlim( [ min( Coords(:,1)) max( Coords(:,1)) ] );
ylim( [ min( Coords(:,2)) max( Coords(:,2)) ] );
axis off;

这是我得到的结果:

我不知道为什么到处都是划线。我还注意到,即使在 plot(x,y) = 0 时,线条仍在绘制中。

我还想根据两点之间的距离改变线条的粗细和不透明度:例如较粗和较暗的线表示点之间的距离较短。如果两点之间的距离很长,则线更细/更轻。

我希望我的情节看起来像这样:

你的线与散点不匹配的原因是你给的坐标 plot;坐标顺序错误,因此它们没有正确定义线。

我修改了您的代码以更正此问题。我用 line 替换了 plot,但你也可以用 plot 做同样的事情。另外,我定义了anonymous functionsfg来根据两端的距离定义每条线的颜色和粗细,d。您可以修改这些功能以获得不同的图形行为。

n = 10; % number of points
dot_size = 100;
Coords = rand(n, 2);
% maximum possible length in your coordination plane:
L = norm([max(Coords(:,1))-min(Coords(:,1)),max(Coords(:,2))-min(Coords(:,2))]);
% this function defines the line width:
f = @(x) L / (x + 0.1); % 0.1 avoids huge line widths in very small distances
% this function defines the color:
g = @(x) x * [1 1 1] / L;
figure
hold on
for ii = 1:n-1
     for jj = ii+1:n
         d = norm([Coords(ii,1)-Coords(jj,1), Coords(ii,2)-Coords(jj,2)]);
         line([Coords(ii,1) Coords(jj,1)], [Coords(ii,2) Coords(jj,2)], ...
             'LineWidth', f(d), 'Color', g(d));
     end
end
scatter (Coords(:,1), Coords(:,2), dot_size, 'filled', 'MarkerEdgeColor', 'k');
axis tight
axis off

有了这个输出:

备注:

  1. axis tight 是一个将限制设置为最严格的命令。相当于你的xlim( [ min( Coords(:,1)) max( Coords(:,1)) ] );和下一行
  2. for循环中,你应该尽量避免选择一对点两次或同一点作为一条线的两边。
  3. 对于散射,您不需要循环。这一切都可以一次完成。
  4. 画线后我带了scatter,所以圆在上面画了。

还有一个专门的 MATLAB 函数可以生成这样的图:gplot

  data = [
    0.0110  0.1105
    -0.2730 0.2559
    0.3610  0.1528
    -0.0077 -0.2520
    -0.2412 -0.1979
    0.0444  -0.0526
    0.0543  -0.0076
    -0.1710 0.1170
    0.12741 -0.0448
    0.0949  -0.0811]; % Coordinates

    adjM = squareform(pdist(data)); % 
    adjM (adjM > 0) = 1; % Form adjacency matrix based on Euclidean distances


figure; gplot(adjM, data, '-o') % Plot figure based on coordinates and adjacency matrix

然后,根据您的喜好进行自定义,例如如果你想改变标记类型,删除轴,添加标签等。