Matlab:使用"hold on"作为直方图会导致不同的箱宽



我似乎有一个奇怪的问题,即当我绘制以下内容时,两个直方图的条形似乎没有相同的宽度:

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建议您使用histogram()而不是hist()

如果我不得不猜测为什么您的条形宽度不同,那是因为您对每个直方图都有不同数量的垃圾箱,尽管不要对此表示宽容。(这也可能是一种风格上的东西,其中杆被偏移了,因此您可以看到两种颜色,因为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');

希望这有所帮助!

我没有目的,但两者的宽度都是相同的,垃圾箱是不同的。如果您想以相同的数字显示两个,则必须以这种方式适应

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

最新更新