How to label nodes in a plot?

7 views (last 30 days)
Haya Ali
Haya Ali on 26 May 2023
Commented: Steven Lord on 27 May 2023
I want to add labels to nodes " D1, D2,....,D23" in my plot and change the color of lines to magenta. Please help.
close all; clear all; clc;
theta=linspace(0,2*pi,24);theta=theta(1:end-1);
[x,y]=pol2cart(theta,1);
links= [-1 -1 0 0 0 0 0 1 0 1 0 0 -1 1 1 -1 1 0 -1 1 -1 1 0;
1 0 0 0 -1 0 0 1 0 1 -1 -1 -1 0 0 -1 -1 -1 0 0 -1 1 0;
1 0 1 0 1 0 0 1 0 0 1 1 -1 1 0 -1 0 0 0 0 -1 -1 0;
1 -1 0 0 -1 0 -1 -1 0 0 -1 1 0 0 0 -1 1 -1 0 -1 -1 0 1;
0 1 -1 0 0 0 -1 1 -1 -1 -1 1 0 0 0 0 -1 0 0 -1 1 0 0;
1 0 0 1 0 0 -1 -1 0 0 0 1 -1 0 0 -1 1 -1 0 -1 -1 1 1;
-1 0 -1 0 1 0 0 1 0 1 0 -1 -1 1 1 0 0 0 0 1 1 0 0;
0 0 -1 0 0 0 1 0 0 1 0 0 1 -1 0 1 0 -1 1 1 -1 1 -1;
0 0 -1 -1 0 0 0 -1 0 -1 0 1 1 0 1 -1 1 -1 -1 -1 -1 0 0;
0 -1 -1 -1 1 0 0 -1 0 0 0 0 1 1 0 -1 1 0 0 -1 -1 0 1;
0 0 1 0 -1 0 -1 0 0 1 -1 -1 0 0 -1 0 -1 0 -1 1 1 1 1;
-1 0 0 1 1 0 -1 -1 0 -1 -1 1 -1 0 -1 0 0 1 0 1 1 0 1;
0 -1 -1 0 1 0 -1 -1 0 -1 0 0 -1 0 0 -1 1 0 0 1 0 0 -1;
0 -1 0 -1 -1 0 0 -1 0 0 0 -1 1 1 0 0 1 0 0 -1 -1 0 0;
-1 0 -1 -1 1 0 0 1 0 1 0 -1 1 1 1 -1 0 0 -1 0 0 -1 -1;
-1 0 0 1 0 -1 -1 -1 0 -1 -1 1 -1 0 0 0 0 0 -1 1 1 1 1;
0 -1 -1 0 1 0 -1 -1 0 -1 0 0 1 0 0 1 0 0 0 1 1 1 -1;
1 1 -1 0 -1 0 -1 1 0 1 -1 -1 -1 0 1 0 -1 -1 0 0 1 0 -1;
0 -1 1 1 0 0 -1 -1 1 1 0 -1 -1 1 0 -1 1 0 -1 0 0 1 0;
0 -1 -1 0 1 0 0 1 0 1 0 0 -1 1 0 1 1 0 -1 0 -1 0 -1;
1 1 -1 1 -1 0 0 0 -1 1 0 0 -1 0 -1 -1 -1 0 1 1 1 1 0];
[ind1,ind2]=ind2sub(size(links),find(links(:)));
h=figure(1);clf(h);
G = plot(x,y,'.k','markersize',20);
hold on
arrayfun(@(p,q)line([x(p),x(q)],[y(p),y(q)]),ind1,ind2);
axis equal off

Accepted Answer

Steven Lord
Steven Lord on 26 May 2023
This matrix looks similar to the one from one of your previous questions. If that's the case, and you plotted the graph object, you could call the labelnode function to label the plot of the graph object and change its properties to change the edge colors or other properties.
g = graph(bucky);
figure
h = plot(g);
nodesToLabel = 3:3:numnodes(g);
labelnode(h, nodesToLabel, "Node " + nodesToLabel)
figure
h = plot(g, 'EdgeColor', 'r');
figure
h = plot(g, 'EdgeColor', 'r');
highlight(h, 5, neighbors(g, 5), 'LineWidth', h.LineWidth*10) % Make edges from 5 ten times thicker
  1 Comment
Steven Lord
Steven Lord on 27 May 2023
The bucky graph was just a simple example. Create your own graph and use the functions I did on that graph.

Sign in to comment.

More Answers (0)

Categories

Find more on Graph and Network Algorithms 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!