EDA365电子论坛网
标题:
matlab柱状图(histogram)显示数值及比例小程序。
[打印本页]
作者:
House
时间:
2020-4-15 14:06
标题:
matlab柱状图(histogram)显示数值及比例小程序。
matlab柱状图(histogram)显示数值及比例小程序。
) @# j6 k( a3 u, d( P0 P7 L/ K o) _
m文件如下:
( e6 ^# ^& P6 P7 `& z
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
' e! Y6 w% G( F; v* Q3 Y: H
% N' I- d+ o7 S2 ?+ X! e9 O/ r
! X; H3 a: ^& g6 O2 U' w
# K* \, Z' \* j8 O y
7 l- H# Z/ F! H
调用范例:
+ h8 z' G9 K' Y& G
x=randi([1,100],1,50);
myhist(x);
3 J+ f# U. Y4 E; b
8 \, t$ P1 m4 ?( U5 F
2 R, P8 a! N ~# O5 y
2 |5 Y( {; ~, x% T( a
作者:
WNSKS
时间:
2020-4-15 18:43
看看楼主分享的代码。
欢迎光临 EDA365电子论坛网 (https://bbs.eda365.com/)
Powered by Discuz! X3.2