matlab畫圖的記錄

在使用matlab畫圖的過程中,我們可能需要在一張圖中同時畫出各種線、標出各種點,加上各種legend。
但是在加legend的時候,出現這種情況

代碼爲:

plot(x,y,'marker','o','markerfacecolor','r')
hold on
plot(x(end),y(end),'marker','o','markerfacecolor','k')
legend('一串紅點','最後一個黑點')

畫出的圖是這樣的:
在這裏插入圖片描述

修改一下代碼:

plot(x,y,'marker','o','markerfacecolor','r')
hold on
plot(x(end),y(end),'o','markerfacecolor','k')
legend('一串紅點','最後一個黑點')

畫出的圖爲:
在這裏插入圖片描述
這樣就可以達到想要的效果。
其實使用“marker”這個東西,表示所畫的是一條線,所以matlab在黑點上畫了一條線,直接加“o”,表示畫的是一個點,所以legend就是一個點。

如果只要點,不要線的話,只要去掉第一個plot的marker就可以。這樣所有的紅點不會被線連接。

有時候,需要對仿真圖進行局部放大,網上查到了兩種方法,一個使用使用magnify.m文件,個人體驗一般。

function magnify(f1)
%
%magnify(f1)
%
%  Figure creates a magnification box when under the mouse
%  position when a button is pressed.  Press '+'/'-' while
%  button pressed to increase/decrease magnification. Press
%  '>'/'<' while button pressed to increase/decrease box size.
%  Hold 'Ctrl' while clicking to leave magnification on figure.
%
%  Example:
%     plot(1:100,randn(1,100),(1:300)/3,rand(1,300)), grid on,
%     magnify;

% Rick Hindman - 7/29/04

if (nargin == 0), f1 = gcf; end;
set(f1, ...
  'WindowButtonDownFcn',  @ButtonDownCallback, ...
  'WindowButtonUpFcn', @ButtonUpCallback, ...
  'WindowButtonMotionFcn', @ButtonMotionCallback, ...
  'KeyPressFcn', @KeyPressCallback);
return;

function ButtonDownCallback(src,eventdata)
f1 = src;
a1 = get(f1,'CurrentAxes');
a2 = copyobj(a1,f1);

set(f1, ...
  'UserData',[f1,a1,a2], ...
  'Pointer','fullcrosshair', ...
  'CurrentAxes',a2);
set(a2, ...
  'UserData',[2,0.2], ...  %magnification, frame size
  'Color',get(a1,'Color'), ...
  'Box','on');
xlabel(''); ylabel(''); zlabel(''); title('');
set(get(a2,'Children'), ...
  'LineWidth', 2);
set(a1, ...
  'Color',get(a1,'Color')*0.95);
set(f1, ...
  'CurrentAxes',a1);
ButtonMotionCallback(src);
return;

function ButtonUpCallback(src,eventdata)
H = get(src,'UserData');
f1 = H(1); a1 = H(2); a2 = H(3);
set(a1, ...
  'Color',get(a2,'Color'));
set(f1, ...
  'UserData',[], ...
  'Pointer','arrow', ...
  'CurrentAxes',a1);
if ~strcmp(get(f1,'SelectionType'),'alt'),
  delete(a2);
end;
return;

function ButtonMotionCallback(src,eventdata)
H = get(src,'UserData');
if ~isempty(H)
  f1 = H(1); a1 = H(2); a2 = H(3);
  a2_param = get(a2,'UserData');
  f_pos = get(f1,'Position');
  a1_pos = get(a1,'Position');
  
  [f_cp, a1_cp] = pointer2d(f1,a1);
  
  set(a2,'Position',[(f_cp./f_pos(3:4)) 0 0]+a2_param(2)*a1_pos(3)*[-1 -1 2 2]);
  a2_pos = get(a2,'Position');
  
  set(a2,'XLim',a1_cp(1)+(1/a2_param(1))*(a2_pos(3)/a1_pos(3))*diff(get(a1,'XLim'))*[-0.5 0.5]);
  set(a2,'YLim',a1_cp(2)+(1/a2_param(1))*(a2_pos(4)/a1_pos(4))*diff(get(a1,'YLim'))*[-0.5 0.5]);
end;
return;

function KeyPressCallback(src,eventdata)
H = get(gcf,'UserData');
if ~isempty(H)
  f1 = H(1); a1 = H(2); a2 = H(3);
  a2_param = get(a2,'UserData');
  if (strcmp(get(f1,'CurrentCharacter'),'+') | strcmp(get(f1,'CurrentCharacter'),'='))
    a2_param(1) = a2_param(1)*1.2;
  elseif (strcmp(get(f1,'CurrentCharacter'),'-') | strcmp(get(f1,'CurrentCharacter'),'_'))
    a2_param(1) = a2_param(1)/1.2;
  elseif (strcmp(get(f1,'CurrentCharacter'),'<') | strcmp(get(f1,'CurrentCharacter'),','))
    a2_param(2) = a2_param(2)/1.2;
  elseif (strcmp(get(f1,'CurrentCharacter'),'>') | strcmp(get(f1,'CurrentCharacter'),'.'))
    a2_param(2) = a2_param(2)*1.2;
  end;
  set(a2,'UserData',a2_param);
  ButtonMotionCallback(src);
end;
return;



% Included for completeness (usually in own file)
function [fig_pointer_pos, axes_pointer_val] = pointer2d(fig_hndl,axes_hndl)
%
%pointer2d(fig_hndl,axes_hndl)
%
%	Returns the coordinates of the pointer (in pixels)
%	in the desired figure (fig_hndl) and the coordinates
%       in the desired axis (axes coordinates)
%
% Example:
%  figure(1),
%  hold on,
%  for i = 1:1000,
%     [figp,axp]=pointer2d;
%     plot(axp(1),axp(2),'.','EraseMode','none');
%     drawnow;
%  end;
%  hold off

% Rick Hindman - 4/18/01

if (nargin == 0), fig_hndl = gcf; axes_hndl = gca; end;
if (nargin == 1), axes_hndl = get(fig_hndl,'CurrentAxes'); end;

set(fig_hndl,'Units','pixels');

pointer_pos = get(0,'PointerLocation');	%pixels {0,0} lower left
fig_pos = get(fig_hndl,'Position');	%pixels {l,b,w,h}

fig_pointer_pos = pointer_pos - fig_pos([1,2]);
set(fig_hndl,'CurrentPoint',fig_pointer_pos);

if (isempty(axes_hndl)),
  axes_pointer_val = [];
elseif (nargout == 2),
  axes_pointer_line = get(axes_hndl,'CurrentPoint');
  axes_pointer_val = sum(axes_pointer_line)/2;
end;



另外一種方法是使用axes函數,例如:
第一步:畫出一個大圖一個小圖的座標軸

figure
ax1 = axes('Position',[0.1 0.1 0.7 0.7]);
ax2 = axes('Position',[0.65 0.65 0.28 0.28]);

第二步,把內容畫到圖中

contour(ax1,peaks(20))
surf(ax2,peaks(20))

在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章