MATLAB-panel-builder
helps you compose publication-quality scientific figure panels with precise and repeatable layouts in MATLAB. The shared precise layout lets you align panels perfectly into complete figures by simply stacking them vertically or horizontally. MATLAB-panel-builder
can thus be combined with TikZ for stacking panels into complete figures with a creation pipeline that is fully reproducible and under version control in Git.
- π Precise Layout Control: Define panel dimensions in centimeters for exact sizing
- π¨ Consistent Styling: Maintain uniform fonts, margins, and aesthetics across panels
- π Flexible Panel Composition: Easy vertical and horizontal stacking of panels
- π Reproducible Workflow: Version-controlled figure creation pipeline
- π― Publication-Ready: Optimized for scientific publication requirements
-
Add the matlab-panel-builder directory to your MATLAB path:
addpath('path/to/matlab-panel-builder');
-
Or place the
+PanelBuilder
package directory in your working directory.
% Configure panel dimensions, margins, and axes separation
config = struct();
config.panel = struct();
config.panel.dimensions = struct('widthCm', 10, 'heightCm', 8);
config.panel.margins = struct('leftCm', 1.5, 'bottomCm', 1.5, 'rightCm', 0.5, 'topCm', 0.5);
config.panel.axesSeparation = struct('xCm', 1.0, 'yCm', 1.0);
% Define style properties that can be set via groot
config.style = struct();
config.style.properties = struct();
config.style.properties.defaultAxesFontSize = 8;
PanelBuilder.configure(config);
PanelBuilder.setStyle();
% Create a simple panel
[fig, axs] = PanelBuilder.createPanel();
ax = axs{1,1};
plot(ax, 1:10, rand(1,10));
% Create a 2x2 grid of panels
[fig, axs] = PanelBuilder.createPanel('rows', 2, 'cols', 2);
% Plot in different panels
plot(axs{1,1}, 1:10, rand(1,10));
scatter(axs{1,2}, 1:10, rand(1,10));
bar(axs{2,1}, 1:5, rand(1,5));
histogram(axs{2,2}, randn(100,1));
For advanced configuration scenarios, the configure()
function also supports special operators to modify the existing configuration:
% Initial configuration (width = 10 cm, font size = 8)
config = struct();
config.panel = struct('dimensions', struct('widthCm', 10));
PanelBuilder.configure(config);
% Increase width by 2 cm (now 12 cm)
configUpdate = struct();
configUpdate.panel = struct('dimensions', struct('widthCm', '+=2'));
PanelBuilder.configure(configUpdate);
Supported operators:
"+=X"
: Add X to current value"-=X"
: Subtract X from current value"*X"
: Multiply current value by X"=X"
: Set value to X
Note: Override operators are not supported for style.properties
fields. Properties only support direct assignment since default values cannot be defined for all possible groot properties.
Extra features include wrappers for systematically aligning legends, scale bars, colorbars, and annotations. In addition, the package includes a feature for placing a grid over the whole panel to verify that all elements have their intended position.
% Add corner annotations with optional background colors
PanelBuilder.Features.addAnnotation(ax, 'TEXT', 'loc', 'northwest');
% Add X and Y scale bars
PanelBuilder.Features.drawXScaleBar(ax, 1, '1 cm'); % 1 cm scale bar
PanelBuilder.Features.drawYScaleBar(ax, 2, '2 cm'); % 2 cm scale bar
% Add legend with automatic styling and positioning
plot(ax, x, y1, 'DisplayName', 'Series 1');
plot(ax, x, y2, 'DisplayName', 'Series 2');
leg = PanelBuilder.Features.addLegend(ax, 'Location', 'northeast');
% Add colorbar in different positions
scatterHandle = scatter(ax, x, y, [], colors, 'filled');
cbar = PanelBuilder.Features.addColorbar(ax, scatterHandle, 'right');
% Add gridlines to verify positioning (0.5 cm resolution by default)
PanelBuilder.Features.drawGridlines(fig);
+PanelBuilder/ % Main package namespace
βββ configManager.m % Central configuration management
βββ configure.m % Public configuration interface
βββ createPanel.m % Core panel creation with grid layouts
βββ getConfig.m % Configuration retrieval
βββ resetConfig.m % Reset configuration to defaults
βββ setStyle.m % Apply styling to graphics root
βββ +Features/ % Specialized features
β βββ addAnnotation.m % Panel corner annotations
β βββ addColorbar.m % Colorbar positioning
β βββ addLegend.m % Legend styling and positioning
β βββ drawGridlines.m % Debug grid overlay
β βββ drawXScaleBar.m % Horizontal scale bars
β βββ drawYScaleBar.m % Vertical scale bars
βββ +Helpers/ % Utility functions
βββ adjustAxesSize.m % Axes dimension adjustments
βββ applyLegendStyle.m % Legend style application
βββ centerAxes.m % Axes centering utilities
βββ cmToAxesRel.m % CM to axes relative coordinates
βββ cmToFigRel.m % CM to figure relative coordinates
βββ cmToPixels.m % CM to pixel conversion
βββ copyAxes.m % Axes copying utilities
βββ createFullFigureAxes.m % Full-figure axes creation
βββ ptToCm.m % Point to centimeter conversion
βββ validateAxesUnits.m % Axes units validation
βββ validateFigureUnits.m % Figure units validation
examples/ % Usage examples and templates
βββ exampleConfig.m % Complete configuration template
βββ usageExample.m % Comprehensive feature demonstration
tests/ % Unit tests
βββ testConfigureOverride.m % Configuration override tests
βββ testHelperFunctions.m % Helper function tests
docs/ % Additional documentation
βββ STYLEGUIDE.md % Code style guidelines and conventions
assets/ % Logo and visual assets
βββ MATLAB-panel-builder logo.png % Project logo
See examples/usageExample.m
for a comprehensive demonstration that shows:
- 2Γ2 panel creation with precise 11Γ11 cm dimensions
- Complete configuration setup (dimensions, margins, styling)
- All annotation positions (NW, NE, SW, SE) with background colors
- Y-axis and X-axis scale bars with custom labels
- Colorbars in all 4 positions (left, right, top, bottom)
- Debug gridlines for visual validation
- Publication-ready styling with consistent fonts
% Run the comprehensive usage example
run('examples/usageExample.m');
To ensure that panels maintain their intended dimensions when saved, it is recommended to use the export_fig
package. MATLAB's default figure saving functions may not preserve the precise centimeter-based dimensions that PanelBuilder is designed around.
% Save with export_fig to preserve dimensions
export_fig(fig, 'panel.pdf', '-pdf');
export_fig(fig, 'panel.png', '-png', '-r300'); % 300 DPI for high quality
We welcome contributions! Please follow these steps:
- Fork the repository
- Create a feature branch (
git checkout -b feature/amazing-feature
) - Make your changes following the style guidelines
- Commit your changes (
git commit -m 'Add amazing feature'
) - Push to the branch (
git push origin feature/amazing-feature
) - Open a Pull Request
This project is released under the MIT License.