EDA365欢迎您登录!
您需要 登录 才可以下载或查看,没有帐号?注册
x
function new_fig_handle = shift_axis_to_origin( fig_handle ) % 本函数目的是把 matlab 做的图坐标轴移到图形的中间部分去(与数学的做图习惯一致)
+ t3 v& q& `. @" ~- N! {% 2008.10.10 in pku
9 |* _8 M5 d! F9 A& D%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%" W3 f. A$ G8 e; i
figure('Name','shift_axis_to_origin','NumberTitle','off') % Create a new figure
# x; ?3 T* K. d, B% J) S* j% 拷贝图形到一个新的窗口
) }9 Q) M9 u" J4 m% o6 ^new_fig_handle = copyobj( fig_handle , gcf ); xL=xlim ;, f9 K4 ]3 R. C4 i# |, ~
yL=ylim ; xt=get(gca,'xtick') ;
: w2 @: o% Q3 y) Hyt=get(gca,'ytick') ;
, D1 i# C6 q$ z* C0 ~, pset(gca,'XTick',[],'XColor','w') ;
$ f2 o, _- }" d! A7 D& c2 V! ?set(gca,'YTick',[],'YColor','w') ; % 把 x 和 y 坐标轴的两个方向各延长 10% (为了视觉上好看)
( k0 u8 u' X w0 textend_x = ( xL(2)-xL(1) ) * 0.1 ;
/ R: w# ?. m% s' Textend_y = ( yL(2)-yL(1) ) * 0.1 ;
- w) S+ n/ }& K2 nxxL = xL + [ -extend_x extend_x] ;3 u: G, Q9 F9 x' K4 E6 e
yyL = yL + [ -extend_y extend_y] ;
2 ]. Q0 U& Q/ }set(gca,'xlim', xxL) ;
0 l- J4 k* o. _( G" Vset(gca,'ylim', yyL) ; pos = get(gca,'Position') ;
( ~5 v0 L# c. k, qbox off; x_shift = abs( yyL(1)/(yyL(2)-yyL(1)) ) ;
F# ?6 J8 Q' fy_shift = abs( xxL(1)/(xxL(2)-xxL(1)) ) ; temp_1 = axes( 'Position', pos + [ 0 , pos(4) * x_shift , 0 , - pos(4)* x_shift*0.99999 ] ) ;
1 _. g: \7 N! H; o& r% s5 ~; g+ exlim(xxL) ;
" V! L6 ~1 [8 m, ^0 L' M# O3 X; J. L obox off ;1 n8 h) n( M9 E; p5 S. ]1 k- _
set(temp_1,'XTick',xt,'Color','None','YTick',[]) ;( s* K" a: d" [+ D! x5 c
set(temp_1,'YColor','w') ; temp_2 = axes( 'Position', pos + [ pos(3) * y_shift , 0 , -pos(3)* y_shift*0.99999 , 0 ] ) ;
& G8 J# a7 U8 f. f/ ?ylim(yyL) ;6 _0 \1 x' q- a7 e' U( z7 M
box off ;
, [- N2 i5 l& z' [2 gset(temp_2,'YTick',yt,'Color','None','XTick',[]) ;& \, u+ D+ q0 m P! a4 O. N
set(temp_2,'XColor','w') ; Base_pos = get(new_fig_handle,'Position') ;
% F: ~: C& {' |" h- narrow_pos_in_x_dircetion = Base_pos(2) - Base_pos(4) * yyL(1)/(yyL(2)-yyL(1)) ;
: i! R$ Z% w1 ], _arrow_pos_in_y_dircetion = Base_pos(1) - Base_pos(3) * xxL(1)/(xxL(2)-xxL(1)) ; annotation('arrow',[Base_pos(1) , Base_pos(1)+Base_pos(3)] , [arrow_pos_in_x_dircetion , arrow_pos_in_x_dircetion ] , 'Color','k');
; D0 _9 B- J2 A7 ~ K1 k, Rannotation('arrow',[arrow_pos_in_y_dircetion , arrow_pos_in_y_dircetion ] , [Base_pos(2) , Base_pos(2)+Base_pos(4)] , 'Color','k');( s: f: p, D ~6 L1 l3 q* J
end %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 下面是一个例子: % 本程序目的是把 matlab 做的图坐标轴移到图形的中间部分去(与数学的做图习惯一致)( M* l; E5 D B
% 2008.10.08# B, h7 ]% r" J+ U% G7 ~/ d
% clc;clear;close all;' i0 W# Q8 V2 M( F. I* q
t=linspace(-2,8,100);
' S! L7 m& Y: ya1=axes;
0 u8 ]' K8 m" s/ ~plot(t,cos(t)); % xt=get(gca,'xtick');% {( C9 p; Q6 z, M! q
% set(gca,'XTick',[],'XColor','w');
}4 g5 i* n9 ^) T% xL=xlim;, \9 l8 F! b- ]: O0 |9 `
% p=get(gca,'Position');0 z+ z! ^0 {/ v9 o
% box off;
t ^% j; N0 [& }: s0 B8 n& {% a2=axes('Position',p+[0,p(4)/2,0,-p(4)/2]);1 V# ~6 s# a# b
% xlim(xL);box off;
3 j0 K% D' a# q3 D8 z% set(gca,'XTick',xt,'Color','None','YTick',[]); new_fig_handle = shift_axis_to_origin( gca ) ; %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 此例子的实现效果如下两个图所示:
6 S( o$ s3 [/ a' b) W1 O }) z( m! C5 A1 A
! V# |: b4 r' ?! l( E! q
. j% I6 s- _+ H7 J3 t. B, n
* ^7 m! h+ L4 s |