Different line styles in network graph?

16 ビュー (過去 30 日間)
Yiteng
Yiteng 2018 年 6 月 8 日
コメント済み: Andrew Sandeman 2023 年 6 月 21 日
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 Center および File ExchangeGraph 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