EDA365欢迎您登录!
您需要 登录 才可以下载或查看,没有帐号?注册
x
function new_fig_handle = shift_axis_to_origin( fig_handle ) % 本函数目的是把 matlab 做的图坐标轴移到图形的中间部分去(与数学的做图习惯一致)* G" ]) u% v* b( ~+ m+ q! J, `
% 2008.10.10 in pku
w0 w. b* [ Z0 Y" }# p% u8 k/ [%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%6 U; z0 M) W$ H0 `4 _" m8 [
figure('Name','shift_axis_to_origin','NumberTitle','off') % Create a new figure
: c" Q, n1 a/ @% B% 拷贝图形到一个新的窗口3 Y! C( R$ G3 p( }( A
new_fig_handle = copyobj( fig_handle , gcf ); xL=xlim ;
# n' |3 W# |! |$ F9 ?, W- E$ WyL=ylim ; xt=get(gca,'xtick') ;
. n" _ k( E' }, myt=get(gca,'ytick') ;
% Y) R0 y/ k: @- [) R. Z' L8 vset(gca,'XTick',[],'XColor','w') ;
% R& d2 x9 }( i+ I& k3 Z( N, r* Xset(gca,'YTick',[],'YColor','w') ; % 把 x 和 y 坐标轴的两个方向各延长 10% (为了视觉上好看)7 w' f' O- V2 o, O! j
extend_x = ( xL(2)-xL(1) ) * 0.1 ;' \ n2 T& K5 X) u* B% T
extend_y = ( yL(2)-yL(1) ) * 0.1 ;
1 F1 Q: b3 h9 a |' M7 C) ixxL = xL + [ -extend_x extend_x] ;! ]) ~+ v4 F1 e8 L8 ^2 w
yyL = yL + [ -extend_y extend_y] ;
7 C: y" d4 }) b( L/ h# @set(gca,'xlim', xxL) ;, d0 t8 _" p5 D* U+ M- p
set(gca,'ylim', yyL) ; pos = get(gca,'Position') ;
2 k0 |# ~. \" i" {+ Nbox off; x_shift = abs( yyL(1)/(yyL(2)-yyL(1)) ) ;
: y& E3 z4 |; J5 f2 Z% V' f( G4 Gy_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 ] ) ;6 G$ T0 p5 B" B% V+ H$ X5 E
xlim(xxL) ;
, |5 t0 ^3 k1 S: a+ ^" lbox off ;3 x4 ~6 E3 \- Q: B# g
set(temp_1,'XTick',xt,'Color','None','YTick',[]) ;
4 C; T* I0 x$ A: z7 c2 W jset(temp_1,'YColor','w') ; temp_2 = axes( 'Position', pos + [ pos(3) * y_shift , 0 , -pos(3)* y_shift*0.99999 , 0 ] ) ;
6 i/ \4 v' W4 f# Aylim(yyL) ;
* \& y+ R U9 Mbox off ;
3 D7 r5 p8 f' s3 D' }1 s$ ]) Kset(temp_2,'YTick',yt,'Color','None','XTick',[]) ;
, d% I" N0 P! U& b8 x' tset(temp_2,'XColor','w') ; Base_pos = get(new_fig_handle,'Position') ;( y+ }5 b" N4 X# Z$ w* E# ~; c/ K
arrow_pos_in_x_dircetion = Base_pos(2) - Base_pos(4) * yyL(1)/(yyL(2)-yyL(1)) ;
2 i: e& m. r/ r k0 O- R# Darrow_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');
( ]3 q7 |; v+ \9 L$ K* \annotation('arrow',[arrow_pos_in_y_dircetion , arrow_pos_in_y_dircetion ] , [Base_pos(2) , Base_pos(2)+Base_pos(4)] , 'Color','k');
% f$ Q3 Y9 ] {+ Y. iend %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 下面是一个例子: % 本程序目的是把 matlab 做的图坐标轴移到图形的中间部分去(与数学的做图习惯一致)
9 {' R$ v: ^6 Z5 p8 n, Y% 2008.10.08
7 S/ }3 F" ~8 |: K" }/ \% clc;clear;close all;; G7 ]2 {5 ?. X" B* r$ C& ~
t=linspace(-2,8,100);
9 ~5 Q' m# s) }# h7 Ba1=axes;
/ N, |9 s8 s# n, G& W6 _+ B# E# }plot(t,cos(t)); % xt=get(gca,'xtick');
0 b6 p* [5 S" a2 I8 u8 i/ v% set(gca,'XTick',[],'XColor','w');+ f' V4 w! [% ^
% xL=xlim;
# f! ^% e8 x0 I4 e6 S6 g- t* I/ u7 K% p=get(gca,'Position');
! @5 f# U6 R3 V/ f% i& e0 e Y% box off;% C$ C6 D; e, s! C# \1 ^
% a2=axes('Position',p+[0,p(4)/2,0,-p(4)/2]);
& X+ _" r& c' n- Z% xlim(xL);box off;
/ {# [& l1 q6 N9 f0 y0 O2 l% set(gca,'XTick',xt,'Color','None','YTick',[]); new_fig_handle = shift_axis_to_origin( gca ) ; %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 此例子的实现效果如下两个图所示:
2 d6 t4 }8 Q+ ]/ Z9 M) u7 H3 _) z; R! S
0 K) P3 ~$ ~% W+ a" F8 v" y8 S: |
! L4 Z1 V" |- }" ^3 x
2 p* L( P$ G$ X2 r+ h |