forked from kristinbranson/BABAM
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrotateticklabel.m
75 lines (72 loc) · 2.09 KB
/
rotateticklabel.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
function th=rotateticklabel(h,rot,demo)
%ROTATETICKLABEL rotates tick labels
% 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.
%
% Of course, GCA can be substituted for H if desired.
%
% TH=ROTATETICKLABEL([],[],'demo') shows a demo figure.
%
% Known deficiencies: if tick labels are raised to a power, the power
% will be lost after rotation.
%
% See also datetick.
% Written Oct 14, 2005 by Andy Bliss
% Copyright 2005 by Andy Bliss
%DEMO:
if nargin==3
x=[now-.7 now-.3 now];
y=[20 35 15];
figure
plot(x,y,'.-')
datetick('x',0,'keepticks')
h=gca;
set(h,'position',[0.13 0.35 0.775 0.55])
rot=90;
end
%set the default rotation if user doesn't specify
if nargin==1
rot=90;
end
%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
%get current tick labels
a=get(h,'XTickLabel');
%erase current tick labels from figure
set(h,'XTickLabel',[]);
%get tick label positions
b=get(h,'XTick');
c = get(h,'YLim');
ydir = get(h,'YDir');
if strcmpi(ydir,'reverse'),
c = c([2,1]);
end
%make new tick labels
if rot<180
th=text(b,repmat(c(1)-.1*(c(2)-c(1)),length(b),1),a,'HorizontalAlignment','right','rotation',rot,'Parent',h);
for i = 1:numel(th),
ex = get(th(i),'Extent');
pos = get(th(i),'Position');
if strcmpi(ydir,'normal'),
off = c(1) - (ex(2)+ex(4));
pos(2) = pos(2) + off;
set(th(i),'Position',pos);
else
off = ex(2)-ex(4) - c(1);
pos(2) = pos(2) - off;
set(th(i),'Position',pos);
end
end
else
th=text(b,repmat(c(1)-.1*(c(2)-c(1)),length(b),1),a,'HorizontalAlignment','left','rotation',rot,'Parent',h);
end
set(th,'Interpreter','none');