How to label nodes in a plot?

5 visualizzazioni (ultimi 30 giorni)
Haya Ali
Haya Ali il 26 Mag 2023
Commentato: Steven Lord il 27 Mag 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

Risposta accettata

Steven Lord
Steven Lord il 26 Mag 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 Commento
Steven Lord
Steven Lord il 27 Mag 2023
The bucky graph was just a simple example. Create your own graph and use the functions I did on that graph.

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Graph and Network Algorithms in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by