Highlighting edges of a graph

11 次查看(过去 30 天)
Hari
Hari 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 CenterFile Exchange 中查找有关 2-D and 3-D Plots 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by