Why does my EdgeColor map incorrectly to the edges of my Graph Plot?
3 次查看(过去 30 天)
显示 更早的评论
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
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!
0 个评论
采纳的回答
Steven Lord
2022-12-11
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
G.Edges
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.
更多回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!