Why does my EdgeColor map incorrectly to the edges of my Graph Plot?

3 visualizaciones (últimos 30 días)
I've generated a Graph Plot and would like to color specific edges that connect to a specific node. I want to do this automatically because I expect the Edges of my plot to change as I update my data. Here is how I constructed it (with arbitrary values).
%% Construct arbitrary dataset
EdgeTable = table('Size',[5 3],'VariableTypes',{'categorical' 'categorical' 'double'},'VariableNames',{'Source' 'Target' 'Weight'});
EdgeTable.Source = {'A' 'A' 'B' 'B' 'C'}'; EdgeTable.Target = {'B' 'D' 'C' 'D' 'D'}'; EdgeTable.Weight = (1:5)'; % I will use the Weight column to identify the edges easily
EdgeTable.EndNodes = cellstr([EdgeTable.Source EdgeTable.Target]); % Create the required EndNodes variable
EdgeTable = movevars(EdgeTable,'EndNodes','Before','Source'); % Place EndNodes as the first variable of the table
%% Create another column of the table that contains desired color. Edges to the target 'D' must be red in this case.
EdgeTable.EdgeCData = [0 0 0; 1 0 0; 0 0 0; 1 0 0; 1 0 0]; % I did this automatically using logical indexing for my real data, but I will put the values manually for this example.
G = digraph(EdgeTable,'omitselfloops');
p = plot(G,'EdgeColor',EdgeTable.EdgeCData,'ArrowPosition',0.8,...
'EdgeLabel',G.Edges.Weight,'Layout','layered','Direction','down');
EdgeTable
EdgeTable = 5×5 table
EndNodes Source Target Weight EdgeCData ______________ ______ ______ ______ ___________ {'A'} {'B'} {'A'} {'B'} 1 0 0 0 {'A'} {'D'} {'A'} {'D'} 2 1 0 0 {'B'} {'C'} {'B'} {'C'} 3 0 0 0 {'B'} {'D'} {'B'} {'D'} 4 1 0 0 {'C'} {'D'} {'C'} {'D'} 5 1 0 0
When I display the EdgeTable, it's clear that the Edges with Weights 2, 4, and 5 which connect to the Node D must be red. However, it is Edge 3 that is colored red and not Edge 4. Why is this the case? Does setting the Layout to 'layered' shuffle the indices? I can't think of the reason why. Please help me and thank you in advance!

Respuesta aceptada

Steven Lord
Steven Lord el 11 de Dic. de 2022
Let's compare the EdgeTable variable and the Edges property of the graph.
%% Construct arbitrary dataset
EdgeTable = table('Size',[5 3],'VariableTypes',{'categorical' 'categorical' 'double'},'VariableNames',{'Source' 'Target' 'Weight'});
EdgeTable.Source = {'A' 'A' 'B' 'B' 'C'}'; EdgeTable.Target = {'B' 'D' 'C' 'D' 'D'}'; EdgeTable.Weight = (1:5)'; % I will use the Weight column to identify the edges easily
EdgeTable.EndNodes = cellstr([EdgeTable.Source EdgeTable.Target]); % Create the required EndNodes variable
EdgeTable = movevars(EdgeTable,'EndNodes','Before','Source'); % Place EndNodes as the first variable of the table
%% Create another column of the table that contains desired color. Edges to the target 'D' must be red in this case.
EdgeTable.EdgeCData = [0 0 0; 1 0 0; 0 0 0; 1 0 0; 1 0 0]; % I did this automatically using logical indexing for my real data, but I will put the values manually for this example.
G = digraph(EdgeTable,'omitselfloops');
p = plot(G,'EdgeColor',EdgeTable.EdgeCData,'ArrowPosition',0.8,...
'EdgeLabel',G.Edges.Weight,'Layout','layered','Direction','down');
EdgeTable
EdgeTable = 5×5 table
EndNodes Source Target Weight EdgeCData ______________ ______ ______ ______ ___________ {'A'} {'B'} {'A'} {'B'} 1 0 0 0 {'A'} {'D'} {'A'} {'D'} 2 1 0 0 {'B'} {'C'} {'B'} {'C'} 3 0 0 0 {'B'} {'D'} {'B'} {'D'} 4 1 0 0 {'C'} {'D'} {'C'} {'D'} 5 1 0 0
G.Edges
ans = 5×5 table
EndNodes Source Target Weight EdgeCData ______________ ______ ______ ______ ___________ {'A'} {'B'} {'A'} {'B'} 1 0 0 0 {'A'} {'D'} {'A'} {'D'} 2 1 0 0 {'B'} {'D'} {'B'} {'D'} 4 1 0 0 {'B'} {'C'} {'B'} {'C'} 3 0 0 0 {'C'} {'D'} {'C'} {'D'} 5 1 0 0
The graph function does not guarantee that the Edges property will contain the edge table that you passed in exactly; it can (and in this case does) reorder the rows of that table as you can see most obviously from the Weight and EdgeCData variables. If we use the Edges table from the graph object itself for the color data in the plot (like you did with the Weight data):
figure
p = plot(G,'EdgeColor',G.Edges.EdgeCData,'ArrowPosition',0.8,...
'EdgeLabel',G.Edges.Weight,'Layout','layered','Direction','down');
we receive the plot that you expected.
  1 comentario
Ricardo Roxas
Ricardo Roxas el 11 de Dic. de 2022
I see! Thanks so much. I should have used the EdgeCData that was passed into the Graph object itself because of the shuffling.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Networks en Help Center y File Exchange.

Productos


Versión

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by