【随记】Matlab画四象限图形

我只是想静静的画个图

使用 Matlab 画个普通的函数曲线
转自 http://forum.vibunion.com/thread-70921-1-1.html

Code

新建一个 m 文件

function  new_fig_handle = shift_axis_to_origin( fig_handle )

% 本函数目的是把 matlab 做的图坐标轴移到图形的中间部分去(与数学的做图习惯一致)
% 2008.10.10 in pku
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
figure('Name','shift_axis_to_origin','NumberTitle','off')                         % Create a new figure
% 拷贝图形到一个新的窗口
new_fig_handle = copyobj( fig_handle , gcf );

xL=xlim ;
yL=ylim ;

    xt=get(gca,'xtick') ;
    yt=get(gca,'ytick') ;
    set(gca,'XTick',[],'XColor','w') ;
    set(gca,'YTick',[],'YColor','w') ;

     % 把 x 和 y 坐标轴的两个方向各延长 10% (为了视觉上好看)
    extend_x = ( xL(2)-xL(1) ) * 0.1 ;
    extend_y = ( yL(2)-yL(1) ) * 0.1 ;
    xxL = xL + [ -extend_x extend_x] ;
    yyL = yL + [ -extend_y extend_y] ;
    set(gca,'xlim', xxL) ;
    set(gca,'ylim', yyL) ;

    pos = get(gca,'Position') ;
    box off;

    x_shift = abs( yyL(1)/(yyL(2)-yyL(1)) ) ;
    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 ] ) ;
    xlim(xxL) ; 
    box off ;
    set(temp_1,'XTick',xt,'Color','None','YTick',[]) ;
    set(temp_1,'YColor','w') ;


    temp_2 = axes( 'Position', pos + [ pos(3) * y_shift , 0 , -pos(3)* y_shift*0.99999 , 0 ] ) ;
    ylim(yyL) ; 
    box off ;
    set(temp_2,'YTick',yt,'Color','None','XTick',[]) ;
    set(temp_2,'XColor','w') ;

    Base_pos = get(new_fig_handle,'Position') ;
    arrow_pos_in_x_dircetion = Base_pos(2) - Base_pos(4) * yyL(1)/(yyL(2)-yyL(1)) ;
    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'); 
    annotation('arrow',[arrow_pos_in_y_dircetion , arrow_pos_in_y_dircetion ] , [Base_pos(2) , Base_pos(2)+Base_pos(4)] , 'Color','k'); 


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

再新建一个写函数

% 本程序目的是把 matlab 做的图坐标轴移到图形的中间部分去(与数学的做图习惯一致)
% 2008.10.10 in pku

clc;clear;close all;
t=linspace(-2,8,100);
a1=axes;
plot(t,cos(t));

new_fig_handle = shift_axis_to_origin( gca ) ;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

Shot

Matlab2015使用实际效果

  • 7
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值