Highlighting edges of a graph
13 views (last 30 days)
Show older comments
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.
0 Comments
Answers (3)
Luciano Garim
on 7 Oct 2020
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!
Ameer Hamza
on 7 Oct 2020
Edited: Ameer Hamza
on 7 Oct 2020
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 Comments
Ameer Hamza
on 7 Oct 2020
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;
Steven Lord
on 7 Oct 2020
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')
0 Comments
See Also
Categories
Find more on 2-D and 3-D Plots 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!