comparison core/tools/centeraxes.m @ 0:e9a9cd732c1e tip

first hg version after svn
author wolffd
date Tue, 10 Feb 2015 15:05:51 +0000
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:e9a9cd732c1e
1 function out = centeraxes(ax,opt)
2 % out = centeraxes(ax,opt)
3 %==========================================================================
4 % Center the coordinate axes of a plot so that they pass through the
5 % origin.
6 %
7 % Input: 0, 1 or 2 arguments
8 % 0: Moves the coordinate axes of 'gca', i.e. the currently active
9 % axes.
10 % 1: ax, a handle to the axes which should be moved.
11 % 2: ax, opt, where opt is a struct with further options
12 % opt.fontsize = 'size of axis tick labels'
13 % opt.fontname = name of axis tick font.
14 %
15 % If the opt struct is ommitted, the current values for the axes are used.
16 %
17 % Output: stuct out containing handles to all line objects created by this
18 % function.
19 %
20 %==========================================================================
21 % Version: 1.1
22 % Created: October 1, 2008, by Johan E. Carlson
23 % Last modified: November 21, 2009, by Johan E. Carlson
24 %==========================================================================
25
26 if nargin < 2,
27 fontsize = get(ax,'FontSize');
28 fontname = get(ax,'FontName');
29 end
30 if nargin < 1,
31 ax = gca;
32 end
33
34 if nargin == 2,
35 if isfield(opt,'fontsize'),
36 fontsize = opt.fontsize;
37 else
38 fontsize = get(ax,'FontSize');
39 end;
40 if isfield(opt,'fontname'),
41 fontname = opt.fontname;
42 else
43 fontname = get(ax,'FontName');
44 end
45 end
46
47 axes(ax);
48 set(gcf,'color',[1 1 1]);
49 xtext = get(get(ax,'xlabel'),'string');
50 ytext = get(get(ax,'ylabel'),'string');
51
52 %--------------------------------------------------------------------------
53 % Check if the current coordinate system include the origin. If not, change
54 % it so that it does!
55 %--------------------------------------------------------------------------
56 xlim = 1.1*get(ax,'xlim');
57 ylim = 1.1*get(ax,'ylim');
58 set(ax,'xlim',xlim);
59 set(ax,'ylim',ylim);
60
61 if xlim(1)>0,
62 xlim(1) = 0;
63 end
64
65 if ylim(1)>0,
66 ylim(1) = 0;
67 end
68
69 if xlim(2) < 0,
70 xlim(2) = 0;
71 end
72
73 if ylim(2) < 0,
74 ylim(2) = 0;
75 end;
76
77 set(ax,'xlim',xlim,'ylim',ylim);
78
79
80 % -------------------------------------------------------------------------
81 % Caculate size of the "axis tick marks"
82 % -------------------------------------------------------------------------
83 axpos = get(ax,'position');
84 figpos = get(gcf,'position');
85 aspectratio = axpos(4) / (axpos(3));
86 xsize = xlim(2) - xlim(1);
87 ysize = ylim(2) - ylim(1);
88 xticksize = ysize/figpos(4)*12;
89 yticksize = xsize*aspectratio/figpos(3)*12;
90
91 % -------------------------------------------------------------------------
92 % Store old tick values and tick labels
93 % -------------------------------------------------------------------------
94 ytick = get(ax,'YTick');
95 xtick = get(ax,'XTick');
96 xticklab = get(ax,'XTickLabel');
97 yticklab = get(ax,'YTickLabel');
98
99 % -------------------------------------------------------------------------
100 % Draw new coordinate system
101 % -------------------------------------------------------------------------
102
103 yax = line([0; 0],[ylim(1)-1; ylim(2)]);
104 xax = line([xlim(1); xlim(2)],[0; 0]);
105 set(xax,'color',[0 0 0])
106 set(yax,'color',[0 0 0])
107
108 % Draw x-axis ticks
109 for k = 1:length(xtick),
110 newxtick(k) = line([xtick(k); xtick(k)],[-xticksize/2; xticksize/2]);
111 if (xtick(k)~=0),
112 newxticklab(k) = text(xtick(k),-1.5*xticksize, strtrim(xticklab(k,:)));
113 set(newxticklab(k),'HorizontalAlignment','center',...
114 'Fontsize',fontsize,'FontName',fontname);
115 end
116 end
117 set(newxtick,'color',[0 0 0]);
118
119 % Draw y-axis ticks
120 for k = 1:length(ytick),
121 newytick(k) = line([-yticksize/2; yticksize/2],[ytick(k); ytick(k)]);
122 if (ytick(k)~=0),
123 newyticklab(k) = text(-.8*yticksize,ytick(k), yticklab(k,:));
124 set(newyticklab(k),'HorizontalAlignment','right',...
125 'FontSize',fontsize,'FontName',fontname);
126 end
127 end
128 set(newytick,'color',[0 0 0]);
129
130 %--------------------------------------------------------------------------
131 % Move xlabels
132 %--------------------------------------------------------------------------
133 newxlabel = text(xlim(2),-1.5*xticksize,xtext);
134 set(newxlabel,'HorizontalAlignment','center',...
135 'FontWeight','demi','FontSize',fontsize+2,'FontName',fontname);
136
137 newylabel = text(-yticksize,ylim(2)*1.02,ytext);
138 set(newylabel,'HorizontalAlignment','right','VerticalAlignment','top',...
139 'FontWeight','demi','FontSize',fontsize+2,'FontName',fontname);
140
141 %--------------------------------------------------------------------------
142 % Create arrowheads
143 %--------------------------------------------------------------------------
144 x = [0; -yticksize/4; yticksize/4];
145 y = [ylim(2); ylim(2)-xticksize; ylim(2)-xticksize];
146 patch(x,y,[0 0 0])
147
148 x = [xlim(2); xlim(2)-yticksize; xlim(2)-yticksize];
149 y = [0; xticksize/4; -xticksize/4];
150 patch(x,y,[0 0 0])
151
152 axis off;
153 box off;
154
155 %--------------------------------------------------------------------------
156 % Create output struct
157 %--------------------------------------------------------------------------
158
159 if nargout > 0,
160 out.xaxis = xax;
161 out.yaxis = yax;
162 out.xtick = newxtick;
163 out.ytick = newytick;
164 out.xticklabel = newxticklab;
165 out.yticklabel = newyticklab;
166 out.newxlabel = newxlabel;
167 out.newylabel = newylabel;
168 end;