# matlab 绘制直方图——常用命令

close all ,
x = rand(100,1);
%获得直方图的数据
[n,y] = hist(x);
maxN = max(n);
%设置显示x,y长度限制
axis([0 1.2 0 maxN+2])
%根据直方图的数据绘制出图形
bar(y,n);
for i = 1:length(y)
%直方图上面数据对不齐，利用水平和垂直对齐 ，可以参考search  Document 中的text函数
text(y(i),n(i)+0.5,num2str(n(i)),'VerticalAlignment','middle','HorizontalAlignment','center');

end

histfit(x,maxN)显示拟合的曲线%%%%%%%%%%%%%

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%5

[f,xi,bw] = ksdensity(x);%核密度估计的方法

figure()

plot(xi,f,'-k','LineWidth',2);hold on

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

x=normrnd(0,1,1,100);

hist(x);

[n,hx]=hist(x);

str = mat2cell(num2str(n'));

text(hx,n+0.3,str,'Color','r');

x=[1 2 ];

y=[0. 12];

%y=exp(-x.*x);

% stem(x,y,'r');

bar(x,y,'b');

for ii=1:length(x)

str=sprintf('%4.1f%%',y(ii)*100);%显示百分号

%text(x(ii)-0.2,y(ii)+0.02,str);

text(x(ii)-0.2,y(ii)+0.02,str,'VerticalAlignment','middle','HorizontalAlignment','center');

end

% set(gca,'FontName','Times New Roman','FontSize',15);

x=[1 2 3 4 5 6];

y=[2 4 6 8 10 12];

bar(x,y,'b');hold on;

x2=min(x):(length(x)-1)/6:max(x)%点列

y2=interp1(x,y,x2,'spline')%上述函数进行插值，

%  figure,

plot(x2,y2,'r','Linewidth',2);做出图像.

matlab程序，修改一下文年后缀即可。

https://blog.sciencenet.cn/blog-597740-1036452.html

## 相关博文

GMT+8, 2022-8-17 11:29