投稿问答最小化  关闭

万维书刊APP下载

Matlab论文插图绘制模板第96期—分组箱线图

2023/6/5 10:46:14  阅读:66 发布者:

在之前的文章中,分享了Matlab箱线图的绘制模板:

填充箱线图的绘制模板:

进一步,再来分享一下分组箱线图的绘制模板。

先来看一下成品效果:

模板中最关键的部分内容:

 

1. 数据准备

此部分主要是读取原始数据。

% 读取数据

load data.mat

 

2. 颜色定义

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

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

这里直接使用TheColor配色工具中的XKCD配色库:

%% 颜色定义

C1 = TheColor('xkcd',426);

C2 = TheColor('xkcd',331);

 

3. 分组箱线图绘制

通过调用两次boxplot’命令,绘制初始分组箱线图。

box_1 = boxplot(group1,'positions',p1,'Colors',C1,'Widths',0.7,'Symbol','o');

box_2 = boxplot(group2,'positions',p2,'Colors',C2,'Widths',0.7,'Symbol','o');

hTitle = title('Miles per Gallon by Vehicle Origin');

hXLabel = xlabel('Samples');

hYLabel = ylabel('Miles per Gallon (MPG)');

 

4. 细节优化

为了插图的美观,对箱线图线宽线型属性进行调整:

% 线宽线型

set([box_1,box_2],'LineWidth',1.5)

set([box_1(1:2,:),box_2(1:2,:)],'LineStyle','-')

然后,对坐标轴细节等进行美化:

% 坐标轴美化

set(gca, 'Box', 'off', ...                                        % 边框

         'LineWidth', 1,...                                       % 线宽

         'XGrid', 'off', 'YGrid', 'on', ...                       % 网格

         'TickDir', 'out', 'TickLength', [.015 .015], ...         % 刻度

         'XMinorTick', 'off', 'YMinorTick', 'off', ...            % 小刻度

         'XColor', [.1 .1 .1],  'YColor', [.1 .1 .1])             % 坐标轴颜色

set(gca, 'XTick', (p1+p2)/2,...

         'XTickLabel', {'Sample1','Sample2','Sample3','Sample4'},...

         'Xlim',[-0.5 11.5],...

         'Ylim',[0 6]);

% legend

lg = findobj(gca,'Tag', 'Box');

hLegend = legend([lg(5),lg(1)],...

                 'Group1','Group2');

% 字体和字号

set(gca, 'FontName', 'Arial', 'FontSize', 9)

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');

以上。

转自:“阿昆的科研日常”微信公众号

如有侵权,请联系本站删除!


本文评论

暂无相应记录!

首页<<1>>尾页共0页共0条记录
  • 万维QQ投稿交流群    招募志愿者

    版权所有 Copyright@2009-2015豫ICP证合字09037080号

     纯自助论文投稿平台    E-mail:eshukan@163.com