我正在尝试堆叠两个具有正值和负值的柱形图。但是使用以下代码,我将柱形图分开,有时一个隐藏在另一个之后。我正在发布脚本和示例数据,以便可以复制。我正在使用在线Matlab
retndecm(1:52,1)= [0.3715
0.3590
0.2862
0.1936
0.2177
0.2111
0.2019
0.2012
0.2162
0.2236
0.1874
0.2116
0.2569
0.2384
0.2436
0.2416
0.2590
0.2682
0.2695
0.2832
0.2616
0.2506
0.2548
0.2470
0.2612
0.2588
0.2399
0.2848
0.3023
0.3166
0.3535
0.3328
0.3187
0.3365
0.3313
0.3417
0.3411
0.4013
0.3717
0.3574
0.3288
0.3071
0.3271
0.2802
0.2787
0.2649
0.2619
0.2258
0.2432
0.2314
0.2151
0.2080];
retndecm(1:52,2)=[0.0561
0.0462
0.0783
0.1246
0.1099
0.0921
0.0755
0.0675
0.0582
0.0599
0.0249
0.0137
0.0136
0.0108
0.0052
0.0176
0.0259
0.0183
0.0258
0.0312
0.0383
0.0421
0.0293
0.0376
0.0355
0.0344
0.0341
0.0321
0.0404
0.0289
0.0318
0.0374
0.0283
0.0289
0.0405
0.0321
0.0402
0.0528
0.0791
0.0756
0.0798
0.0892
0.0933
0.0978
0.1073
0.1194
0.1172
0.1117
0.1174
0.0960
0.1041
0.1178];
retndecm(1:52,3) = [-0.1152
-0.0963
-0.1035
-0.1144
-0.1309
-0.1289
-0.1337
-0.1347
-0.1272
-0.1289
-0.1361
-0.1176
-0.1057
-0.1125
-0.1068
-0.1119
-0.1142
-0.1188
-0.1163
-0.1115
-0.1098
-0.1145
-0.1110
-0.1046
-0.0953
-0.0985
-0.1044
-0.0967
-0.0964
-0.0932
-0.0959
-0.0938
-0.0930
-0.0843
-0.0815
-0.0673
-0.0609
-0.0591
-0.0464
-0.0736
-0.0681
-0.0691
-0.0612
-0.0758
-0.0658
-0.0690
-0.0736
-0.0774
-0.0731
-0.0762
-0.0711
-0.0754];
ndfspi(1:52,1)= [0.6025
0.6024
0.6028
0.6024
0.6030
0.6028
0.6026
0.6031
0.6031
0.6031
0.6029
0.6029
0.6010
0.6030
0.6033
0.6025
0.6034
0.6021
0.6014
0.6027
0.6031
0.6032
0.6030
0.6032
0.6029
0.6025
0.6024
0.6029
0.6026
0.6011
0.6025
0.6013
0.6025
0.6028
0.5989
0.6025
0.6028
0.6035
0.6035
0.6033
0.6031
0.6030
0.6033
0.6027
0.6030
0.6031
0.6033
0.6029
0.6031
0.6027
0.6026
0.6029];
nfundbeta = ndfspi(1:52,1)-1;
%% figures
set(groot,'DefaultFigureColormap',jet);
r=figure;
hold on;
M=retndecm(:,1)';
t = datetime(2001,1,5) + calweeks(1:52);
h = bar(t, retndecm(:,1),.99,"stacked");
set(h, 'FaceColor', 'flat');
h.CData = ndfspi(:,1)';
caxis([min(-1) max(1)]);
hold on;
t = datetime(2001,1,5) + calweeks(1:52);
l=bar(t,retndecm(:,2),.99,"stacked");
l.FaceColor = [.5 0 .5];
hold on;
N=retndecm(:,3)';
t = datetime(2001,1,5) + calweeks(1:52);
h = bar(t, retndecm(:,3),.99,"stacked");
set(h, 'FaceColor', 'flat');
h.CData = nfundbeta(:,1)';
caxis([min(-1) max(1)]);
colorbar('eastoutside');
set(gcf, 'PaperSize', [20 10], 'PaperPosition', [0 0 20 10])
set(findall(gcf,'-property','FontSize'),'FontSize',12);
saveas(gcf,'test2', 'pdf');
hold off;