File Exchange

image thumbnail

Align axes labels in 3D plot

version (2.21 KB) by Matthew Arthington
Align the x, y and z labels of the current axes with the x, y and z axes for 3D plots.


Updated 30 Jan 2012

View Version History

View License

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

If the current axes have been rotated away from the X-Y view, this function will calculate the angles of the current x, y and z axes with respect to horizontal. These angles are used to rotate the current x, y and z axes labels to align them with their respective directions.

This only works for equal sized axes (axis equal) and orthographic projection.

Cite As

Matthew Arthington (2021). Align axes labels in 3D plot (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (20)


Work well. Thank you.

Some issues,
1) Doesn't work properly with axes direction property (e.g. ax.YDir = ax.YAxis.Direction) set to 'reverse' instead of default 'normal'
2) Labels can overlap with axis ticks (z axis). Need more spacing. Workaround is to make the ticks shorter.

Eric LePage

Apology. The problem is semantics. "Align" is a bad word because it creates the high expectation that the text will always lie parallel to the axis at whichever angle the axis occurs. The labels sort-of follow the start of the axis, and are kept upright in order that the text itself is not slanted. In my case I have been using a larger font for publication purposes, and the position of the x and y labels are inconsistently dissimilar in relation to the axes. Reasonable effort therefore. (This is the kind of capability which the user has a right to expect that Mathworks would have done properly from the outset).

Eric LePage

Does not work. There needs to be better description of prerequisite conditions. e.g. most generally for 3D plots the axes will be different parameters with different ranges. If the only circumstances for which this works is that the parameters are the same and plotted isometrically, then the routine is not going to be much use for most users. Also the callback needs to work with "rotate3D on" so that they stay aligned whichever way the plot is dragged. If that is what is intended, it needs more work. (Incidentally in the example plot above does not seem to be axis equal).

Paal Skjetne

Does not work

Haibo Huang

Allan Albuquerque

Thank you for the try, but this function is not working. My axis are x=[0:0.2:0.8] and y=[6:1:10]. Then, both axis have 5 elements (equal). When I run nothing happen.

Fabio Furcas


Do not work. Fails when axis not equal.

Siddharth Maddali

Prefect. Thanks!

Changjie Guan

Very useful function!

Matthew Arthington


"Tools For Axis Label Alignment In 3 D Plot" was inspired by this function. Please look at that submission to see if it can do what you need.


Hi, this does work with "axis equal" on. Any idea on how to make it work with the z axis set to automatic?

George Shoane

Fantastic program. It works!


alex harijanto

Mehdi Moghaddam

that was perfect...just has a typing mistake.... instead of axis equal you have written axes equal....thnx

Matthew Arthington


Yes, it does, and I've updated it so that it also works with Camera Roll too. Hope this helps.


does this work if you rotate using orbit camera from cameratoolbar?

Matthew Arthington

From the author: The labels should appear in the same place for the same view, but Matlab decides where that place is according to the current spacing.

I've looked into making this work without "axis equal" having been called (equal sized axes) and I'm stumped as to how to figure out the stretch of the plotted axes when they're not equal. Any help would be appreciated.

Rob Campbell

Useful. The only problem is that if you rotate and re-align a few times then it's possible for the labels to drift away from the centre of the axes. It would be nice if the label were re-centred as well as aligned.

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

Inspired: phymhan/matlab-axis-label-alignment

Community Treasure Hunt

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

Start Hunting!