Highlighting edges of a graph

How do I highlight specific edges of a graph? Let's say for the figure given below, I need to highlight the edges 9-16 and 8-15. Please help.

Réponses (3)

Luciano Garim
Luciano Garim le 7 Oct 2020

0 votes

Hi Hari!
I had the same problem. I found the solution here:
https://www.mathworks.com/help/matlab/ref/matlab.graphics.chart.primitive.graphplot.highlight.html
Try these examples above. If you have any difficulty let me know.
I hope helped you!

1 commentaire

Hari
Hari le 7 Oct 2020
Hi, I have read the documentation. Couldnt figure out how should I give the edges to be highlighted as input.

Connectez-vous pour commenter.

Ameer Hamza
Ameer Hamza le 7 Oct 2020
Modifié(e) : Ameer Hamza le 7 Oct 2020

0 votes

You can give them a different color and sizes. Here is a simple example
n = 20;
G = graph(1:n, [2:n 1]);
plot(G)
nodes = [5 7 9 11]; % nodes to highlight
colors = [0 0 0; % color for normal nodes
1 0 0]; % color for highlighted nodes
ax = axes();
colormap(colors);
gp = plot(G);
gp.NodeCData = zeros(1, n);
gp.NodeCData(nodes) = 1;
gp.MarkerSize = gp.MarkerSize*ones(1, n);
gp.MarkerSize(nodes) = 10;

4 commentaires

Hari
Hari le 7 Oct 2020
This works for nodes. I would like to highlight the links (edges)
Same method apply for edges too
n = 20;
G = graph(1:n, [2:n 1]);
m = size(G.Edges, 1);
nodes = [5 7 9 11]; % nodes to highlight
edges = [1 6 9 14];
colors = [0 0 0; % color for normal nodes
1 0 0]; % color for highlighted nodes
ax = axes();
colormap(colors);
gp = plot(G);
gp.NodeCData = zeros(1, n);
gp.NodeCData(nodes) = 1;
gp.MarkerSize = gp.MarkerSize*ones(1, n);
gp.MarkerSize(nodes) = 10;
gp.EdgeCData = zeros(1, m);
gp.EdgeCData(edges) = 1;
gp.LineWidth = 1*ones(1, m);
gp.LineWidth(edges) = 3;
Hari
Hari le 7 Oct 2020
How did you refer to the edges in this case by giving [1 6 9 14]? Does it select the link having these nodes?
Sorry if this queston is stupid. I'm just a novice programmer.
No, MATLAB's graph() uses its own internal numbering for edges. If you want to specify edges using the nodes, then try the following code. It specifies nodes at two ends of the edge you want to highlight.
n = 20;
G = graph(1:n, [2:n 1]);
m = size(G.Edges, 1);
nodes = [5 7 9 11]; % nodes to highlight
edges_source = [1; 4; 7; 10];
edges_destination = [2; 5; 8; 11]; % an edge must exist between corresponding nodes of two vectors
edges = G.findedge(edges_source, edges_destination);
colors = [0 0 0; % color for normal nodes
1 0 0]; % color for highlighted nodes
ax = axes();
colormap(colors);
gp = plot(G);
gp.NodeCData = zeros(1, n);
gp.NodeCData(nodes) = 1;
gp.MarkerSize = gp.MarkerSize*ones(1, n);
gp.MarkerSize(nodes) = 10;
gp.EdgeCData = zeros(1, m);
gp.EdgeCData(edges) = 1;
gp.LineWidth = 1*ones(1, m);
gp.LineWidth(edges) = 3;

Connectez-vous pour commenter.

Steven Lord
Steven Lord le 7 Oct 2020

0 votes

The easiest way for this example is to specify the source and target nodes. Let's plot the bucky graph:
G = graph(bucky);
h = plot(G);
To highlight edge (9, 10) and (29, 43) in red, we specify the source nodes [9 29] and the corresponding target nodes [10 43] as the second and third inputs to highlight. We then tell highlight we want to change the EdgeColor property of those edges to red.
highlight(h, [9 29], [10 43], 'EdgeColor', 'r')

Catégories

En savoir plus sur Graph and Network Algorithms dans Centre d'aide et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by