Different line styles in network graph?

15 次查看(过去 30 天)
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

采纳的回答

Steven Lord
Steven Lord 2018-6-8
编辑:Steven Lord 2018-6-8
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 个评论
Andrew Sandeman
Andrew Sandeman 2023-6-21
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 2023-6-21
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 = ["-", "--"];
```

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Graph and Network Algorithms 的更多信息

标签

产品


版本

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by