Matlab的:用“坚持”在不同分箱宽度

问题描述:

直方图结果我好像有奇怪的问题,当我绘制以下,似乎两个柱状图的条不会有相同的宽度:Matlab的:用“坚持”在不同分箱宽度

hold on 
[N,X] = hist(feature_1(:,1)) 
Bh = bar(X,N,'facecolor',[0.7 0.2 0.2]); 
[A,Y] = hist(feature_2(:,1)) 
Bh = bar(Y,A,'facecolor',[0.3 0.6 0.2]); 
hold off 

这是为什么? 谢谢

编辑:对不起,没有提供输入。 例如,feature_1(:,1:5) =

[0.72507334 
0.019627856 
0.19571847 
-0.23818338 
1.6526113 
0.23925941 
0.69914567 
0.15934853 
0.28082907 
-0.035707321 
0.072205774 
-0.15791744 
0.81654513 
0.19398287 
-0.33666527 
-0.24295111 
-1.0770919 
-1.2977802 
0.67290813 
-0.56841594 
-0.28522778 
-2.2450733 
-1.4413888 
-2.2216258 
-0.46346179 
1.8239603 
1.6443830 
1.3715266 
0.34339836 
-0.29903534] 

feature_2(:,1) =

[0.18098037 
-0.81469119 
-0.086869463 
-0.67799056 
1.1408544 
1.2589806 
1.0065788 
0.64472252 
-0.70849174 
0.69045025 
-0.0031675443 
-0.82824785 
0.15744546 
-0.028384065 
-0.065391541 
-0.35754660 
-1.0809286 
-0.12427557 
1.3792992 
-0.28740802 
1.7593855 
-1.2061185 
-3.0156419 
-1.1680259 
0.23381938 
0.97127295 
0.91487378 
0.83101124 
0.24949571 
-0.96599007] 

MATLAB suggests您使用histogram()而不是hist()

如果我不得不猜测为什么你的酒吧是不同的宽度,这将是因为你有不同数量的每个直方图bin,但不要听我的话。 (这也可能是一种风格的东西,那里的酒吧被抵消,这样就可以看见这两个颜色,hist()不混合像histogram()一样。)

您可以通过使用histogram()指定宽度解决宽度问题:

histogram(feature_1(:,1:5),'BinWidth',.5); 
hold on 
histogram(feature_2(:,1),'BinWidth',.5); 

如果你运行这段代码,你就可以看到绘制风格的差异:

subplot(2,1,1) 
hold on 
[N,X] = hist(feature_1(:,1:5)); 
Bh = bar(X,N,'facecolor',[0.7 0.2 0.2]); 
[A,Y] = hist(feature_2(:,1)); 
Bh = bar(Y,A,'facecolor',[0.3 0.6 0.2]); 

subplot(2,1,2) 
histogram(feature_1(:,1:5),'BinWidth',.5,'FaceColor','r'); 
hold on 
histogram(feature_2(:,1),'BinWidth',.5,'FaceColor','g'); 

希望这会有所帮助!

+0

有点?更喜欢绝对真棒回答:)!非常感谢 – Pegah

我没有得到目的,但两者的宽度相同,箱子是不同的。如果你想在同一个图中显示出来进行比较,那么你必须采用这种方式来适应

bar([X',Y']) 
xlable('-->No of bins') 
legend('Feature1','Feature2')