Hello all,
I am creating several bar charts and would like to indicate the value above each bar. Unfortunately, this value varies very often and I have to edit each chart individually. If I do not do this, the chart looks like this:
Is there a way to automatically scale the axes so that there is enough space for the value to still be displayed above the bar? Is there also a function so that the values are above the bar for positive numbers and below the bar for negative numbers? I always change this in the function itself with "top" or "bottom".
Many thanks in advance!

 採用された回答

Star Strider
Star Strider 2023 年 4 月 16 日

0 投票

I am not exactly certain wher eyou want the bar values placed, so it may be necessary to edit the two text calls to create the result you want. This increases the ylim values to accommodate the bar labels, and selects the labels to be placed either above or below the bar ends, depenmding on their signs.
Try this —
x = 1:10;
y = round(100*randn(size(x)));
figure
hb = bar(x, y, 'c');
xe = hb.XEndPoints;
ye = hb.YEndPoints;
ylim(ylim*1.105) % Multiply 'ylim'
yepos = y>=0; % 'true' For y>= 0
text(xe(yepos),ye(yepos), compose('%g',y(yepos)), 'Horiz','center', 'Vert','bottom') % Positive Values
text(xe(~yepos),ye(~yepos), compose('%g',y(~yepos)), 'Horiz','center', 'Vert','top') % Negative Values
.

5 件のコメント

Max1234
Max1234 2023 年 4 月 16 日
Thank you very much! You have helped me a lot! That was exactly what I needed!
Max1234
Max1234 2023 年 4 月 16 日
I do have one more question. How can I now incorporate the fact that if the value is 0, there is no number?
Star Strider
Star Strider 2023 年 4 月 16 日
In that event, the ‘yepos’ logical vector becomes two logical vectors in order to eliminate printing the zero value —
yepos = y>0; % 'true' For y>0
yeneg = y<0; % 'true' For y<0
text(xe(yepos),ye(yepos), compose('%g',y(yepos)), 'Horiz','center', 'Vert','bottom') % Positive Values
text(xe(yeneg),ye(yeneg), compose('%g',y(yeneg)), 'Horiz','center', 'Vert','top') % Negative Values
With those changes —
x = 1:10;
y = round(100*randn(size(x)));
y(6) = 0; % Force 'y(6)' To Be Zero
figure
hb = bar(x, y, 'c');
xe = hb.XEndPoints;
ye = hb.YEndPoints;
ylim(ylim*1.105) % Multiply 'ylim'
yepos = y>0; % 'true' For y>0
yeneg = y<0; % 'true' For y<0
text(xe(yepos),ye(yepos), compose('%g',y(yepos)), 'Horiz','center', 'Vert','bottom') % Positive Values
text(xe(yeneg),ye(yeneg), compose('%g',y(yeneg)), 'Horiz','center', 'Vert','top') % Negative Values
The plot is essentially the same as the previous plot, with no value being plotted for bars (here position 6) with zero values.
.
Max1234
Max1234 2023 年 4 月 16 日
Perfect! Thank you very much!
Star Strider
Star Strider 2023 年 4 月 16 日
As always, my pleasure!

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

その他の回答 (1 件)

DGM
DGM 2023 年 4 月 16 日
編集済み: DGM 2023 年 4 月 16 日

0 投票

This should work with positive or negative values.
y = [5 10 15 100 150 200];
%y = [y -y];
hb = bar(y,0.4);
xtips = hb.XEndPoints;
ytips = hb.YEndPoints;
barlabels = string(hb.YData);
ht = text(xtips,ytips,barlabels,'HorizontalAlignment','center',...
'VerticalAlignment','bottom');
if any(y < 0)
% flip the labels on negative bars
for k = 1:numel(ht)
if y(k) < 0
ht(k).VerticalAlignment = 'top';
end
end
% get the bot edge of the lowest label
% add 2% just for some extra padding
[~,mnidx] = min(y);
ymin = ht(mnidx).Extent(2)*1.02;
else
% set the ylimit
yl = ylim();
ymin = yl(1);
end
% get the top edge of the highest label
% add 2% just for some extra padding
[~,mxidx] = max(y);
ymax = sum(ht(mxidx).Extent([2 4]))*1.02;
% set the ylimit
ylim([ymin ymax])

カテゴリ

ヘルプ センター および File ExchangeAxes Appearance についてさらに検索

質問済み:

2023 年 4 月 16 日

コメント済み:

2023 年 4 月 16 日

Community Treasure Hunt

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

Start Hunting!

Translated by