Effacer les filtres
Effacer les filtres

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

2 vues (au cours des 30 derniers jours)
z cy
z cy le 19 Août 2020
Modifié(e) : Adam Danz le 22 Août 2020
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 ?

Réponse acceptée

Steven Lord
Steven Lord le 19 Août 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 commentaires
Steven Lord
Steven Lord le 22 Août 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
Adam Danz le 22 Août 2020
Modifié(e) : Adam Danz le 22 Août 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')

Connectez-vous pour commenter.

Plus de réponses (1)

Adam Danz
Adam Danz le 19 Août 2020
Modifié(e) : Adam Danz le 19 Août 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
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 commentaires
Adam Danz
Adam Danz le 19 Août 2020
Thanks, Steven Lord. I wasn't aware of the highlight function. Your comment should probably be an (accepted) answer.
z cy
z cy le 20 Août 2020
Thank you very much!

Connectez-vous pour commenter.

Catégories

En savoir plus sur Loops and Conditional Statements dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by