How to put endpoint on Bar graph

16 ビュー (過去 30 日間)
shadan Kazempour Sabahi
shadan Kazempour Sabahi 2021 年 10 月 20 日
編集済み: dpb 2021 年 10 月 20 日
y = [75.8 78.05; 81 80.30; 91 80.78];
b = bar(y);
Hello , I want to add endpoints on each graph. How should I do that?
  3 件のコメント
shadan Kazempour Sabahi
shadan Kazempour Sabahi 2021 年 10 月 20 日
No I mean that I want my values for each Bar graph shows on top of my graphs for example first bar graph is 75.8 and I want this value to be appeared on top of the graph.
Scott MacKenzie
Scott MacKenzie 2021 年 10 月 20 日
OK, got it. I just posted an answer. Good luck.

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

採用された回答

Star Strider
Star Strider 2021 年 10 月 20 日
In R2021b, this is straightforward (and described in Specify Labels at the Ends of Bars).
y = [75.8 78.05; 81 80.30; 91 80.78];
figure
b = bar(y);
for k = 1:numel(b)
xtips = b(k).XEndPoints;
ytips = b(k).YEndPoints;
text(xtips, ytips, compose('%.2f',y(:,k)), 'Horiz','center', 'Vert','bottom')
end
In R2017b, this is a bit more involved —
figure
b = bar(y);
for k = 1:numel(b)
ctr = bsxfun(@plus, b(k).XData, [b(k).XOffset]');
ydt = b(k).YData;
str = compose('%.2f',ydt);
text(ctr,ydt, str, 'HorizontalAlignment','center', 'VerticalAlignment','bottom')
end
Experiment to get different results.
.

その他の回答 (1 件)

dpb
dpb 2021 年 10 月 20 日
編集済み: dpb 2021 年 10 月 20 日
Like
maybe?
There's a klunky example at the doc for bar, but it is far simpler than the multi-step illustration to write it as
hB=bar(y);
arrayfun(@(h)text(h.XEndPoints,h.YEndPoints, ...
compose('%.1f',h.YData), ...
'HorizontalAlignment','center', ...
'VerticalAlignment','bottom'),hB)
Salt the format string to suit...

カテゴリ

Help Center および File Exchange2-D and 3-D Plots についてさらに検索

タグ

製品


リリース

R2017b

Community Treasure Hunt

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

Start Hunting!

Translated by