Digraph EdgeCData not plotting correct color.

4 次查看(过去 30 天)
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!

采纳的回答

Mayank Sengar
Mayank Sengar 2022-8-30
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
You can refer Graph plot appearance and behavior for more clarity.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Graph and Network Algorithms 的更多信息

产品


版本

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by