How to format the number of digits after a decimal point for double values when visualising EdgeLabels in a graph?
51 ビュー (過去 30 日間)
古いコメントを表示
Hi,
I have a directed graph where I would like to display the edge labels which are double. When I display it, it shows 4-digits after the decimal point but it looks so crowded on the graph (please see figure below). Therefore, I would like to round it up to 2 digits after the decimal point or even 1-digit.
I have tried a few things but I haven't still figured it out. Although it will still be crowded with 2 digits after the decimal point, I would still think it's much more manageble.
Could you please help me?
Additional comment: I have used the MATLAB preferences --> Variables --> Default array format and changed it to "bank". The matrix is showing the values with 2 digits after the decimal point but the edge labels are still with 4 digits after the decimal point.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/275209/image.png)
3 件のコメント
BobH
2020 年 3 月 5 日
If yes, then by default, displaying edge labels means 'show the edge weights', so you may be able to alter the weights to achieve the look you want.
If you are comfortable changing the underlying code, you can try making a change to show arbitrary text.
採用された回答
Steven Lord
2020 年 3 月 5 日
The display format preferences don't affect the precision used to create edge labels when you plot a digraph object. Use sprintf to create a string array or a cell array containing char vectors from the edge weights of your digraph and set the EdgeLabel property of the GraphPlot object to that array. Alternately you could round the weights to 2 decimal places and specify that numeric vector as the EdgeLabel property.
S = bucky;
S = S.*rand(size(S));
d = digraph(S);
figure
h = plot(d, 'EdgeLabel', round(d.Edges.Weight, 2));
title('Round')
figure
h = plot(d, 'EdgeLabel', arrayfun(@(x) sprintf("%.2f", x), d.Edges.Weight));
title('Sprintf')
The two plots are very slightly different (a weight of exactly 0.5 would be displayed differently, for example.)
0 件のコメント
その他の回答 (0 件)
参考
カテゴリ
Help Center および File Exchange で Graph and Network Algorithms についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!