Clear Filters
Clear Filters

How to format the number of digits after a decimal point for double values when visualising EdgeLabels in a graph?

51 views (last 30 days)
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.
  3 Comments
Suzan Doornwaard
Suzan Doornwaard on 5 Mar 2020
Thanks for your help. I think I realized the problem (i might be wrong). I have some transition probabilities that requires more than .2f precision, such as 0.00245..
I guess MATLAB is automatically preventing me to convert such data to 0.00 by showing .4 no matter what I've tried.
BobH
BobH on 5 Mar 2020
Are you using a biograph object for this directed graph?
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.

Sign in to comment.

Accepted Answer

Steven Lord
Steven Lord on 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.)

More Answers (0)

Categories

Find more on Graph and Network Algorithms in Help Center and File Exchange

Products


Release

R2018b

Community Treasure Hunt

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

Start Hunting!