EDA365欢迎您登录!
您需要 登录 才可以下载或查看,没有帐号?注册
x
function new_fig_handle = shift_axis_to_origin( fig_handle ) % 本函数目的是把 matlab 做的图坐标轴移到图形的中间部分去(与数学的做图习惯一致)8 j* u5 b: K9 t; V; M, R
% 2008.10.10 in pku$ [, b3 }; g5 h: H% ^$ T
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%& Q! x9 n1 m* Y( ?
figure('Name','shift_axis_to_origin','NumberTitle','off') % Create a new figure. z9 C* J/ N* o# z
% 拷贝图形到一个新的窗口/ j# r& m/ e. I$ [5 N, v
new_fig_handle = copyobj( fig_handle , gcf ); xL=xlim ;
1 A# s" _2 E7 U1 \3 byL=ylim ; xt=get(gca,'xtick') ;+ p5 B$ [. m- `' W6 }7 ~/ \7 d* g x
yt=get(gca,'ytick') ;0 x# H1 k. k0 P! Z8 n! f# }+ `
set(gca,'XTick',[],'XColor','w') ;
3 N& r5 T0 \) @& J4 [; X2 `set(gca,'YTick',[],'YColor','w') ; % 把 x 和 y 坐标轴的两个方向各延长 10% (为了视觉上好看)' ^% i3 v g4 {1 W& S+ }
extend_x = ( xL(2)-xL(1) ) * 0.1 ;. H& k. V: Y. q$ x+ @, C# w5 A; ]
extend_y = ( yL(2)-yL(1) ) * 0.1 ;
7 i# C# `2 S0 P( D& ]xxL = xL + [ -extend_x extend_x] ;
# Z4 C2 m6 Q9 } LyyL = yL + [ -extend_y extend_y] ;6 o5 {9 t& c+ N4 O. m+ ?
set(gca,'xlim', xxL) ;
( Z" H; D7 t- t; _& S, ~5 j7 |# \set(gca,'ylim', yyL) ; pos = get(gca,'Position') ;! F5 w; `% i0 e" y# y5 E
box off; x_shift = abs( yyL(1)/(yyL(2)-yyL(1)) ) ;
* N- H: r d" }% a+ B1 @( }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 ] ) ;. q2 T; ?- [6 V0 U3 m) f
xlim(xxL) ;* d- m; b/ {1 a7 O
box off ;) U& T! M" x2 C# Z9 }
set(temp_1,'XTick',xt,'Color','None','YTick',[]) ;' N3 C; h. l4 X" c. k# J+ t% e( [
set(temp_1,'YColor','w') ; temp_2 = axes( 'Position', pos + [ pos(3) * y_shift , 0 , -pos(3)* y_shift*0.99999 , 0 ] ) ;4 }; ?3 A, j. Y( ~8 f" D, n
ylim(yyL) ;* ]9 b0 i7 V O
box off ;6 \5 ~/ w* o+ o& m" }7 K, H
set(temp_2,'YTick',yt,'Color','None','XTick',[]) ;: j1 p* g1 _: M9 }6 O
set(temp_2,'XColor','w') ; Base_pos = get(new_fig_handle,'Position') ;3 n' \# |- T) i: ]
arrow_pos_in_x_dircetion = Base_pos(2) - Base_pos(4) * yyL(1)/(yyL(2)-yyL(1)) ;
/ C# L+ x5 D9 c( k" Narrow_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');% X) q& p' @3 |
annotation('arrow',[arrow_pos_in_y_dircetion , arrow_pos_in_y_dircetion ] , [Base_pos(2) , Base_pos(2)+Base_pos(4)] , 'Color','k');$ s- K; L5 Z+ n1 g8 A$ q4 M2 K! T
end %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 下面是一个例子: % 本程序目的是把 matlab 做的图坐标轴移到图形的中间部分去(与数学的做图习惯一致): p$ t+ }7 x1 }5 f5 R2 R" [9 h
% 2008.10.08
. T. O2 i5 M0 Q B3 {% clc;clear;close all;
: N9 R2 f5 _: R$ @% x4 q1 _t=linspace(-2,8,100);6 `3 ^0 H4 t! M/ z6 b3 A9 A# V: T
a1=axes;# W# ]# l, ~1 O7 X' K( {# i
plot(t,cos(t)); % xt=get(gca,'xtick');
3 t( s: S. |) m/ _! ?% set(gca,'XTick',[],'XColor','w');
9 i1 U% _" e1 o$ ^% xL=xlim;& z$ t8 j$ j0 ^
% p=get(gca,'Position');
6 I! w. W& B0 g8 O% box off;
2 @" Z5 c' F( }2 j- n% a2=axes('Position',p+[0,p(4)/2,0,-p(4)/2]);
' n( b( e8 v: \ d% xlim(xL);box off;
X: @4 ^; ?- ?' |% set(gca,'XTick',xt,'Color','None','YTick',[]); new_fig_handle = shift_axis_to_origin( gca ) ; %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 此例子的实现效果如下两个图所示:
) e/ p/ y4 ^. ], z7 l, e- T: C( i! M, N6 F* f) K" n
! F! Z- Q8 K- @; P
( Z1 C. V2 i% U* H
; L+ {3 j5 ], s4 L, a* t |