フィルターのクリア

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.
  3 件のコメント
Suzan Doornwaard
Suzan Doornwaard 2020 年 3 月 5 日
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 2020 年 3 月 5 日
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.

サインインしてコメントする。

採用された回答

Steven Lord
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 件)

カテゴリ

Help Center および File ExchangeGraph and Network Algorithms についてさらに検索

製品


リリース

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by