To eliminate the large invisible white border around a MATLAB figure without cropping your axes, labels, or titles, use the modern exportgraphics(gcf, 'output.png', 'Padding', 'tight') function when saving files. For on-screen figures and multi-panel plots, replace legacy subplot with tiledlayout('Padding', 'compact') or programmatically align the axes coordinates with ax.TightInset.
Method 1: Clean Figure Export with exportgraphics (Recommended)
In modern MATLAB (R2020a and newer), exportgraphics automatically crops out excessive empty margins while preserving all tick marks, axis labels, legends, and colorbars without any distortion:
% Generate a sample plot
figure('Color', 'w');
x = linspace(0, 10, 500);
plot(x, sin(x), 'b-', 'LineWidth', 2);
xlabel('Time (s)');
ylabel('Amplitude (V)');
title('Damped Harmonic Response');
grid on;
% Export with ZERO excess white border at publication quality (300 DPI)
exportgraphics(gcf, 'Clean_Figure_Tight.png', 'Resolution', 300, 'Padding', 'tight');
% Export as vector PDF for LaTeX / Word papers
exportgraphics(gcf, 'Clean_Figure.pdf', 'ContentType', 'vector', 'Padding', 'tight');
Method 2: Multi-Panel Plots with tiledlayout (Replaces Subplot Margins)
The legacy subplot() command forces large fixed margins between tiles. Replace it with tiledlayout() to control outer padding and interior spacing directly:
figure('Color', 'w');
% Create a 2x2 grid with tight padding and compact tile spacing
t = tiledlayout(2, 2, 'Padding', 'compact', 'TileSpacing', 'compact');
% Tile 1
nexttile;
plot(sin(1:100)); title('Plot 1');
% Tile 2
nexttile;
plot(cos(1:100)); title('Plot 2');
% Tile 3
nexttile;
plot(tan(1:100)); title('Plot 3');
% Tile 4
nexttile;
plot(exp(linspace(0, 1, 100))); title('Plot 4');
% Shared labels across layout
xlabel(t, 'Normalized Time Index');
ylabel(t, 'Response Values');
% Export without outer border
exportgraphics(gcf, 'Tiled_Layout_Tight.png', 'Padding', 'tight');
Method 3: Programmatic On-Screen Maximization via TightInset
If you need the figure canvas to fill the entire window while viewing it interactively on-screen, calculate the exact inner margin using the axes TightInset property:
figure('Color', 'w');
x = 0:0.1:10;
plot(x, cos(x), 'r', 'LineWidth', 1.8);
xlabel('X-Axis Label');
ylabel('Y-Axis Label');
title('Maximized Canvas');
% Extract the axes object and calculate exact label insets
ax = gca;
outerpos = ax.OuterPosition;
ti = ax.TightInset;
% Adjust Position so axes expand precisely to the border of the labels
left = outerpos(1) + ti(1);
bottom = outerpos(2) + ti(2);
ax_width = outerpos(3) - ti(1) - ti(3);
ax_height = outerpos(4) - ti(2) - ti(4);
ax.Position = [left, bottom, ax_width, ax_height];
Method 4: Removing Outer Border for Legacy saveas / print
If you are using legacy scripts where exportgraphics is not supported, strip the loose insets manually before saving:
ax = gca;
set(ax, 'LooseInset', get(ax, 'TightInset'));
set(gcf, 'PaperPositionMode', 'auto');
print('-dpng', '-r300', 'legacy_tight_figure.png');
Summary Comparison of Border Removal Methods
| Method | Primary Use Case | Advantages |
|---|---|---|
exportgraphics(..., 'Padding', 'tight') |
Saving images (PNG, PDF, EPS, TIFF) for reports or websites. | Fastest & Most Accurate; automatically prevents label clipping and eliminates invisible padding. |
tiledlayout('Padding', 'compact') |
Multi-plot dashboards and subplot grids. | Removes dead white space between panels and coordinates shared titles. |
ax.Position = [TightInset Calculations] |
On-screen figure window maximization. | Expands the plot canvas to fill the display window dynamically. |
set(gca, 'LooseInset', ...) |
Legacy MATLAB (R2019b and older). | Eliminates default figure padding when using print. |
Need a Custom Version or Complete Simulation for This Problem?
Our 500+ PhD engineers build, debug, and optimize working MATLAB scripts and Simulink (.slx) models tailored to your exact assignment rubrics with zero plagiarism.
Explore similar technical troubleshooting questions and verified MATLAB solutions: