4.72727

4.7 | 60 ratings Rate this file 423 Downloads (last 30 days) File Size: 4.25 KB File ID: #3486
image thumbnail

XTICKLABEL_ROTATE

by

 

23 May 2003 (Updated )

Rotate XTickLabel to better accommodate long labels and text labels.

Editor's Notes:

This file was a File Exchange Pick of the Week

| Watch this File

File Information
Description

A modified version of XTICKLABEL_ROTATE90, this version allows for arbitrary angle rotation and most importantly the use of text labels. In addition, an attempt is made to adjust the size and placement of the axis and xlabel to accommodate the new labels.

  Example : %Use text labels rotated 45° without tex interpreter
   boxplot(randn(5,5),1)
   xticklabel_rotate([1:5],45,{'label_1','label_2','label_3','label_4','label_5'},'interpreter','none')

Rev.2010 due to user comments

Acknowledgements

Xticklabel Rotate90 and Hatched Fill Patterns inspired this file.

This file inspired Protein Array Tool, Yticklabel Rotate, and Rotate X Tick Labels For Multi Axes.

MATLAB release MATLAB 7.4 (R2007a)
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (79)
08 Apr 2014 Erik  
04 Mar 2014 Sabri Mutlu  
22 Jan 2014 Andreas Kraushaar

The script does not work in the sense that often the labels are not alligned, at least when saving a graph or when scaling a graph. rotateXLabels.m in http://www.mathworks.com/matlabcentral/fileexchange/27812-rotate-x-axis-tick-labels/all_files works fine for us instead.

15 Jan 2014 MoonKnight  
12 Sep 2013 Krishna

There might be small glitch,while using this rotation on a bar graph with positive and negative variations and long texts for ticks, a part of the labels ended up being positioned inside the graph. :(

09 May 2013 Scott Teuscher

when using a pareto chart which has two yticks, the yticks on the left stay the same but the yticks on the right get moved so they no longer align with the data.

08 Feb 2013 Masatoshi  
18 Aug 2012 Faezeh Safari

It works great with the xticklabel rotation! But, when I save my matlab figure as pdf the pdf file contains rotated version of my matlab figure. I have double check everything and found this function is the cause. I will appreciate if anybody can help me with this.

13 Jul 2012 Shahar Shlomi

Thanks so much!
I've downloaded the file and simply added xticklabel_rotate to my code. Magic!

06 Mar 2012 ted p teng  
21 Feb 2012 Julian

Haven't worked it hard - seemed to do what I needed out of the box - so thanks very much.

13 Oct 2011 Ziv

just what i needed. few minor issues but got the hang of it in no time.
Ziv yekutieli

08 Sep 2011 Boi-Yee Liao

thank you

02 Sep 2011 gda

When I'm calling for xticklabel_rotate function I'm getting the error
??? Error using ==> xticklabel_rotate at 78
xticklabel_rotate : can not process, either xticklabel_rotate has already been run or
XTickLabel field has been erased

Can anyone help me? thanks

27 Aug 2011 Minli

this helps a lot. thank you !!

10 Aug 2011 Corinne  
14 May 2011 Pepa  
02 May 2011 Brian Katz

I have examine the issue mentioned by Denzel Li. I have tried to reproduce the errors mentioned with only small sucess. I have found an issue with the axis position being changed when y-labels are on the right, which I will look into. I do not get such notable misalignements as you have shown when I run the same examples, so I am not sur how to respond. Please verify that these issues exist when you PRINT the figure, and it is not a simple figure screen display issue.

The text boxes are created at the tick positions, in data units. They are middle aligned, there is not much more that can be done, but I am open to suggestions.

02 May 2011 Brian Katz

It seems that the problem seen by Daniel Visser is related to the newer version of boxplot. I am not currently running v.2010 so I cannot examine this issue further at this time. Sorry.

15 Feb 2011 m s

thanks a lot.

09 Feb 2011 Pratik Chhatbar

it works!! will save a lot of my time!!! yay!!!!

08 Feb 2011 Lindsey

??? Error using ==> xticklabel_rotate at 117
xticklabel_rotate : must have same number of elements in "XTick"
and "XTickLabel"

I have checked that I have 29 ticks and 29 labels

21 Jan 2011 Denzel Li

XTICKLABEL_ROTATE is a wonderful tool. Thanks a lot for making it available. How ever, I had the same problem as mentioned by Theresa above, which results in figures hard to use for publish. I also described this issue in great detail on stackoverflow:

http://stackoverflow.com/questions/4763673/problem-or-bug-in-xticklabel-rotate-while-drawing-heatmap-and-rotating-xtick-labe

Please take a look and any suggestion will be highly appreciated.

15 Jan 2011 Ian

I got the following error when running just xticklable_rotate():
Warning: Error occurred while evaluating listener callback.
> In axis>LocSetLimits at 208
In axis at 94
In plotPhases3 at 46
??? Error using ==> set
Values must be finite and non-NaN

The function works fine when there is a single plot, maybe this is because I am using subplots?
I would really like if this function worked.

17 Dec 2010 Alecsander Eitan  
19 Oct 2010 Julian

Thanks very much, handy utility (I've not tried working it hard - seemed to do what I needed out of the box)

18 Oct 2010 Daniel Visser

Hi! Cool stuff!
Unfortunately I Have the same problem as ander and bob with:
figure; boxplot(rand(2));
set(gca,'xtick',[1 2],'xticklabel',{'Tick1' 'Tick2'})
xticklabel_rotate()
%matlab version: 7.10
The error starts at line 160: set(gca,'units','pixel')!?! (Why does the change of unit change the axis size?)
Nevertheless: Thank you so far

28 Aug 2010 Nick Mariette

Nice one. Just what I needed. Thanks Brian!

19 Aug 2010 Brian Katz

Please note that, as stated in the function and the error message above, XTickLabel_Rotate CANNOT be called multiple times on the same plot. Calling a new figure, the example works fine.

02 Jun 2010 Li

The example does not work:
boxplot(randn(5,5),1)
xticklabel_rotate([1:5],45,{'label_1','label_2','label_3','label_4','label_5'},'interpreter','none')
??? Error using ==> xticklabel_rotate at 71
xticklabel_rotate : can not process, either xticklabel_rotate has already been
run or XTickLabel field has been erased

28 May 2010 Aaron

Useful, but labels are offset (as are titles) when using multiple subplots (specifically, a 2x2 array of subplots).

26 Apr 2010 Diego Barragán  
22 Dec 2009 Benjamin

a fast and easy solution is to comment

set(gca,'position',newpos) ;

and, if necessary, set the new position manually

for me, with a few changes, it works very well

21 Dec 2009 Benjamin

I have the same Problem

Error in ==> xticklabel_rotate at ...

Seems to be a problem because I changed the XAxisLocation to 'Top'

I will check if there is a simple solution if there is solution yet.

23 Nov 2009 Theresa

Another update: when you rotate the labels they're not always lined up with each other. Super annoying and makes the figure unusable for publication....which makes the function pretty much useless to me. Sorry...seems like a good start but I've encountered too many small annoying bugs that make the figures publication unfriendly.

17 Nov 2009 bob

Problem, same as les ander:

??? Error using ==> set
Width and height must be > 0.

Error in ==> xticklabel_rotate at 168
set(gca,'position',newpos) ;

any help?

thx

15 Oct 2009 Tove  
30 Sep 2009 Theresa

Another update: using the full command works with rotations of 90 degrees for me.

i.e. xticklabel_rotate([], 90, []) works well, but xticklabel_rotate does not work. Now that I can use the full functionality of this nice program I want to update my original rating.

31 Aug 2009 Theresa

Unfortunately, I have not had the success of others with this file. When it rotates the labels, it overlays them onto the plot so they are half over the x-axis and data, and half where they should be. It looks horrible and I can't use the figures that way.

Update: The bug is in rotating 90 degrees. Other rotations work OK. But it's annoying to not have full rotation functionality.

30 Apr 2009 Salifou

Great Function. Thanks a bunch! It saved me from caving in to Stata.

11 Mar 2009 Gilles Criton

Thank you very much, so useful !

24 Feb 2009 Etsuko  
13 Feb 2009 Fredrik  
12 Feb 2009 Tue  
05 Jan 2009 Federico De Masi

THanks... tens of boxplots to process. Not automated thanks to this script.

25 Jul 2008 flicker 420

Thanks..Nice work

06 Jul 2008 M Shaw

Very helpful and well documented. Thanks!!

03 Jul 2008 Peter Pan

Sorry,
but this rating system is confusing.

The function is perfect, just a small buck.

03 Jul 2008 Peter Pan

Nice function, thanks a lot!

There is a small error, it occurs just when only one XTickLabelName is given.

For example:
bar( [ 1 ] ); xticklabel_rotate;

It claims on line 160, cause get(htext, 'extent' ) gives no cell, but an array back.
My solution:
x = get( hText, 'extent' );
if iscell( x ) == true
textsizes = cell2mat( x ) ;
else
textsizes = x;
end
with line 160 deleted works.

Any,
Thanks!

20 May 2008 Korey Y

Great function. I had to place my X-axis labels at the top and therefore have them left-aligned so I added a few lines somewhere around line 141:

xAxisLocation = get(gca, 'XAxisLocation');
if (xAxisLocation == 'top')
set(hText,'Rotation',rot,'HorizontalAlignment','left',varargin{:})
else
set(hText,'Rotation',rot,'HorizontalAlignment','right',varargin{:})
end

17 Apr 2008 Chris Lowis

Brilliant, solved my problem with a single function call using the default options.

20 Feb 2008 David K

Excellent utility. It almost needs to be part of matlab. This is so essential.

21 Dec 2007 Natalia Rivera

This is really helpful. Thank you!

03 Dec 2007 Patrick Freeman

Very useful function for displaying long labels. Good job!

01 Dec 2007 Brent Bergan

As soon as you zoom in, the tick labels do not follow appropriately. Too bad, this would have been a great function

12 Nov 2007 Greg Heins

Awesome - worked first time - like the TeX ability.

12 Nov 2007 Greg Heins

Awesome - worked first time - like the TeX ability.

31 Aug 2007 George C

just when i was about to switch to excel, i found this! MATLAB rocks!!

08 Aug 2006 Nathan Pust

Very helpful code.
if lines 68-77 are changed to as follows, the code will allow the current text labels to be used and property value pairs to be changed for those labels. Example usage:

xticklabel_rotate([],45,[],'Fontsize',14)

Lines 68-76:
if (nargin < 3 || isempty(varargin{1})) & (~exist('XTick') | isempty(XTick)),
xTickLabels = get(gca,'XTickLabel') ; % use current XTickLabel
% remove trailing spaces if exist (typical with auto generated XTickLabel)
temp1 = num2cell(xTickLabels,2) ;
for loop = 1:length(temp1),
temp1{loop} = deblank(temp1{loop}) ;
end
xTickLabels = temp1 ;
varargin = varargin(2:length(varargin));
end

10 Feb 2006 Stanley Bileschi

works exactly like I wanted it to

29 Dec 2005 Brian Katz

I have re-tested this with changing fonts in the original call and also afterwards via the handle, with no problems...

12 Nov 2005 Shai Shen-Orr

If you try setting to a different font through XTickLabel_rotate function call or after the labels has been set - the entire axis is messed up.

29 Oct 2005 Fangwen Lu

It's cool!

10 Aug 2005 Kay Pham

This is really useful! GREAT!

10 Aug 2005 Kay Pham

This is great! It's really useful! Thanks.

08 Jun 2005 Malik Yousef

When i use your code on 4 subplots i get weird results whne one of them that has different axis.
And i see that the subplot become so small!!
How to overcome this problem

05 Jun 2005 Isabella Osetinsky

Thank you. Solves many problems!
Add "set(gca,'tickdir','out')" to point out the label location.

26 May 2005 mark drever

this was sweet.

05 May 2005 les ander

??? Error using ==> set
Width and height must be > 0.

Error in ==> xticklabel_rotate at 168
set(gca,'position',newpos) ;

01 Oct 2004 Prasanth Karumanchi

awesome stuff

26 Jun 2004 Yuval Cohen

Other than the zoom issue, perfect. BRAVO!

20 Jun 2004 Brian Katz

As this operation removes the current labels and places text objects a tthe appripriate place in the figure window, there is no dynamic link to the axis afterwards, making zoom response difficult. Until MatLab integrates this type of behavoir directly into the label properties I think it would be very difficult to include dynamic behavior.

05 Jun 2004 K H

For this and similar functions where text replaces ticks, is there a way to make it react to zooming?

28 May 2004 Anshuman Bansal

Well Done!
I was struggling with this issue since last couple of months! Trying to write my own unsuccessful code..! Saved me a lot of time! I guess it should be part of standard MATLAB package

26 May 2004 Imola Fodor

Awesome. Thanks!

29 Feb 2004 Alf Magnusson

You made my day (nigth) Thanks

03 Feb 2004 John Godward

I spent some time looking for standard Matlab functions to do this with no luck. Then I found this m-file which did what was required. Thanks.

10 Oct 2003 David Sterling

Nicely done! Now I don't have to rotate my plotting axis(eg. barh and horizontal boxplot) to handle the long strings that inevitably creep into my categorical tick labels.

11 Sep 2003 Martin Schaefer

You saved me from using Excel for a whole bunch of Bargraph plots.
Thanks very much for that.
Another nice option would be to edit the ticks freely, that means to allow 2 line ticks and things alike

Updates
11 Jun 2003

Update 11-jun-03 : Resizing of axis is improved with control of xlabel,ylabel, and title. Improved default codition works with PREDEFINED XTickLabels now.

03 Mar 2009

Use suggested modifications have been integrated. The current version now support TOP xlabels. In addition, the degree of resizing of the axis has been reduced.

11 Jun 2010

Rev.2010. Revisions made to improve functionality in response to various user recommendations.

Contact us