idignew

这个小代码用于matlab柱状图显示比例及数值,

 

函数如下:

function myhist(x)
% myhist Codeby SimonLiang
% Email:idignew@126.com
% 用于显示柱状图比例及具体数值

h=histogram(x);

hold on;

%显示柱状图数值
hBin=h.BinEdges(1:end-1)+h.BinWidth/2;
text(hBin,h.Values+max(h.Values)/25,num2cell(h.Values));

%计算百分比
Hpercent=round(h.Values/sum(h.Values)*100);

%加入百分号
Hpercent2=num2cell(Hpercent);
for i=1: length(Hpercent)
    Hpercent2(i)={[num2str(Hpercent(i)),\'%\']};
end
text(hBin,h.Values+max(h.Values)/15,Hpercent2);%显示百分比

%显示标题
title([\'TotalCounts=\',num2str(sum(h.Values))]);

hold off
end


 

调用范例:

x=randi([1,100],1,50);
myhist(x);

 

效果如下:

 

分类:

技术点:

相关文章:

  • 2022-02-02
  • 2022-01-25
  • 2022-12-23
  • 2022-01-10
  • 2021-08-03
  • 2021-12-12
  • 2022-12-23
猜你喜欢
  • 2022-01-14
  • 2021-05-05
  • 2022-12-23
  • 2022-12-23
  • 2021-05-08
  • 2021-12-13
  • 2022-02-22
相关资源
相似解决方案