Digraph EdgeCData not plotting correct color.
4 views (last 30 days)
Show older comments
Mitchell Tillman
on 27 Aug 2022
Answered: Mayank Sengar
on 30 Aug 2022
I am trying to plot a digraph with edges colored using colors from XKCD's color list on the File Exchange. The problem is that the edge colors do not plot correctly. Here is my code:
fig=uifigure; % Initialize a new figure.
load(filePath,'rgblist'); % Load the matrix of rgb triples (N x 3)
% Find which rows in the rgblist matrix contain the correct colors. RGB
% triples are rounded here to account for MATLAB's rounding error.
edgeColorsIdx=NaN(size(Digraph.Edges.Color,1),1);
for i=1:size(Digraph.Edges.Color,1)
edgeColorsIdx(i)=find(ismember(round(rgblist,3),round(Digraph.Edges.Color(i,:),3),'rows')==1);
end
colormap(fig,rgblist); % Set the colormap of the specified figure to rgblist.
% Plot the Digraph with standard blue nodes, and one color per edge.
plot(fig,Digraph,'XData',Digraph.Nodes.Coordinates(:,1),'YData',Digraph.Nodes.Coordinates(:,2),'NodeLabel',Digraph.Nodes.FunctionNames,'NodeColor',[0 0.447 0.741],...
'EdgeCData',edgeColorsIdx);
This code finds the correct color indices, because I can plot them properly using patch or any other type of plot. I also think that the colormap is being properly set, because
isequal(colormap(fig),rgblist)
returns 1. But when plotting the Digraph, it plots the wrong color edges! What am I doing wrong? Thanks!
0 Comments
Accepted Answer
Mayank Sengar
on 30 Aug 2022
You can make use of GraphPlot properties here and use the given syntax:
P = plot(Digraph,'Edgecolor',R);
where, R is a column vector consisting of RGB triplets of color of corresponding edges. RGB triplets can be obtained using rgb function provided in XKCD's color list on the File Exchange.
You can also refer to the example below
t = 1:3; % declaring row vector [1 2 3]
h = 2:4; % declaring row vector [2 3 4]
g = digraph(t,h); % created directed graph with edges corresponding elements of t and h
colors = ["Red" ; "Blue" ; "Green"]; % colors of corresponding edges
R = rgb(colors); % column vector consisting of RGB triplets of colors of corresponding edges
p = plot(g,'EdgeColor',R); % plotting the digraph with colored edges
0 Comments
More Answers (0)
See Also
Categories
Find more on Graph and Network Algorithms in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!