在matlab中画两个函数的图像,y=sinx,y=cosx,怎么把函数名表在相应图像旁?另如何去掉边框和纵轴?请高手帮忙回答,急啊
在matlab中画两个函数的图像,y=sinx,y=cosx,怎么把函数名表在相应图像旁?另如何去掉边框和纵轴?请高手帮忙回答,急啊
答案:2 悬赏:40 手机版
解决时间 2021-05-09 14:18
- 提问者网友:爱唱彩虹
- 2021-05-09 09:42
最佳答案
- 五星知识达人网友:平生事
- 2021-05-09 11:14
此函数可以把所有这样的图形图像的坐标轴画在原点,只要已知图像的句柄。
函数如下:
function new_fig_handle = shift_axis_to_origin( fig_handle )
% 本函数目的是把 matlab 做的图坐标轴移到图形的中间部分去(与数学的做图习惯一致)
% 2008.10.08 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 ;
set(gca,'xlim', xL + [ -extend_x extend_x]) ;
set(gca,'ylim', yL + [ -extend_y extend_y]) ;
pos = get(gca,'Position') ;
box off;
x_shift = abs( yL(1)/(yL(2)-yL(1)) ) ;
y_shift = abs( xL(1)/(xL(2)-xL(1)) ) ;
temp_1 = axes( 'Position', pos + [ 0 , pos(4) * x_shift , 0 , - pos(4)* x_shift ] ) ;
xlim(xL) ;
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 ] ) ;
ylim(yL) ;
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) * yL(1)/(yL(2)-yL(1)) ;
arrow_pos_in_y_dircetion = Base_pos(1) - Base_pos(3) *xL(1)/(xL(2)-xL(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.08
%
clc;clear;close all;
t=linspace(-2,8,100);
a1=axes;
plot(t,cos(t));
% xt=get(gca,'xtick');
% set(gca,'XTick',[],'XColor','w');
% xL=xlim;
% p=get(gca,'Position');
% box off;
% a2=axes('Position',p+[0,p(4)/2,0,-p(4)/2]);
% xlim(xL);box off;
% set(gca,'XTick',xt,'Color','None','YTick',[]);
new_fig_handle = shift_axis_to_origin( gca ) ;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
此例子的实现效果如下两个图所示:
[ 本帖最后由 impu 于 2008-10-9 15:48 编辑 ] 附件
函数如下:
function new_fig_handle = shift_axis_to_origin( fig_handle )
% 本函数目的是把 matlab 做的图坐标轴移到图形的中间部分去(与数学的做图习惯一致)
% 2008.10.08 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 ;
set(gca,'xlim', xL + [ -extend_x extend_x]) ;
set(gca,'ylim', yL + [ -extend_y extend_y]) ;
pos = get(gca,'Position') ;
box off;
x_shift = abs( yL(1)/(yL(2)-yL(1)) ) ;
y_shift = abs( xL(1)/(xL(2)-xL(1)) ) ;
temp_1 = axes( 'Position', pos + [ 0 , pos(4) * x_shift , 0 , - pos(4)* x_shift ] ) ;
xlim(xL) ;
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 ] ) ;
ylim(yL) ;
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) * yL(1)/(yL(2)-yL(1)) ;
arrow_pos_in_y_dircetion = Base_pos(1) - Base_pos(3) *xL(1)/(xL(2)-xL(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.08
%
clc;clear;close all;
t=linspace(-2,8,100);
a1=axes;
plot(t,cos(t));
% xt=get(gca,'xtick');
% set(gca,'XTick',[],'XColor','w');
% xL=xlim;
% p=get(gca,'Position');
% box off;
% a2=axes('Position',p+[0,p(4)/2,0,-p(4)/2]);
% xlim(xL);box off;
% set(gca,'XTick',xt,'Color','None','YTick',[]);
new_fig_handle = shift_axis_to_origin( gca ) ;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
此例子的实现效果如下两个图所示:
[ 本帖最后由 impu 于 2008-10-9 15:48 编辑 ] 附件
- 1.jpg (18.08 KB)
2008-10-9 15:45
- 2.jpg (15.58 KB)
2008-10-9 15:45
全部回答
- 1楼网友:长青诗
- 2021-05-09 12:04
subplot(1,2,1)
fplot('cos',[-pi,pi])
grid on
box off
set(gca,'xgrid','off')
text(0,0,'y=cosx')
subplot(1,2,2)
fplot('sin',[-pi,pi])
grid on
box off
set(gca,'xgrid','off')
text(0,0,'y=sinx')
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯