Align axis labels nicely in parallel with axes in MATLAB (3-D) plots.


Updated 23 Dec 2017

From GitHub

View License on GitHub

Editor's Note: This file was selected as MATLAB Central Pick of the Week

This is a simple MATLAB function for axis label alignment. It still works when Projection mode is Perspective or when DataAspectRatio is not [1 1 1].
## Functions
- `align_axislabel`: it rotates $x$, $y$ and $z$ labels to the direction of their corresponding axes and moves the labels to a proper distance from the axes;
- `align_axislabel_log`: supports log-scale in $z$ axis;
- `axislabel_rotation`: it performs rotation only;
- `axislabel_rotation_angle`: it computes the angles of $x$, $y$ and $z$ labels to be rotated properly (without performing the actual rotation);
- `axislabel_translation`: it moves axis labels to a proper distance from the axes;
- `axislabel_translation_slider`: a GUI slider for setting parameters `AXISALIGN_TRANS_A` and `AXISALIGN_TRANS_B`.
## Common usage
Label alignment plus GUI slider:
z = peaks;
figure('color', [1 1 1])
set(gca, 'dataaspectratio', [1 1 0.5], 'projection', 'perspective', 'box', 'on')
xlabel('This is an x label', 'fontsize', 16, 'fontweight', 'bold', 'color', [1 0 0])
ylabel('This is a y label', 'fontsize', 16, 'fontweight', 'bold', 'color', [0 0 0])
zlabel('This is a z label', 'fontsize', 16, 'fontweight', 'bold', 'color', [0 0 1])

% Add the following after you plot your figures
h = rotate3d;
set(h, 'ActionPreCallback', 'set(gcf,''windowbuttonmotionfcn'',@align_axislabel)')
set(h, 'ActionPostCallback', 'set(gcf,''windowbuttonmotionfcn'','''')')
set(gcf, 'ResizeFcn', @align_axislabel)
align_axislabel([], gca)


See `demo.m` for more information.

Cite As

Ligong Han (2023). phymhan/matlab-axis-label-alignment (, GitHub. Retrieved .

MATLAB Release Compatibility
Created with R2016a
Compatible with any release
Platform Compatibility
Windows macOS Linux

Inspired by: Align axes labels in 3D plot

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Versions that use the GitHub default branch cannot be downloaded

Version Published Release Notes

- updates examples

- Adds GUI slider for parameter setting (controlling translation behaviour)

- Adds check for class of 'ax' in align_axislabel.m
- Adds a new example where axis labels are updated/aligned as you rotate the plot simultaneously
- Adds example for subplots
- Changes align_axislabel.m back to align_axislabels.m, sorry for the confusion
- Fixes typos in examples
- Log scale on z axis

- Revised for R2014b
- Adds a trans_mode option

- Modified the method to move labels to avoid potential overlap;
- Modified parameters in axis_translation.

- Modified the direction of axis label translation

- Added a function for translation
- Updated descriptions

To view or report issues in this GitHub add-on, visit the GitHub Repository.
To view or report issues in this GitHub add-on, visit the GitHub Repository.