在之前的文章中,分享了Matlab带误差棒的折线图绘制模板:

带误差棒的柱状图绘制模板:

进一步,再来分享一下带误差棒的堆叠柱状图的绘制模板。
先来看一下成品效果:

特别提示:本期内容『数据+代码』已上传资源群中,加群的朋友请自行下载。有需要的朋友可以关注同名公号【阿昆的科研日常】,后台回复关键词【绘图桶】查看加入方式。
模板中最关键的部分内容:
1. 数据准备
此部分主要是读取原始数据并初始化绘图参数。
% 读取数据load data.mat% 初始化绘图参数x = X;y = A;errD = errdw;errU = errup;
2. 颜色定义
作图不配色就好比做菜不放盐,总让人感觉少些味道。
但颜色搭配比较考验个人审美,需要多加尝试。
这里直接使用TheColor配色工具中的SCI权威配色库:
%% 颜色定义map = TheColor('sci',2064,'map',10);map = flipud(map);C1 = map(1,1:3);C2 = map(2,1:3);C3 = map(3,1:3);C4 = map(6,1:3);

3. 带误差棒的堆叠柱状图绘制
通过调用‘bar’和‘errorbar’命令,绘制初始带误差棒的堆叠柱状图。
GO = bar(x,y',0.8,'stacked','EdgeColor','k');% 添加误差棒for i = 1:4xpos = GO(1,i).XEndPoints; % v2019bypos = GO(1,i).YEndPoints; % v2019berrorbar(xpos, ypos, errU(i,:), errD(i,:),...'LineStyle','none','Color','k','LineWidth',0.8);endhTitle = title('Stacked bar chart with errorbar');hXLabel = xlabel('Samples');hYLabel = ylabel('RMSE (m)');
4. 细节优化
为了插图的美观,将初始带误差棒的堆叠柱状图赋上之前选择的颜色:
% 赋色GO(1).FaceColor = C1;GO(2).FaceColor = C2;GO(3).FaceColor = C3;GO(4).FaceColor = C4;
然后,对坐标轴细节等进行美化:
% 坐标区基本属性调整set(gca, 'Box', 'off', ... % 边框'LineWidth', 1, ... % 线宽'XGrid', 'off', 'YGrid', 'on', ... % 网格'TickDir', 'out', 'TickLength', [.01 .01], ... % 刻度'XMinorTick', 'off', 'YMinorTick', 'off', ... % 小刻度'XColor', [.1 .1 .1], 'YColor', [.1 .1 .1]) % 坐标轴颜色% 坐标轴刻度调整set(gca, 'YTick', 0:0.3:1.5,...'Ylim' , [0 1.5], ...'XTick', 1:13,...'Xticklabel',{1:13},...'Yticklabel',{0:0.3:1.5})% legendhLegend = legend([GO(1),GO(2),GO(3),GO(4)], ...'S1', 'S2','S3','S4', ...'Location', 'northeast');% 字体和字号set(gca, 'FontName', 'Arial', 'FontSize', 10)set([hXLabel, hYLabel], 'FontName', 'Arial', 'FontSize', 11)set(hTitle, 'FontSize', 12, 'FontWeight' , 'bold')% 背景颜色set(gcf,'Color',[1 1 1])
设置完毕后,以期刊所需分辨率、格式输出图片。
%% 图片输出figW = figureWidth;figH = figureHeight;set(figureHandle,'PaperUnits',figureUnits);set(figureHandle,'PaperPosition',[0 0 figW figH]);fileout = 'test';print(figureHandle,[fileout,'.png'],'-r300','-dpng');

以上。



















