You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何减少MATLAB中boxchart空白区域?解决exportgraphics透明报错

解决MATLAB箱线图(boxchart)空白区域过大及背景透明失效问题

问题描述

尝试最小化MATLAB中箱线图的空白区域,使用exportgraphics()设置透明背景时出现以下警告:

Warning: Background transparency is not supported; using white instead.

无法实现减少空白的需求,相关代码如下:

clc; clear; close;

% data
D1= [0.944790802    0.557698361 0.373906779 2.046704837 2.301581619 1.527444403 1.209060357 0.255009648 0.006522648 1.391635043 0.020485623 3.06E-05    2.219570211 0.031292703 1.4617617   0.709825512 0.140968679 0.39152202  0.118959281 0.604265713 0.108340057 1.254792134 0.187867762 0.391214936 0.527749647 0.284452271 0.137486525 1.50698204  0.398872235 1.09E-05    6.030073741 0.343347524 2.66E-05    0.244522229 1.828567513 0.384888011 0.150066426 0.458832813 0.039189943 1.356957639 0.162054596 0.547751882 3.835735492 0.038394762 0.661566595 0.13553248  0.270139424 1.451613168 0.044339379 0.413170264 1.795609121 0.109564539 0.201333355 1.207746569 0.812164457 0.003902586 3.508953738 0.024859534 1.193314027 ]';
D2= [0.523944609    1.423099939 1.869691969 0.920446948 1.977619794 0.781737256 0.95977679  1.100075857 0.017899515 4.533151907 0.03790224  2.80E-05    3.432894823 0.120809324 2.359138901 1.530822669 0.24903375  3.017043435 0.357091919 1.775763839 0.067044341 2.383827733 0.496209584 1.016302313 2.17366118  0.718760335 0.316183758 1.662342228 0.658886019 1.57E-05    1.798207987 2.194711665 3.12E-05    0.777365181 1.31022147  1.803898883 0.142538755 1.633823624 0.095730818 1.917168272 1.066430117 1.570445025 0.67080804  0.028328869 0.265164055 1.313050736 0.988211581 1.355540089 0.180799139 1.559261863 2.765900536 0.275343017 0.828076679 1.749404233 1.276678701 0.003919396 4.740058349 0.059366823 1.978131529 ]';
D3 = [7.503974554   2.43552972  8.907981809 10.82563814 11.09197623 18.18846472 6.084768631 5.464296776 4.067161596 14.26678079 2.792424541 0.572435047 2.733751731 3.559015982 2.505767574 5.449188972 2.786168085 12.06056623 9.470572167 3.116171421 0.458594772 0.434307287 3.447147643 5.200432201 6.18563517  19.19753749 3.656691267 4.967834173 10.29317107 2.121656326 1.394653476 2.790630109 3.152455521 2.082149563 5.232312376 1.259368399 6.269558563 3.251263155 3.898594351 10.54554845 10.20258999 6.661139665 4.464739104 1.665067753 8.608715167 8.084037724 2.402608378 7.60967001  7.554079841 6.05757598  3.768961429 3.502224334 1.027846476 1.537041705 17.67476646 1.538015623 15.93866248 6.015687282 6.338130064 ]';
data = [D1 D2 D3];

% Requires R2019b or later
t = tiledlayout(2,2, 'Padding', 'none', 'TileSpacing', 'compact'); 

%box chart 
b=boxchart(data);

b.BoxFaceColor = [0 0.4470 0.7410];
b.WhiskerLineColor  = [0.17 0.17 0.17];
b.MarkerColor = [0 0.4470 0.7410];%black
b.JitterOutliers = 'on';
b.MarkerStyle = '.';
xlabel('x')
ylabel('y ')
title('Transparent background')

% Requires R2020a or later
exportgraphics(t,'boxPlot.jpg','BackgroundColor','none')

解决方案

1. 解决背景透明警告

JPG格式不支持透明通道,若需要透明背景,需改用PNG格式导出:

exportgraphics(t,'boxPlot.png','BackgroundColor','none')

2. 进一步消除空白区域

除了设置tiledlayout的Padding为none和TileSpacing为compact,还可以添加以下操作:

  • 调整坐标轴的LooseInset参数,消除轴周围的默认空白:
ax = gca;
ax.LooseInset = [0 0 0 0];
  • 若允许隐藏非必要元素(如标题、坐标轴标签),可直接清空它们以节省空间:
title('');
xlabel('');
ylabel('');
  • 调整图形窗口尺寸,让内容填满窗口:
fig = gcf;
fig.Position = [100 100 ax.Position(3)*1.1 ax.Position(4)*1.1];

3. 优化后的完整代码

将上述修改整合后的代码如下:

clc; clear; close;

% data
D1= [0.944790802    0.557698361 0.373906779 2.046704837 2.301581619 1.527444403 1.209060357 0.255009648 0.006522648 1.391635043 0.020485623 3.06E-05    2.219570211 0.031292703 1.4617617   0.709825512 0.140968679 0.39152202  0.118959281 0.604265713 0.108340057 1.254792134 0.187867762 0.391214936 0.527749647 0.284452271 0.137486525 1.50698204  0.398872235 1.09E-05    6.030073741 0.343347524 2.66E-05    0.244522229 1.828567513 0.384888011 0.150066426 0.458832813 0.039189943 1.356957639 0.162054596 0.547751882 3.835735492 0.038394762 0.661566595 0.13553248  0.270139424 1.451613168 0.044339379 0.413170264 1.795609121 0.109564539 0.201333355 1.207746569 0.812164457 0.003902586 3.508953738 0.024859534 1.193314027 ]';
D2= [0.523944609    1.423099939 1.869691969 0.920446948 1.977619794 0.781737256 0.95977679  1.100075857 0.017899515 4.533151907 0.03790224  2.80E-05    3.432894823 0.120809324 2.359138901 1.530822669 0.24903375  3.017043435 0.357091919 1.775763839 0.067044341 2.383827733 0.496209584 1.016302313 2.17366118  0.718760335 0.316183758 1.662342228 0.658886019 1.57E-05    1.798207987 2.194711665 3.12E-05    0.777365181 1.31022147  1.803898883 0.142538755 1.633823624 0.095730818 1.917168272 1.066430117 1.570445025 0.67080804  0.028328869 0.265164055 1.313050736 0.988211581 1.355540089 0.180799139 1.559261863 2.765900536 0.275343017 0.828076679 1.749404233 1.276678701 0.003919396 4.740058349 0.
相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.11 23:51:15