Different line styles in network graph?
15 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Yiteng
am 8 Jun. 2018
Kommentiert: Andrew Sandeman
am 21 Jun. 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
0 Kommentare
Akzeptierte Antwort
Steven Lord
am 8 Jun. 2018
Bearbeitet: Steven Lord
am 8 Jun. 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 Kommentare
Andrew Sandeman
am 21 Jun. 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
am 21 Jun. 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 = ["-", "--"];
```
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Graph and Network Algorithms finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!