EDA365欢迎您登录!
您需要 登录 才可以下载或查看,没有帐号?注册
x
function new_fig_handle = shift_axis_to_origin( fig_handle ) % 本函数目的是把 matlab 做的图坐标轴移到图形的中间部分去(与数学的做图习惯一致)9 e1 M; u* R2 m0 I
% 2008.10.10 in pku
. g# B. p$ `: }%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%/ @% v5 `# ]' n5 C
figure('Name','shift_axis_to_origin','NumberTitle','off') % Create a new figure
0 d! w2 Y7 n. D( a- ~% 拷贝图形到一个新的窗口! F2 I5 `: g }4 L5 I
new_fig_handle = copyobj( fig_handle , gcf ); xL=xlim ;$ J" K" p4 u4 ^+ R, P [
yL=ylim ; xt=get(gca,'xtick') ;
2 Z4 u' K" w; J: fyt=get(gca,'ytick') ;
# ~7 \- |- X4 [3 |7 M0 l4 hset(gca,'XTick',[],'XColor','w') ;- S+ k* A7 M& Q" j1 u* ]7 |- T% k$ n
set(gca,'YTick',[],'YColor','w') ; % 把 x 和 y 坐标轴的两个方向各延长 10% (为了视觉上好看)
% [5 R/ l+ X# z& q1 Hextend_x = ( xL(2)-xL(1) ) * 0.1 ;, [9 r' @6 [# [1 B$ ]
extend_y = ( yL(2)-yL(1) ) * 0.1 ;) {1 s1 W- l7 R( C
xxL = xL + [ -extend_x extend_x] ;" @: Z$ n D3 k! {! j4 `7 _: x
yyL = yL + [ -extend_y extend_y] ;' Y6 X6 l( {8 `9 Y+ q3 q) M R
set(gca,'xlim', xxL) ;9 n9 A0 W4 t6 H: ~* ~
set(gca,'ylim', yyL) ; pos = get(gca,'Position') ;
$ u. U' ?1 {' ~/ A$ b, C8 dbox off; x_shift = abs( yyL(1)/(yyL(2)-yyL(1)) ) ;4 k' Q9 q" c. ^
y_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 ] ) ;8 _! A& L! B b/ J
xlim(xxL) ;
! V0 }+ {1 U& j1 K0 v: ^box off ;
* I1 p( v1 h! T( x- gset(temp_1,'XTick',xt,'Color','None','YTick',[]) ;1 n+ s7 N K1 |
set(temp_1,'YColor','w') ; temp_2 = axes( 'Position', pos + [ pos(3) * y_shift , 0 , -pos(3)* y_shift*0.99999 , 0 ] ) ;/ z6 z7 E+ a" N9 z2 k
ylim(yyL) ;
! H/ K/ s- C6 |# zbox off ;
3 Q+ N9 x+ d6 k8 O* t$ f/ j) t5 ~set(temp_2,'YTick',yt,'Color','None','XTick',[]) ;
. w: o+ u: M5 U& tset(temp_2,'XColor','w') ; Base_pos = get(new_fig_handle,'Position') ;
6 y! J" S8 X) j$ h& Oarrow_pos_in_x_dircetion = Base_pos(2) - Base_pos(4) * yyL(1)/(yyL(2)-yyL(1)) ;* K" @1 L O- T) z- w$ L' B
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');
: L1 ^! D, n0 ~' z# Jannotation('arrow',[arrow_pos_in_y_dircetion , arrow_pos_in_y_dircetion ] , [Base_pos(2) , Base_pos(2)+Base_pos(4)] , 'Color','k');" {4 J2 p, V% K( V4 g2 B7 P
end %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 下面是一个例子: % 本程序目的是把 matlab 做的图坐标轴移到图形的中间部分去(与数学的做图习惯一致)
" L1 _5 I$ g4 c; Z/ Q+ N5 B% 2008.10.08
R1 k P: \5 E3 ]5 D% F% clc;clear;close all;
7 `- n& o. h# C6 b2 n# k5 {t=linspace(-2,8,100);: f" d" _' ~& c" t6 d
a1=axes;
: {" \; \: a+ U' ?. [8 ]* Hplot(t,cos(t)); % xt=get(gca,'xtick');
) @ Y6 q: `7 e" a0 |$ r% set(gca,'XTick',[],'XColor','w');* U6 C# ~& H* [+ |/ r; _' p
% xL=xlim;
{! f9 u, f4 _% p=get(gca,'Position');
' X$ K) B& n- }# c7 b' u/ a% box off;6 U( x* B8 \) `8 a1 o
% a2=axes('Position',p+[0,p(4)/2,0,-p(4)/2]);# \% a* m" L0 n+ T
% xlim(xL);box off;
: M6 s8 D* D! X# n/ f l% set(gca,'XTick',xt,'Color','None','YTick',[]); new_fig_handle = shift_axis_to_origin( gca ) ; %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 此例子的实现效果如下两个图所示:
|/ k* ?* K& \$ c" H
1 ~1 ~2 ]7 R# f" g, u0 |( P
' \, c2 A5 H x1 a- ^/ X& B! z1 V: Q N. `
: M- L [3 K' j8 f |