필터 지우기
필터 지우기

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 CenterFile Exchange에서 Graph 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