1

Here are the coordinates that I am planning to plot, filename is 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

Here is my code that plots the scatter graph first:

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;

Here is the result I get:

scatter figure

I don't know why the lines are being drawn everywhere. I also notice that even when plot(x,y) = 0, the line is still being drawn.

I also would like to change the thickness and opacity of the line depending on the distance between the two points: E.g. thicker and darker line for short distance between points. And thinner /lighter line if the distance between two points are long.

I want my plot to look something like this:

Ideal result

rayryeng
  • 102,964
  • 22
  • 184
  • 193
tamkrit
  • 27
  • 5

2 Answers2

3

The reason your lines do not match the scattered points is the coordinates you give to plot; The coordinates are in wrong order and therefore they do not define the lines correctly.

I modified your code to correct this issue. I replaced plot with line, but you can also do the same with plot. In addition, I defined the anonymous functions f and g to define the color and thickness of each line based on distance of the two ends, d. You can modify these functionalities to get different graphical behaviors.

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

With this output:

enter image description here

Notes:

  1. axis tight is a command that sets the limits to the tightest possible. It is equivalent to your xlim( [ min( Coords(:,1)) max( Coords(:,1)) ] ); and the next line.
  2. In the for-loops you should try to avoid choosing one pair of points twice or same point as both sides of a line.
  3. For scattering you do not need a loop. It could all be done at once.
  4. I brought scatter after plotting the lines, so the circles are drawn on top.
Erfan
  • 1,897
  • 13
  • 23
2

There is also a specialized MATLAB function for generating plots like this: 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

enter image description here

Then, customize to your liking, e.g. if you want to change marker type, remove the axis, add labels etc.

vkehayas
  • 278
  • 3
  • 14