How to set colorspace of line in special code of graph?
1 view (last 30 days)
Show older comments
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 Comments
Accepted Answer
Steven Lord
on 19 Aug 2020
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 Comments
Steven Lord
on 22 Aug 2020
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
on 22 Aug 2020
Edited: Adam Danz
on 22 Aug 2020
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')
More Answers (1)
Adam Danz
on 19 Aug 2020
Edited: Adam Danz
on 19 Aug 2020
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 Comments
Adam Danz
on 19 Aug 2020
Thanks, Steven Lord. I wasn't aware of the highlight function. Your comment should probably be an (accepted) answer.
See Also
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!