Matlab实现画中画效果

转自:http://blog.sina.com.cn/s/blog_7ec5cc870100wtf3.html

 在数据处理过程中经常需要将曲线某一部分的局部放大图与原图画在一起,翻了一些ilovematlab论坛的帖子,自己实现了一下。功能:在一个figure上显示一大一小两个axes即两个图,其中小图的位置可以拖动。具体思路如下:

   1、在一个figure中通过带'position'选项的axes()函数绘制两条曲线(以下简称‘大图’、‘小图’);
        设置小图的鼠标按下响应函数;
   2、在小图的鼠标按下响应函数中,设置figure的鼠标移动和鼠标抬起响应函数;
   3、在鼠标移动响应函数中,根据鼠标位置调整小图的位置。
   具体代码如下( 此处下载):
 
  
function [h1, h2, hp1, hp2] = plotinplot( x1, y1, x2, y2, strlegend ) 
%PLOTINPLOT 在一个figure中显示画中画效果 
% 函数形式:[h1, h2] = plotinplot( x1, y1, x2, y2, strlegend ) 
% x1,y1:在大图中显示的曲线的数据 
% x2,y2:在小图中显示的曲线的数据 
% strlegend:两个图的图例,为二维元胞数组形式{'legend1','legend2'} 
% [h1, h2]:返回两个axes的句柄,可根据该句柄对图形进一步修改 
% [hp1, hp2]:返回两个plot的句柄,可根据该句柄对曲线进一步修改
% Example: 
% x=0:pi/10:2*pi; 
% y1=sin(x); 
% y2=cos(x); 
% plotinplot(x,y2,x,y2,{'sin','cos'}); 
% 输入参数处理 
if nargin == 0 
x1=0:pi/50:2*pi; 
y1=sin(x1); 
x2=0:pi/50:2*pi; 
y2=cos(x2); 
end 
if ~exist('strlegend','var'
strlegend={'大图中的曲线','小图中的曲线'}; 
end 
% 在两个axes中分别画出对应的曲线 
figure; 
h1=axes(); 
hp1=plot(x1,y1,'b'); 
h2=axes('position',[0.3 0.2 0.3 0.3]); 
hp2=plot(x2,y2,'r');
hold on;
h=[hp1; hp2]; 
legend(h,strlegend); 
% 设置鼠标按下响应函数 
arg.h1=h1; 
arg.h2=h2; 
set(h2, 'ButtonDownFcn', {@ButtonDown,arg}); 
end 
function ButtonDown(src, evt, arg) 
arg.OrgPt = get(arg.h1, 'CurrentPoint'); 
arg.OrgPos = get(arg.h2,'position'); 
set(gcf, 'WindowButtonMotionFcn', {@WindowButtonMotion,arg}); 
set(gcf, 'WindowButtonUpFcn', @WindowButtonUp); 
end 
function WindowButtonMotion(src, evt, arg) 
currPt = get(arg.h1, 'CurrentPoint'); 
deltax=currPt(1,1)-arg.OrgPt(1,1); 
deltay=currPt(1,2)-arg.OrgPt(1,2); 
h1xlim=get(arg.h1,'xlim'); 
h1ylim=get(arg.h1,'ylim'); 
ratiox=deltax/(h1xlim(2)-h1xlim(1)); 
ratioy=deltay/(h1ylim(2)-h1ylim(1)); 
h1pos=get(arg.h1,'position'); 
h2pos=get(arg.h2,'position');
newx=arg.OrgPos(1)+ratiox*h1pos(3); 
newy=arg.OrgPos(2)+ratioy*h1pos(4); 
set(arg.h2,'position',[newx,newy,h2pos(3),h2pos(4)]); 
end 
function WindowButtonUp(src, evt) 
set(gcf, 'WindowButtonMotionFcn'''); 
set(gcf, 'WindowButtonUpFcn'''); 
end
   效果:
Matlab实现画中画效果

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值