Matlab进阶绘图第65期—带分组折线段的柱状图

发布于:2024-07-28 ⋅ 阅读:(144) ⋅ 点赞:(0)

带分组折线段的柱状图是在原始柱状图的基础上,在每组柱状图位置处分别添加折线段,以进行对比或添加额外信息

由于Matlab中未收录带分组折线段的柱状图的绘制函数,因此需要大家自行设法解决

本文使用自制的BarwithGroupedLine小工具进行带分组折线段的柱状图的绘制,先来看一下成品效果:

特别提示:本期内容『数据+代码』已上传资源群中,加群的朋友请自行下载。有需要的朋友可以关注同名公号【阿昆的科研日常】,后台回复关键词【全家桶】查看加入方式


1. 数据准备

此部分主要是读取原始数据,并初始化绘图参数

% 读取数据load data.mat% 初始化绘图参数X = x;Y = dataset;YL = linedata;

2. 颜色定义

作图不配色就好比做菜不放盐,总让人感觉少些味道。

但颜色搭配比较考验个人审美,需要多加尝试。

这里直接使用TheColor配色工具中的SCI权威配色库

%% 颜色定义C = TheColor('sci',2040,'map',5);C = flipud(C);

3. 带分组折线段的柱状图绘制

调用‘BarwithGroupedLine’函数,绘制初始带分组折线段的柱状图

[GO,L] = BarwithGroupedLine(X,Y,YL,C);hTitle = title('Bar with Grouped Lines');hXLabel = xlabel('Samples');hYLabel = ylabel('RMSE (m)');

4. 细节优化

为了插图的美观,对坐标轴细节等进行美化

%% 细节优化% 线型调整for i = 1:length(L)    L(i).Color = 'k';    L(i).Marker = "o";    L(i).LineWidth = 0.5;end% 坐标区调整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.1:1,...         'Ylim' , [0 0.4], ...         'XTick',1:3,...         'Xticklabel',{'samp1' 'samp2' 'samp3'},...         'Yticklabel',{0:0.1:1})% Legend hLegend = legend([GO,L(1)], ...                 'A', 'B', 'C','D', 'E','REF', ...                 'Location', 'eastoutside', ...                 'Box','off');% 字体和字号set(gca, 'FontName', 'Arial', 'FontSize', 10)set([hLegend, hXLabel, hYLabel], 'FontSize', 11, 'FontName', 'Arial')set(hTitle, 'FontSize', 12, 'FontWeight' , 'bold')% 背景颜色set(gcf,'Color',[1 1 1])% 添加上、右框线xc = get(gca,'XColor');yc = get(gca,'YColor');unit = get(gca,'units');ax = axes( 'Units', unit,...           'Position',get(gca,'Position'),...           'XAxisLocation','top',...           'YAxisLocation','right',...           'Color','none',...           'XColor',xc,...           'YColor',yc);set(ax, 'linewidth',1,...        'XTick', [],...        'YTick', []);

设置完毕后,以期刊所需分辨率、格式输出图片。

%% 图片输出figW = figureWidth;figH = figureHeight;set(figureHandle,'PaperUnits',figureUnits);set(figureHandle,'PaperPosition',[0 0 figW figH]);fileout = 'test';print(figureHandle,[fileout,'.png'],'-r300','-dpng');

以上。


网站公告

今日签到

点亮在社区的每一天
去签到