Digraph EdgeCData not plotting correct color.
조회 수: 5 (최근 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!
댓글 수: 0
채택된 답변
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
댓글 수: 0
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Graph and Network Algorithms에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!