
Show Values on bar graph; above bar when positive values, below bar when positive values
    조회 수: 5 (최근 30일)
  
       이전 댓글 표시
    
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
채택된 답변
  Mehmed Saad
      
 2020년 4월 22일
        
      편집: Mehmed Saad
      
 2020년 4월 22일
  
      figure,

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
추가 답변 (1개)
  Johannes Hougaard
      
 2020년 4월 22일
        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
참고 항목
카테고리
				Help Center 및 File Exchange에서 Discrete Data Plots에 대해 자세히 알아보기
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!


