Digraph EdgeCData not plotting correct color.

5 ビュー (過去 30 日間)
Mitchell Tillman
Mitchell Tillman 2022 年 8 月 27 日
回答済み: Mayank Sengar 2022 年 8 月 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 Center および File ExchangeColormaps についてさらに検索

製品


リリース

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by