필터 지우기
필터 지우기

How to set colorspace of line in special code of graph?

조회 수: 2 (최근 30일)
z cy
z cy 2020년 8월 19일
편집: Adam Danz 2020년 8월 22일
G = graph(index_s,index_t,weighted);
p1 = plot(G,'XData',X1,'YData',Y1);
What I want to change is the color of lines. Have anyone can help me ?

채택된 답변

Steven Lord
Steven Lord 2020년 8월 19일
There's no need to create additional lines to change the colors of lines in a GraphPlot. highlight them instead.
% Create graph
s = [1 1 1 2 2 3 3 4 5 5 6 7];
t = [2 4 8 3 7 4 6 5 6 8 7 8];
G = graph(s,t);
% Plot the graph with blue edges
h = plot(G, 'EdgeColor', 'b');
% Change the EdgeColor, LineWidth, and LineStyle properties of the edge (3, 4)
highlight(h, 3, 4, 'EdgeColor', 'r', 'LineWidth', 6, 'LineStyle', ':')
% Change the Marker and MarkerSize properties of nodes 1, 3, 5, and 7
highlight(h, 1:2:7, 'Marker', '+', 'MarkerSize', 16)
  댓글 수: 4
Steven Lord
Steven Lord 2020년 8월 22일
I don't believe we offer the capability to change the edge color of the markers indepedently of the main body of the markers (which you control with NodeColor.)
Adam Danz
Adam Danz 2020년 8월 22일
편집: Adam Danz 2020년 8월 22일
In that case, my answer is somewhat useful. You can plot another marker on top of a node and set it's EdgeColor.
hold on
hNew = plot(h.XData(3), h.YData(3), 'ro', 'LineWidth', 1);
linkprop([h,hNew],{'Marker','MarkerSize'}) % make sure marker props match
however, if the node properties are different between nodes, these properties will no longer be scalar and you'll need to index them.
hold on
hNew = plot(h.XData(3), h.YData(3), ...
'MarkerEdgeColor', 'r', ...
'LineWidth', 1, ...
'Marker', h.Marker{3}, ...
'MarkerSize', h.MarkerSize(3), ...
'MarkerFaceColor', 'none')

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Adam Danz
Adam Danz 2020년 8월 19일
편집: Adam Danz 2020년 8월 19일
Change the color of all graph edges
plot(G, . . ., 'r') % red
plot(G, . . ., [.5 0 .5]) % purple
Change the colors of a specific graph edges
See Steven Lord's answer.
Plot new lines on top of edges
I'm not sure how this would be useful but I'll leave it here because it's already written up.
You can use the XData and YData property of the GraphPlot object to re-plot a line segment on top of the existing one.
% Create graph
s = [1 1 1 2 2 3 3 4 5 5 6 7];
t = [2 4 8 3 7 4 6 5 6 8 7 8];
G = graph(s,t);
h = plot(G);
% set color of segment 3-4 to red
hold on
hNew = plot(h.XData(3:4), h.YData(3:4), 'r-');
linkprop([h,hNew],{'LineStyle','LineWidth'}) % make sure line props match
To set all graph edges to different colors,
% Create graph
s = [1 1 1 2 2 3 3 4 5 5 6 7];
t = [2 4 8 3 7 4 6 5 6 8 7 8];
G = graph(s,t);
h = plot(G);
% set color of segment 3-4 to red
hold on
colors = jet(numel(h.XData));
hNew = line([h.XData(:)'; circshift(h.XData(:)',1)], ...
[h.YData(:)'; circshift(h.YData(:)',1)]);
set(s, {'Color'}, mat2cell(colors,ones(size(colors,1),1),3))
linkprop([h,hNew'],{'LineStyle','LineWidth'}) % make sure line props match
  댓글 수: 3
Adam Danz
Adam Danz 2020년 8월 19일
Thanks, Steven Lord. I wasn't aware of the highlight function. Your comment should probably be an (accepted) answer.
z cy
z cy 2020년 8월 20일
Thank you very much!

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by