Skip to content

ala-laurila-lab/matlab-panel-builder

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

1 Commit
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

mpl-panel-builder logo

Create publication-quality scientific figure panels with a consistent layout

License: MIT

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.

Features

  • πŸ“ 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

Installation

  1. Add the matlab-panel-builder directory to your MATLAB path:

    addpath('path/to/matlab-panel-builder');
  2. Or place the +PanelBuilder package directory in your working directory.

Basic usage

Quick Start

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

Multi-panel Layouts

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

Configuration Override

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

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.

Adding Annotations

% Add corner annotations with optional background colors
PanelBuilder.Features.addAnnotation(ax, 'TEXT', 'loc', 'northwest');

Scale Bars

% 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

Legends

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

Colorbars

% Add colorbar in different positions
scatterHandle = scatter(ax, x, y, [], colors, 'filled');
cbar = PanelBuilder.Features.addColorbar(ax, scatterHandle, 'right');

Debug Gridlines

% Add gridlines to verify positioning (0.5 cm resolution by default)
PanelBuilder.Features.drawGridlines(fig);

Project Structure

+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

Examples

Complete Usage Example

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

Saving Panels

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

Contributing

We welcome contributions! Please follow these steps:

  1. Fork the repository
  2. Create a feature branch (git checkout -b feature/amazing-feature)
  3. Make your changes following the style guidelines
  4. Commit your changes (git commit -m 'Add amazing feature')
  5. Push to the branch (git push origin feature/amazing-feature)
  6. Open a Pull Request

License

This project is released under the MIT License.

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published