![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/286520/image.png)
Show Values on bar graph; above bar when positive values, below bar when positive values
3 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
BdS
am 22 Apr. 2020
Bearbeitet: Mehmed Saad
am 22 Apr. 2020
Hi,
with the code below I built a bar graph and inserted the values on each bar.
xtips2 = b(1).XEndPoints;
ytips2 = b(1).YEndPoints;
labels2 = string(b(1).YData);
text(xtips2,ytips2,labels2,'HorizontalAlignment','center',...
'VerticalAlignment','bottom')
is there a way of inserting all negative values below the bar and positive values above?
Many thanks for your reply.
0 Kommentare
Akzeptierte Antwort
Mehmed Saad
am 22 Apr. 2020
Bearbeitet: Mehmed Saad
am 22 Apr. 2020
figure,
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/286520/image.png)
vals = randi([-10 10],1,10);
x = 1:10;
h = bar(x,vals);ylim([-12 12])
lbs1 = cellfun(@num2str,num2cell(vals),'UniformOutput',false);
cond = vals>0;
text(x(cond),vals(cond),lbs1(cond),'HorizontalAlignment','center',...
'VerticalAlignment','bottom')
text(x(~cond),vals(~cond),lbs1(~cond),'HorizontalAlignment','center',...
'VerticalAlignment','top')
0 Kommentare
Weitere Antworten (1)
Johannes Hougaard
am 22 Apr. 2020
Just add an additional line, indexing the text according to ytips2
xtips2 = b(1).XEndPoints;
ytips2 = b(1).YEndPoints;
labels2 = string(b(1).YData);
text(xtips2(ytips2 > 0),ytips2(ytips2 > 0),labels2(ytips2 > 0),'HorizontalAlignment','center',...
'VerticalAlignment','bottom')
text(xtips2(ytips2 < 0),ytips2(ytips2 < 0),labels2(ytips2 < 0),'HorizontalAlignment','center',...
'VerticalAlignment','top')
0 Kommentare
Siehe auch
Kategorien
Mehr zu Graph and Network Algorithms finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!