Highlighting edges of a graph

조회 수: 41(최근 30일)
Hari
Hari 2020년 10월 7일
답변: Steven Lord 2020년 10월 7일
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.

답변(3개)

Luciano Garim
Luciano Garim 2020년 10월 7일
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
Hari
Hari 2020년 10월 7일
Hi, I have read the documentation. Couldnt figure out how should I give the edges to be highlighted as input.

댓글을 달려면 로그인하십시오.


Ameer Hamza
Ameer Hamza 2020년 10월 7일
편집: Ameer Hamza 2020년 10월 7일
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
Ameer Hamza
Ameer Hamza 2020년 10월 7일
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
Steven Lord 2020년 10월 7일
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')

Community Treasure Hunt

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

Start Hunting!

Translated by