EDA365欢迎您登录!
您需要 登录 才可以下载或查看,没有帐号?注册
x
function new_fig_handle = shift_axis_to_origin( fig_handle ) % 本函数目的是把 matlab 做的图坐标轴移到图形的中间部分去(与数学的做图习惯一致)6 c2 ~! r+ r$ o. F* v% Q
% 2008.10.10 in pku
2 S# i& ]; a- k1 W" F$ Y%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%( z# C( h2 s6 E! Q" m& Y
figure('Name','shift_axis_to_origin','NumberTitle','off') % Create a new figure
4 ~8 V+ ^: i _ V* \9 N, R6 s% 拷贝图形到一个新的窗口
: k" q8 }1 O7 @1 c/ }- r' Gnew_fig_handle = copyobj( fig_handle , gcf ); xL=xlim ;4 \4 C1 A0 j; L! [
yL=ylim ; xt=get(gca,'xtick') ;% D( _0 Q; e$ X) |
yt=get(gca,'ytick') ;
9 Y& D! p, I" m, d; R; @& Aset(gca,'XTick',[],'XColor','w') ;9 G s: \ a, g8 T6 D
set(gca,'YTick',[],'YColor','w') ; % 把 x 和 y 坐标轴的两个方向各延长 10% (为了视觉上好看)
, k. w2 z; U, Q% O9 t4 S( ]3 Qextend_x = ( xL(2)-xL(1) ) * 0.1 ;
# X$ f1 ?( N: }& a! H+ Kextend_y = ( yL(2)-yL(1) ) * 0.1 ;
0 _& g5 X$ q9 b& U7 |xxL = xL + [ -extend_x extend_x] ;$ S' s3 F, v" h. T
yyL = yL + [ -extend_y extend_y] ;
) ~5 E# x, P* s6 Bset(gca,'xlim', xxL) ;9 _9 w+ ~' K H* _
set(gca,'ylim', yyL) ; pos = get(gca,'Position') ;
* X# N" G3 c; [" K! i! W, A/ Y+ A; T3 \box off; x_shift = abs( yyL(1)/(yyL(2)-yyL(1)) ) ;
! E6 f& O1 P# U S) S Ay_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 ] ) ; D: U% J% f6 G
xlim(xxL) ;
6 s! L% t5 X5 o8 }box off ;
9 L# `( L5 n8 W& j9 t0 v _% lset(temp_1,'XTick',xt,'Color','None','YTick',[]) ;/ i4 x) n; i, U6 |
set(temp_1,'YColor','w') ; temp_2 = axes( 'Position', pos + [ pos(3) * y_shift , 0 , -pos(3)* y_shift*0.99999 , 0 ] ) ;
, e( X+ n6 z/ fylim(yyL) ;% |4 w3 F7 }; Q! G; w2 E3 G7 p! b9 j
box off ;
6 y Q* J* w f' G/ dset(temp_2,'YTick',yt,'Color','None','XTick',[]) ;: y5 O9 y! \7 h& } c
set(temp_2,'XColor','w') ; Base_pos = get(new_fig_handle,'Position') ;
' L" f: B4 p% h) k' n# Sarrow_pos_in_x_dircetion = Base_pos(2) - Base_pos(4) * yyL(1)/(yyL(2)-yyL(1)) ;
0 r# W- r. W, Barrow_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 u& V8 i& n' z5 |! Q2 _0 L* r: \
annotation('arrow',[arrow_pos_in_y_dircetion , arrow_pos_in_y_dircetion ] , [Base_pos(2) , Base_pos(2)+Base_pos(4)] , 'Color','k');0 K5 E) P' h8 P; E
end %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 下面是一个例子: % 本程序目的是把 matlab 做的图坐标轴移到图形的中间部分去(与数学的做图习惯一致)9 w. W" |8 Z# W3 o0 ?. O
% 2008.10.080 h$ m% N/ y8 J. t2 y
% clc;clear;close all;; `: P' J5 _, x9 i0 w
t=linspace(-2,8,100);
2 `/ \* Y& W0 B8 N% \- I$ t0 ma1=axes;
5 J# p2 ~% o& e3 ?* @plot(t,cos(t)); % xt=get(gca,'xtick');7 M& n/ U0 P8 Y- g
% set(gca,'XTick',[],'XColor','w');- l/ I( u* x( Z+ W
% xL=xlim;3 h( t) Q2 B: I: w/ g
% p=get(gca,'Position');/ d: s5 V. C0 b" h: a' {
% box off;
3 s- ]( ?: N H& i! m% a2=axes('Position',p+[0,p(4)/2,0,-p(4)/2]);5 t7 n7 d i( t0 v) B
% xlim(xL);box off;
. _: L+ l0 b" G6 ~7 ^% set(gca,'XTick',xt,'Color','None','YTick',[]); new_fig_handle = shift_axis_to_origin( gca ) ; %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 此例子的实现效果如下两个图所示:
. y( m& N& n# w4 \. u
5 O& w. M; M/ U$ {/ C
" p* D; T- i* K l/ p9 v+ [8 l3 W1 G
0 r4 o& o) ]# s& g, k0 L |