Different line styles in network graph?

14 vues (au cours des 30 derniers jours)
Yiteng
Yiteng le 8 Juin 2018
Commenté : Andrew Sandeman le 21 Juin 2023
I wondered if there is any way to change the style of individual lines in a network graph, constructed with graph() or digraph(). When plotting, there is an option 'LineStyle', but it does not allow for customization for each each. For instance, suppose I want to change the lines between 1 and 2 to dotted lines, while keeping the lines between the nodes themselves solid.
h=figure;
M = ones(2);
G = digraph(abs(M_int));
edgecolors = [-1 1 1 1];
plot(G, 'Layout', 'circle', 'ArrowSize', 20, 'EdgeAlpha', 1, ...
'EdgeCData', edgecolors, 'LineWidth', 3, 'NodeLabel', {},...
'Marker', 'o', 'MarkerSize', 100, 'NodeColor', 'k');
nLabels = {'1', '2'};
text([-1 1]-0.1, [0 0]+0.01, nLabels, 'Color', 'w', 'FontSize', 40); % node labels
ax = gca;
map = [1, 0, 0
0, 0, 1];
colormap(map);
ax.Visible = 'off';
h.Color = [1 1 1];
set(ax, 'Units', 'Inches', 'Position', [0 0 8 4]);
set(h, 'Units', 'Inches', 'Position', [1 1 8 4]);
Reference: https://nl.mathworks.com/help/matlab/ref/matlab.graphics.chart.primitive.graphplot-properties.html

Réponse acceptée

Steven Lord
Steven Lord le 8 Juin 2018
Modifié(e) : Steven Lord le 8 Juin 2018
One easy way to do this is to highlight the edges you want to change.
g = graph(bucky);
h = plot(g);
highlight(h, 49, 51, 'LineStyle', ':', 'EdgeColor', 'r', 'LineWidth', 12)
The edge between nodes 49 and 51 in the buckyball graph is now wider than the others, red, and dotted.
Note that you don't need to change one node and/or edge at a time; you can specify a path if you want.
highlight(h, ... % highlight
shortestpath(g, 29, 32), ... % a shortest path from nodes 29 to 32
'EdgeColor', 'r', ... % with red lines
'LineWidth', 2, ... % that are wider than the others
'MarkerSize', 8, ... % and have larger
'NodeColor', 'c', ... % cyan
'Marker', 's', ); % squares as the markers
  2 commentaires
Andrew Sandeman
Andrew Sandeman le 21 Juin 2023
is it possible to edit the properties of a specific edge? (there may be more than one edge between the nodes 49 and 51)
Andrew Sandeman
Andrew Sandeman le 21 Juin 2023
Found a solution, you need to access the LineStyle property of GraphPlot object, giving it an string array specifying the line style for every edge e.g.
```
G_plot = plot(G);
G_plot.LineStyle = ["-", "--"];
```

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Graph and Network Algorithms dans Help Center et File Exchange

Produits


Version

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by