Code covered by the BSD License  

Highlights from
Rotate X-axis tick labels

5.0

5.0 | 5 ratings Rate this file 134 Downloads (last 30 days) File Size: 4.59 KB File ID: #27812
image thumbnail

Rotate X-axis tick labels

by Ben Tordoff

 

01 Jun 2010 (Updated 21 Feb 2012)

Rotate x-tick labels to any angle, preserving font settings and coping with resize, zoom, pan etc

| Watch this File

File Information
Description

This function rotates the x-tick labels on a plot. An arbitrary angle can be specified for the text and the label justification adjusts appropriately to ensure the labels lie below the ticks. The axes font properties are preserved in the labels, which will update in response to font changes. Changes to the axes limits are also handled.

Main features:
* Arbitrary angle of rotation
* Justification adjusts appropriately for the specified angle
* Axes font property changes are reflected
* Labels move correctly when zooming and panning

As with other functions that achieve the same result, this works by replacing the xtick labels with text objects. Handles to the created text objects are returned for you to tinker with.

Acknowledgements

The author wishes to acknowledge the following in the creation of this submission:
Rotate XTick Labels for multi-axes

MATLAB release MATLAB 7.10 (2010a)
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (24)
27 Sep 2010 Suresh Joel

Some function arguments missing? (line 256 col 33)

29 Sep 2010 Dan Glaser  
29 Sep 2010 Dan Glaser

This works beautifully, but when it is used with a plot that has a very large number of x tick labels (1000 for example) it does not position them correctly.

Initially of course in this case, they labels are all overlapping, but after zooming it can be seen that they do not position equally and some are directly on top of each other.

Does anyone have any suggestions?

30 Sep 2010 Ben Tordoff

Hi Dan. Having done some testing, it looks like text object positions are only stored to the nearest pixel. This means that when you call rotateXLabels, any labels that are drawn at the same pixel location will store their position as the same value. This seems to be a limitation of MATLAB's normalised position units or at least a restriction imposed when you switch to them. I will see if it's possible to achieve the same label behavior without using normalised coordinates as this may fix your problem. Watch this space...

Cheers. Ben

01 Oct 2010 Dan Glaser

Thanks Ben,
I appreciate you looking into this!
-Dan

06 Oct 2010 Ben Tordoff

Hi Dan. I'm hoping this latest update will have fixed your problem. Please let me know if it doesn't.

Cheers. Ben

06 Oct 2010 Dan Glaser

Thanks, the positioning now works great.

31 Jan 2011 Denzel Li

Hi Ben:
Thanks for the great tool. However it only produces xticklabels which is at the bottom and not on the top. That is, even with the function call:
set(gca, 'XAxisLocation', 'top');
the xticklabels are still at the bottom. Please note that get(gca,XAxisLocation') returns 'top'.
Please let me know if anyone has any suggestion. Thanks.

Best,
Denzel

01 Feb 2011 Ben Tordoff

Hi Denzel,
this hack works by replacing the tick labels with text objects and removing the existing tick labels. This means that setting properties of the tick labels (fonts, position etc) will no longer have any effect. I'll have a look and see if I can make them honour the "XAxisLocation" property, but I suspect that even then you will need to set it before rotating the labels. Watch this space...

Ben

08 Feb 2011 Denzel Li

Hi Ben:
Thanks for your quick reply. Having xticklabels on top and yticklabels on right are especially useful when displaying heatmap with labels. Please keep me updated. Thanks.

11 Feb 2011 Ben Tordoff

Hi Denzel, my first attempt at getting this to work is on its way through the approval process now. You should see it arrive on here in the next day or two.

14 Feb 2011 Miranda

Hi Ben,
Thank you for this nice program. One disadvantage is that the labels are very close to the x-axes, especially if I choose an angle of -90 or 90. I thought of including an extra space in the labels:
if 0 <= angle && angle < 180
  labels = cellfun(@(x) horzcat(x,' '),labels,'UniformOutput', false);
else
  labels = cellfun(@(x) horzcat(' ',x),labels,'UniformOutput', false);
end
Maybe an extra if-expression is needed for the labels on XAxisLocation top...
Miranda

29 Mar 2011 Denzel Li

Hi Ben and Miranda:
The program is cool, especially comparing to other rotation programs which may not adjust the text object position well. Also the code style is clear.
For my previous request of setting the axis to the top, I just manually set the y to a proper value in creatNewLabels(...) and it works.

04 Aug 2011 Thijs

First rotate script I came across that works without issues (yet ;) ) on subplots.

Thank you!

12 Oct 2011 Wolfgang

Hi Ben,

This works fine with me, but the x-Axis labels are not printed into a png-file via
set(gcf, 'PaperPositionMode', 'auto');
print('-dpng','-r90','bild.png');

Can you reproduce this, and do you have a solution.

12 Oct 2011 Ben Tordoff

Hi Wolfgang, I've just tried this using the example in the help and printing using the lines you provided and the labels appear OK. Could you provide a more detailed example that shows the problem? There may be some setting of your figure that makes a difference.

Cheers. Ben

12 Oct 2011 Chris Sparrow

I get the following error:
>> rotateXLabels( gca(), 22 )
??? Error: File: rotateXLabels.m Line: 310 Column: 33
Unexpected MATLAB operator.

line 310 is function onAxesFontChanged( ~, ~ )

it will work if I commnet out the functions having ( ~, ~ )
any thoughts why I have this error?

thanks

13 Oct 2011 Ben Tordoff

Hi Chris, are you by any chance using a version of MATLAB older than R2010a?

If so, you may still be able to get it to work by replacing the '~' with a real variable name, but bear in mind this hasn't been developed or tested on older releases.

Cheers. Ben

13 Oct 2011 Chris Sparrow

yes I am using an old version of Matlab - thanks for the suggestion. Great script btw!

27 Jan 2012 Pablo  
07 Feb 2012 Johan Carlson

Hi,
On screen everything looks fine. However, when I try to print, the tick labels disappear, same as for Wolfgang. I'm running MATLAB 2011b (7.13.0.564) on Windows 7 (x64 version). All plot settings are at the default.

Other than that, great function!
/Johan

Here's the test script that used:

%------------------
clear all
close all
clc

x = 1:0.2:2.8;
y = x.^2;
plot(x,y);
xlab = get(gca,'XTickLabel');
xlab = num2str(xlab);
set(gca,'XTickLabel',xlab);
xlabel('X axis label');
ylabel('Y axis label');
title('Plot title');
rotateXLabels(gca, 45)
set(gcf, 'PaperPositionMode', 'auto');
print('-dpng','plot.png');

21 Feb 2012 Ben Tordoff

Hi Johan, I can see the problem. I am using "normalized" coordinates for the text used as the labels. It seems that with the units set to normalized they don't print, but if I set them to something else they do. I have no idea why this would be.

I will see if I can re-arrange the code to use something other than normalized units without breaking something else (zooming, panning, titles, etc).

:-(

Ben

21 Feb 2012 Ben Tordoff

OK, I've submitted an update that will hopefully fix the printing issues. Please keep a backup of the old version in case this change breaks something else (it passes all my tests, but they probably don't cover all uses). The update should appear here in the next few days. When it does, let me know if it helped.

Cheers.
Ben

29 Feb 2012 Jacques Pecreaux

Worked perfectly in 2 minutes.
A great and user-friendly file.
Some warnings about latex markups while using no interpreter but it's harmless so far
Thanks for sharing!

Please login to add a comment or rating.
Updates
30 Sep 2010

Switch to using normalized units for labels to prevent loss of accuracy with hundreds/thousands of labels.

07 Feb 2011

Should now honour the "XAxisLocation" property of the axes.

21 Feb 2012

Switch to using data coordinates internally to allow printing to work. Please keep the old version in case this breaks something else!

Tag Activity for this File
Tag Applied By Date/Time
plotting Ben Tordoff 03 Jun 2010 11:46:05
xtick Ben Tordoff 03 Jun 2010 11:46:05
xtick Salvador Moreno 09 Jul 2010 03:57:35
plotting shahar hirshfeld 21 Jul 2010 05:46:47
xtick shahar hirshfeld 21 Jul 2010 05:46:50
plotting Suresh Joel 27 Sep 2010 18:15:26
xtick Arturo Serrano 22 Oct 2010 08:26:19
plotting Qaisar 13 Aug 2011 13:53:19
plotting Me Sunrise 02 Nov 2011 01:48:25
xtick Me Sunrise 02 Nov 2011 01:48:30
plotting alberto akel 12 Dec 2011 15:16:52
xtick alberto akel 12 Dec 2011 15:16:54
plotting Guido 16 Apr 2012 05:55:30
xtick Guido 16 Apr 2012 05:55:35

Contact us at files@mathworks.com