How can I rotate the axis tick labels in 2-D and 3-D?

27 views (last 30 days)
I would like to rotate the axis tick labels in 2-D and 3-D.

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 5 May 2016
For MATLAB releases before R2014b, below is an example that demonstrates how to rotate the X-tick labels in MATLAB :
% Create some data:
X = 1:12;
Y = rand(1,12);
% Generate a plot
% Set the tick locations and remove the labels
% Define the labels
lab = [{'January'};{'February'};{'March'};{'April'};{'May'};{'June'};...
% Estimate the location of the labels based on the position
% of the xlabel
hx = get(gca,'XLabel'); % Handle to xlabel
pos = get(hx,'Position');
y = pos(2);
% Place the new labels
for i = 1:size(lab,1)
t(i) = text(X(i),y,lab(i,:));
Following is a simple example of how to rotate an x-label in 3-D:
h = get(gca,'xlabel');
Alternatively, you can use this user created File Exchange submission that can perform the same task:
Please note that the above File Exchange file was not authored by MathWorks. Any questions regarding this file should be directed to the contributing author of that file.
NOTE: If you are working under the MS Windows version of MATLAB, you will need to make sure that you are using a Truetype font in your text objects. Truetype are the only fonts which can be rotated correctly on the screen and on printouts.
Liwei Dai
Liwei Dai on 4 Dec 2015
Have you tried out his code? Actually, it rotates the 'XTickLabel', check the loop part.

Sign in to comment.

More Answers (3)

daniel on 12 Feb 2015
if you have 2014b you can do this:
ax = gca;
ax.XTickLabelRotation = -45;

Alexey on 6 Jan 2016
Edited: MathWorks Support Team on 4 Nov 2021
or (I think starting with 2014) you can just do
as per docs

Abdullah Caliskan
Abdullah Caliskan on 12 Apr 2016
Another way
bar(1:3 ,[0.5 0.3 0.2])

Community Treasure Hunt

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

Start Hunting!