How to set colorspace of line in special code of graph?
조회 수: 1 (최근 30일)
이전 댓글 표시
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 ?
댓글 수: 0
채택된 답변
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
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
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
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
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
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/348563/image.png)
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
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/348566/image.png)
댓글 수: 3
Adam Danz
2020년 8월 19일
Thanks, Steven Lord. I wasn't aware of the highlight function. Your comment should probably be an (accepted) answer.
참고 항목
카테고리
Help Center 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!