MAtlab如何实现axes中图片放大和缩小
1个回答
2015-04-20
展开全部
zoom 指令可以将图形放大或缩小,若要将图形放大时用 zoom on,zoom out,当不再须要放大或缩小图形时用 zoom off。
>> M=peaks(25); % peaks 是MATLAB内建的一个像山峰的特别函数,25是这个
>> plot(M) % 函数矩阵的大小,如果数值愈大则画出的山峰图愈平滑
>> zoom on % 开始放大图形,每按一次Enter键图形就放大一次
>> zoom out % 开始缩小图形,每按一次Enter键图形就缩小一次
>> zoom off % 停止图形放大或缩小功能
可以使用函数来实现此功能
图形移动,放大缩小等功能的函数 :
function axdrag(action)
%AXDRAG Pan and zoom with simple keystrokes
% Use this tool to move quickly around the data displayed in a 2-D plot.
% Make sure the figure has focus, and then press any of the following
% keys to zoom in or out. Clicking and dragging will pan the data.
%
% Keys you can use are:
% z, Z: zoom in, zoom out, in both dimensions
% x, X: zoom in, zoom out, x dimension only
% y, Y: zoom in, zoom out, y dimension only
% arrow keys: pan the data
% a: axis auto
% n: axis normal
% e: axis equal
% g: toggle grid state
% spacebar: toggle axis tick display state
% h: help
%
% Example
% c = pi*(1+sqrt(5))/2;
% x = 0:1000;
% r = 2.72378;
% z = cumsum(exp(i*(c*x.*x + r)));
% plot(real(z),imag(z));
% axdrag
% % Now click, drag, and use special keys ...
% Ned Gulley, March 2003
persistent x0 dx
if nargin < 1,
action = 'initialize';
end
% Use these variables to change the zoom and pan amounts
zoomFactor = 0.9;
panFactor = 0.02;
% Get rid of the help window if it's being displayed
helpTextAxis = findobj(gcbf,'Type','axes','Tag','axdraghelpaxis');
if isempty(helpTextAxis)
helpWasOff = 1;
else
helpWasOff = 0;
delete(helpTextAxis);
end
switch action
case 'initialize'
set(gca,'ButtonDownFcn','axdrag start')
set(gcf,'KeyPressFcn','axdrag keypress')
set(gcf,'DoubleBuffer','on')
case 'start'
set(gcbf,'Units','pixel');
set(gca,'Units','pixel');
set(gcbf,'WindowButtonMotionFcn','axdrag move')
set(gcbf,'WindowButtonUpFcn','axdrag stop')
currentPoint = get(gcbf,'CurrentPoint');
x0 = currentPoint;
axdrag move
case 'move'
currentPoint = get(gcbf,'CurrentPoint');
dx = currentPoint - x0;
x0 = currentPoint;
ap = get(gca,'Position');
xLim = get(gca,'XLim');
yLim = get(gca,'YLim');
set(gca,'XLim',xLim-(diff(xLim)*dx(1)/ap(3)), ...
'YLim',yLim-(diff(yLim)*dx(2)/ap(4)));
case 'stop'
set(gcbf,'WindowButtonMotionFcn','')
set(gcbf,'WindowButtonUpFcn','')
set(gcbf,'Units','normalized');
set(gca,'Units','normalized');
case 'keypress'
currChar = get(gcbf,'CurrentCharacter');
if isempty(currChar)
return
end
if currChar=='a',
axis auto
elseif currChar=='e',
axis equal
elseif currChar=='n',
axis normal
elseif currChar=='g',
grid
elseif currChar==28,
xLim=get(gca,'XLim');
xLimNew = xLim + panFactor*diff(xLim);
set(gca,'XLim',xLimNew)
elseif currChar==29,
xLim=get(gca,'XLim');
xLimNew = xLim - panFactor*diff(xLim);
set(gca,'XLim',xLimNew)
elseif currChar==30,
yLim=get(gca,'YLim');
yLimNew = yLim - panFactor*diff(yLim);
set(gca,'YLim',yLimNew)
elseif currChar==31,
yLim=get(gca,'YLim');
yLimNew = yLim + panFactor*diff(yLim);
set(gca,'YLim',yLimNew)
elseif abs(currChar)==32,
if isempty(get(gca,'XTick')),
set(gca,'XTickMode','auto','YTickMode','auto')
else
set(gca,'XTick',[],'YTick',[],'Box','on')
end
elseif (currChar=='x') | (currChar=='X'),
if currChar == 'X',
zoomFactor=1/zoomFactor;
end
xLim=get(gca,'XLim');
xLimNew = [0 zoomFactor*diff(xLim)] + xLim(1) + (1-zoomFactor)*diff(xLim)/2;
set(gca,'XLim',xLimNew)
elseif (currChar=='y') | (currChar=='Y'),
if currChar == 'Y',
zoomFactor=1/zoomFactor;
end
yLim=get(gca,'YLim');
yLimNew = [0 zoomFactor*diff(yLim)] + yLim(1) + (1-zoomFactor)*diff(yLim)/2;
set(gca,'YLim',yLimNew)
elseif (currChar=='z') | (currChar=='Z'),
if currChar == 'Z',
zoomFactor=1/zoomFactor;
end
xLim=get(gca,'XLim');
yLim=get(gca,'YLim');
xLimNew = [0 zoomFactor*diff(xLim)] + xLim(1) + (1-zoomFactor)*diff(xLim)/2;
yLimNew = [0 zoomFactor*diff(yLim)] + yLim(1) + (1-zoomFactor)*diff(yLim)/2;
set(gca,'XLim',xLimNew,'YLim',yLimNew)
elseif currChar=='h',
if helpWasOff
str = { ...
' '
' AXDRAG. Keys you can use are:'
' '
' z, Z: zoom in, zoom out, both dimensions '
' x, X: zoom in, zoom out, x dimension only '
' y, Y: zoom in, zoom out, y dimension only '
' arrow keys: pan the data'
' a: axis auto'
' n: axis normal'
' e: axis equal'
' g: toggle grid state'
' spacebar: toggle axis tick display state'
' h: help'
' '
' Press ''h'' again to dismiss this message'
' ' ...
};
helpTextAxis = axes( ...
'Tag','axdraghelpaxis', ...
'Units','characters', ...
'Position',[2 1 76 16], ...
'Visible','off');
text(0,1,str, ...
'Parent',helpTextAxis, ...
'VerticalAlignment','top', ...
'BackgroundColor',[1 1 0.8], ...
'FontName','courier', ...
'FontSize',6);
>> M=peaks(25); % peaks 是MATLAB内建的一个像山峰的特别函数,25是这个
>> plot(M) % 函数矩阵的大小,如果数值愈大则画出的山峰图愈平滑
>> zoom on % 开始放大图形,每按一次Enter键图形就放大一次
>> zoom out % 开始缩小图形,每按一次Enter键图形就缩小一次
>> zoom off % 停止图形放大或缩小功能
可以使用函数来实现此功能
图形移动,放大缩小等功能的函数 :
function axdrag(action)
%AXDRAG Pan and zoom with simple keystrokes
% Use this tool to move quickly around the data displayed in a 2-D plot.
% Make sure the figure has focus, and then press any of the following
% keys to zoom in or out. Clicking and dragging will pan the data.
%
% Keys you can use are:
% z, Z: zoom in, zoom out, in both dimensions
% x, X: zoom in, zoom out, x dimension only
% y, Y: zoom in, zoom out, y dimension only
% arrow keys: pan the data
% a: axis auto
% n: axis normal
% e: axis equal
% g: toggle grid state
% spacebar: toggle axis tick display state
% h: help
%
% Example
% c = pi*(1+sqrt(5))/2;
% x = 0:1000;
% r = 2.72378;
% z = cumsum(exp(i*(c*x.*x + r)));
% plot(real(z),imag(z));
% axdrag
% % Now click, drag, and use special keys ...
% Ned Gulley, March 2003
persistent x0 dx
if nargin < 1,
action = 'initialize';
end
% Use these variables to change the zoom and pan amounts
zoomFactor = 0.9;
panFactor = 0.02;
% Get rid of the help window if it's being displayed
helpTextAxis = findobj(gcbf,'Type','axes','Tag','axdraghelpaxis');
if isempty(helpTextAxis)
helpWasOff = 1;
else
helpWasOff = 0;
delete(helpTextAxis);
end
switch action
case 'initialize'
set(gca,'ButtonDownFcn','axdrag start')
set(gcf,'KeyPressFcn','axdrag keypress')
set(gcf,'DoubleBuffer','on')
case 'start'
set(gcbf,'Units','pixel');
set(gca,'Units','pixel');
set(gcbf,'WindowButtonMotionFcn','axdrag move')
set(gcbf,'WindowButtonUpFcn','axdrag stop')
currentPoint = get(gcbf,'CurrentPoint');
x0 = currentPoint;
axdrag move
case 'move'
currentPoint = get(gcbf,'CurrentPoint');
dx = currentPoint - x0;
x0 = currentPoint;
ap = get(gca,'Position');
xLim = get(gca,'XLim');
yLim = get(gca,'YLim');
set(gca,'XLim',xLim-(diff(xLim)*dx(1)/ap(3)), ...
'YLim',yLim-(diff(yLim)*dx(2)/ap(4)));
case 'stop'
set(gcbf,'WindowButtonMotionFcn','')
set(gcbf,'WindowButtonUpFcn','')
set(gcbf,'Units','normalized');
set(gca,'Units','normalized');
case 'keypress'
currChar = get(gcbf,'CurrentCharacter');
if isempty(currChar)
return
end
if currChar=='a',
axis auto
elseif currChar=='e',
axis equal
elseif currChar=='n',
axis normal
elseif currChar=='g',
grid
elseif currChar==28,
xLim=get(gca,'XLim');
xLimNew = xLim + panFactor*diff(xLim);
set(gca,'XLim',xLimNew)
elseif currChar==29,
xLim=get(gca,'XLim');
xLimNew = xLim - panFactor*diff(xLim);
set(gca,'XLim',xLimNew)
elseif currChar==30,
yLim=get(gca,'YLim');
yLimNew = yLim - panFactor*diff(yLim);
set(gca,'YLim',yLimNew)
elseif currChar==31,
yLim=get(gca,'YLim');
yLimNew = yLim + panFactor*diff(yLim);
set(gca,'YLim',yLimNew)
elseif abs(currChar)==32,
if isempty(get(gca,'XTick')),
set(gca,'XTickMode','auto','YTickMode','auto')
else
set(gca,'XTick',[],'YTick',[],'Box','on')
end
elseif (currChar=='x') | (currChar=='X'),
if currChar == 'X',
zoomFactor=1/zoomFactor;
end
xLim=get(gca,'XLim');
xLimNew = [0 zoomFactor*diff(xLim)] + xLim(1) + (1-zoomFactor)*diff(xLim)/2;
set(gca,'XLim',xLimNew)
elseif (currChar=='y') | (currChar=='Y'),
if currChar == 'Y',
zoomFactor=1/zoomFactor;
end
yLim=get(gca,'YLim');
yLimNew = [0 zoomFactor*diff(yLim)] + yLim(1) + (1-zoomFactor)*diff(yLim)/2;
set(gca,'YLim',yLimNew)
elseif (currChar=='z') | (currChar=='Z'),
if currChar == 'Z',
zoomFactor=1/zoomFactor;
end
xLim=get(gca,'XLim');
yLim=get(gca,'YLim');
xLimNew = [0 zoomFactor*diff(xLim)] + xLim(1) + (1-zoomFactor)*diff(xLim)/2;
yLimNew = [0 zoomFactor*diff(yLim)] + yLim(1) + (1-zoomFactor)*diff(yLim)/2;
set(gca,'XLim',xLimNew,'YLim',yLimNew)
elseif currChar=='h',
if helpWasOff
str = { ...
' '
' AXDRAG. Keys you can use are:'
' '
' z, Z: zoom in, zoom out, both dimensions '
' x, X: zoom in, zoom out, x dimension only '
' y, Y: zoom in, zoom out, y dimension only '
' arrow keys: pan the data'
' a: axis auto'
' n: axis normal'
' e: axis equal'
' g: toggle grid state'
' spacebar: toggle axis tick display state'
' h: help'
' '
' Press ''h'' again to dismiss this message'
' ' ...
};
helpTextAxis = axes( ...
'Tag','axdraghelpaxis', ...
'Units','characters', ...
'Position',[2 1 76 16], ...
'Visible','off');
text(0,1,str, ...
'Parent',helpTextAxis, ...
'VerticalAlignment','top', ...
'BackgroundColor',[1 1 0.8], ...
'FontName','courier', ...
'FontSize',6);
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询