Display values barh, YEndPoints/XEndPoints does not work

33 ビュー (過去 30 日間)
Anil Dogan
Anil Dogan 2022 年 6 月 27 日
コメント済み: Voss 2022 年 6 月 28 日
Hello,
I have created a bar chart. I would like to display the values. The example from the help center does not work (https://de.mathworks.com/help/matlab/ref/barh.html). I get the error: Unrecognized method, property, or field 'YEndPoints' for class 'matlab.graphics.chart.primitive.Bar'. I am using Matlab R2019a.
Below is my example:
fig1=figure(1);
X=[6.5,7.5,8.5,9.5,10.5,11.5,12.5,13.5,14.5,15.5,16.5,17.5,18.5];
Y = [
45 72 36
36 7 24
34 35 37
107 86 70
86 61 105
108 71 149
92 64 107
69 57 87
47 47 75
48 51 69
38 33 82
65 33 91
28 10 35
];
fig2=figure(2);
b=barh(X,Y);
grid on
set(gca,'YDir','reverse');
set(gca,'XLim', [0 175]);
set(gca,'XTick', 0:25:175);
set(gca,'YLim',[6 19]);
set(gca,'YTick',6:1:19);
yticklabels({'6','7','8','9','10','11','12','13','14','15','16','17','18','18.3'})
% Does not work:
% xtips1 = b(1).YEndPoints + 0.3;
% ytips1 = b(1).XEndPoints;
% labels1 = string(b(1).YData);
% text(xtips1,ytips1,labels1,'VerticalAlignment','middle')
  1 件のコメント
dpb
dpb 2022 年 6 月 27 日
That was during the period TMW hid the useful properties of bar positions from which could derive the bar midpoints and before the X/YEndpoints properties were introduced.
You'll have to compute the positions yourself; there's a prior Answer I posted several times that shows the workaround; unfortunately, I didn't save a link to it and I have a meeting in town I've got to go get ready for now...
It's a pain if you can't upgrade although it is doable and, once see it through the use of an undocumented property, the code isn't hard, but have to know/see the trick...

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

採用された回答

Voss
Voss 2022 年 6 月 27 日
Maybe you can use X/YData instead of X/YEndPoints. Here it is with labels on all bars; you can adapt it to label only the first bar if that's what you want to do.
fig1=figure(1);
X=[6.5,7.5,8.5,9.5,10.5,11.5,12.5,13.5,14.5,15.5,16.5,17.5,18.5];
Y = [
45 72 36
36 7 24
34 35 37
107 86 70
86 61 105
108 71 149
92 64 107
69 57 87
47 47 75
48 51 69
38 33 82
65 33 91
28 10 35
];
fig2=figure(2);
b=barh(X,Y);
grid on
set(gca,'YDir','reverse');
set(gca,'XLim', [0 175]);
set(gca,'XTick', 0:25:175);
set(gca,'YLim',[6 19]);
set(gca,'YTick',6:1:19);
yticklabels({'6','7','8','9','10','11','12','13','14','15','16','17','18','18.3'})
Nb = numel(b);
dy = (-(Nb-1):2:(Nb-1))/(2*Nb+3);
for ii = 1:Nb
xtips1 = b(ii).YData + 0.3;
ytips1 = b(ii).XData + dy(ii);
labels1 = string(b(ii).YData);
text(xtips1,ytips1,labels1,'VerticalAlignment','middle')
end
  2 件のコメント
Anil Dogan
Anil Dogan 2022 年 6 月 28 日
works really well! Thank you!
Voss
Voss 2022 年 6 月 28 日
You're welcome!

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

その他の回答 (1 件)

dpb
dpb 2022 年 6 月 27 日
One of the previous postings -- same idea but uses the internal Offset values from the bar handles...
This was for bar so will need to change the orientation/alignment for x and y directions to match for barh but otherwise should work...

カテゴリ

Help Center および File ExchangeAxis Labels についてさらに検索

製品


リリース

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by