Highlighting edges of a graph

18 ビュー (過去 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 件のコメント
Hari
Hari 2020 年 10 月 7 日
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.
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')

カテゴリ

Help Center および File ExchangeDirected Graphs についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by