Code covered by the BSD License  

Highlights from
Rotate Tick Label

4.59184

4.6 | 50 ratings Rate this file 187 Downloads (last 30 days) File Size: 1.78 KB File ID: #8722
image thumbnail

Rotate Tick Label

by

 

14 Oct 2005 (Updated )

ROTATETICKLABEL rotates tick labels.

| Watch this File

File Information
Description

ROTATETICKLABEL rotates tick labels. Any arbitrary rotation is acceptable. A demo is included. Right now it only can rotate XTickLabels but it would be easy to modify to rotate YTickLabels.

TH=ROTATETICKLABEL(H,ROT) is the calling form where H is a handle to the axis that contains the XTickLabels that are to be rotated. ROT is an optional parameter that specifies the angle of rotation. The default angle is 90. TH is a handle to the text objects created. For long strings such as those produced by datetick, you may have to adjust the position of the axes so the labels don't get cut off.

Acknowledgements

This file inspired Rotate And Alternate Tick Labels On Any Xaxis (Top Or Bottom).

MATLAB release MATLAB 7 (R14)
Other requirements None
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (57)
17 Sep 2014 Moroni

Just what I needed.

22 Jul 2014 Kaveh  
21 Jul 2014 Radu  
05 May 2014 Quan Wang

Very very useful!

05 Mar 2014 Derya  
03 May 2013 Wiviann

My data has 72 columns. I get an error message when using the rotateticklabel function according to:

Index exceeds matrix dimensions.

Error in rotateXLabels/createNewLabels (line 146)
textLabels(ll) = text( ...

Error in rotateXLabels (line 34)
h = createNewLabels( ax, vals, labels, angle );

It works fine up to 47 columns. But if the data is larger, I get this error.

Does anyone know how to solve this?
Thank you in advance.

29 Mar 2013 William Murphy

It worked, but not when I was using ticks which I had set. The plot I was trying has 21 points, but I only wanted to label every third point [125, 250 500 1000 2000 4000 8000]. The labels that I had used earlier were used, but the other ticks were also labeled
125, 2 3 250 5 6 500 8 9 and so on. So a work around will need to be found. Thank you for the posting.

21 Feb 2013 Carlos Andrade

Worked just fine for me Sonia!

18 Feb 2013 Sonia

I cannot download the function, I am redirected to a page that reportedly doesn´t exist... Any help??

06 Nov 2012 Dominik

Pretty slick. But why do the axis labels get moved to the top axis? I'm using it with subplot(1,2,x) and imagesc. Can anyone help with this?

19 Jun 2012 Nolan C

concise and gets the job done! nice.

30 Apr 2012 Pascal SZACHERSKI  
06 Feb 2012 Yannick

You may want to add the parameters

,'Parent',h

to the end of the 'text' commands in order not to rely on the value of gca.

01 Dec 2011 noosa

I have this error when I tried to run the program : rotateticklabel(gca,45);
??? Undefined function or method 'rotateticklabel' for input arguments of type 'double'.
please there is someone who can help to correct this error?? Thanks !

10 Nov 2011 Chris Taylor

Seems great, although it sometimes rotates axes off the visible section of the plot!

28 Apr 2011 Martin

Is there anyway to get this to work on subplots?

28 Apr 2011 Bencatx  
25 Apr 2011 Jonathan C. Lansey

Great! I modified it to keep the specified axis fontsize with the following:
% Add this line:
fsize = get(h,'fontsize');

% Modify this line to add 'fontsize',fsize

th=text(b,repmat(c(1)-.1*(c(2)-c(1)),length(b),1),a,'HorizontalAlignment','right','rotation',rot,'fontsize',fsize);

24 Apr 2011 Ilmira

very helpful function. thank you!

simple example of using with months:

startDate = datenum ('01-2009','mm-yyyy')
endDate = datenum('10-2010','mm-yyyy')
date = linspace(startDate,endDate,22);
plot(date, sRate), grid
set(gca,'XTick',date)
datetick('x','mmmyy','keepticks')
rotateticklabel(gca, 45)

19 Apr 2011 Hakon

Marcelo - thanks - I had that same problem, your fix solved it.

30 Mar 2011 marcelo

For my graphs, I had an error that reads

Because my graph had too many horizontal categories, Matlab automatically changes the axis step to a value different than 1. In this case, this code doesn't work.

I had to include after the line 46 the following command to make it work

[x y]=size(a);ticks=1; for j=2:x, ticks=horzcat(ticks,j); end, set(h,'XTick',ticks);

20 Mar 2011 Haim  
09 Mar 2011 Hakon

Thanks Andrew, this is extremely helpful. It seems to have a little issue when using subplot and applying rotation to all axes. (e.g. subplot(3,2,x) and then applying roration to each of the subplots - for some plots the text just disappears. R2010b)

04 Mar 2011 Wlodek Tych

So far so good - does exactly what it says on the tin.

I have not tried _all_ the odd combinations and uses, but then if you use Matlab in-depth and manage to crash it, you should be able to help the developer. Why complain? Just make it better instead.

Also, the Author says the procedure rotates X axis labels then why complain that it does not do Y axis? Someone helps you by publishing their work ... OK, I feel a rant coming so I'll finish now.

Many thanks to Andrew!

22 Sep 2010 Jaroslaw Tuszynski

Function did not work correctly for labeling images, but I was able to modify it to work

28 Aug 2010 Koray  
29 Jun 2010 Dymitr Ruta

The placement of XTickLabels is very messy if Y ticks are irregularly spaced or do not start at YMin, so instead of placing ticklabels at c(1)-0.1... change it to ymin(1)-0.1... where ymin=get(h,'YLim')

28 Jun 2010 d c

log fix didn't work for me

25 Jun 2010 Marissa  
25 Feb 2010 Hastiepen

Perfect; very easy to use and is exactly what I needed, thank you very much!

09 Nov 2009 Thodoris Papadopoulos

It was very helpful code for plot with dates.
simple example
y=(1:4);
x=(1:4);
figure(1)
plot(y,x)
set(gca,'XTick',1:4)
set(gca,'XTickLabel',{'date','date','date','date'})
th=rotateticklabel(gca,45);

29 Sep 2009 Quentin

Did not work for me. Always got error "There is no 'XTickLabel' property in the 'lineseries' class." , for plot, errorbar or bar. Little documentation provided.

03 Jun 2009 Patrick

If the y axis is log scale this code will put the new x tickmarks in thw wrong place. Replace "c(1)-.1*(c(2)-c(1))" with the variable "yoffset" and add the following before the text commands to fix the problem:

%get the y axis scale type
yst = get(h,'YScale');
%set up new y location based on scale type of y axis
if strcmp(yst,'log')
c = log10(c)/10;
yoffset = 10^(c(1)-.1*(c(2)-c(1)));
else
yoffset = c(1)-.1*(c(2)-c(1));
end

If your x ticks are 10^something, you can put those back by adding this before the text commands:

%fix for tick lables with exponents
xst = get(h,'XScale');
if strcmp(xst,'log')
a = [repmat('10^{',length(a),1) a repmat('}',length(a),1)];
end

07 Apr 2009 Ricky

put in the following "if statement" to check if there are yticks enabled.

if numel(c) > 0
if rot<180
th=text(b,repmat(c(1)-.1*(c(2)-c(1)),length(b),1),a,'HorizontalAlignment','right','rotation',rot);
else
th=text(b,repmat(c(1)-.1*(c(2)-c(1)),length(b),1),a,'HorizontalAlignment','left','rotation',rot);
end
else
if rot<180
th=text(b,repmat(-.1,length(b),1),a,'HorizontalAlignment','right','rotation',rot,'fontsize',16);
else
th=text(b,repmat(-.1,length(b),1),a,'HorizontalAlignment','left','rotation',rot,'fontsize',16);
end
end

10 Oct 2008 Angela Seyss  
04 Jul 2008 jimmy tsai

thank you this is useful for bar plots.
Matlab should have put in that option the first place.

25 Apr 2008 Fred Glass

This program will sometimes change my plots such that my titles are mislocated.

13 Dec 2007 shawnta zaghotah

you should have more pictures of these ticks but labeled pictures...

05 Dec 2007 James Yu

I guess in your code:

%make sure the rotation is in the range 0:360 (brute force method)
while rot>360
rot=rot-360;
end
while rot<0
rot=rot+360;
end

can be simplified as

rot = mod(rot,360);

06 Nov 2007 Shane Migliore

Thanks. Exactly what I was looking for...plus you can use LaTeX with your method since it makes text boxes.

04 Nov 2007 Matthias Hundt  
13 Oct 2007 Jose Vargas

It works fine. Thanks a lot for posting this useful function. I still can not believe Matlab 7.2 does not have a rotation tick label property in the axes handle.

12 Oct 2007 Gus Lott

Rotation works well. The rotation goes over the X Axis Label. Will need to lower the x axis label below the new x tick label text.

02 Oct 2007 Lucas Fortini  
25 May 2007 Carlos Busso

The only change that I did was to include the font size of the text as a variable. The function is very clever.

17 May 2007 x x

thanks!

12 Feb 2007 g l

The only suggestion I would have is the option to right justify the tick labels. I varied the format to make it compatible with a pareto diagram since some of the names were too long, but not all of the names were of the same length.

10 Jan 2007 Debprakash Patnaik

I am currently using it, Very happy with the results. Thanks

13 Sep 2006 Christoph Mudersbach

Well done. Great idea.

01 Sep 2006 J C

you, sir, are the man.

11 Jul 2006 Scott Otterson

This is a label rotation revolution!

15 Jun 2006 François Portet

exactly what I was looking for, thank you

09 Jun 2006 peter mao

exactly what i was looking for!

08 Jun 2006 Kathleen Spees  
30 Mar 2006 Karl Stahl  
18 Feb 2006 alex redstone

Just what i was looking for. There should be a ticklabel property like this for arbitrary rotation. Thanks a lot.

02 Nov 2005 oh kwoun  

Contact us