How to format the number of digits after a decimal point for double values when visualising EdgeLabels in a graph?
50 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Suzan Doornwaard
il 5 Mar 2020
Risposto: Steven Lord
il 5 Mar 2020
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 Commenti
BobH
il 5 Mar 2020
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.
Risposta accettata
Steven Lord
il 5 Mar 2020
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 Commenti
Più risposte (0)
Vedere anche
Categorie
Scopri di più su Graph and Network Algorithms in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!