matlab画图的记录

本文详细介绍了在Matlab中优化绘图效果的方法,包括如何正确使用marker参数来区分线和点,以及如何调整legend显示。此外,还提供了两种实现局部放大的实用技巧,一是使用自定义的magnify函数,二是利用axes函数创建多个坐标轴以展示不同比例的图表。

在使用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))

在这里插入图片描述

### MATLAB绘图命令教程 #### 创建基本图形 在MATLAB中创建图形通常涉及定义数据集并调用`plot`函数来可视化这些数据。对于简单的线形图,可以传递两个向量作为参数给`plot`函数,第一个代表横坐标值,第二个代表纵坐标值。 ```matlab % 定义时间轴 t 和信号 y1, y2 t = linspace(0, 2*pi, 100); y1 = sin(t); y2 = cos(t); % 绘制第一条曲线 (正弦波) figure; plot(t, y1, '-r'); % 使用红色实线绘制 title('Sine Wave'); xlabel('Time(s)'); ylabel('Amplitude'); hold on; % 绘制第二条曲线 (余弦波), 同一窗口内不同颜色区分 plot(t, y2, '--b'); % 使用蓝色虚线绘制 legend('Sin(t)', 'Cos(t)'); % 添加图例说明两条曲线含义 ``` 上述代码展示了如何在同一张图表上出两组不同的数据序列,并通过设置线条样式(`'-r'`, `'--b'`)使它们易于分辨[^1]。 #### 处理多个子图 当需要展示多幅独立但又相互关联的小型图表时,可利用`subplot`功能分割当前Figure窗口为若干区域,在各自区域内分别作图: ```matlab % 设置三个子图布局 subplot(3, 1, 1); % 第一行位置放置第一个子图 plot(t, exp(-t).*sin(5*t)); title('Damped Sine Wave'); subplot(3, 1, 2); % 第二行位置放置第二个子图 bar(randn(1, 10)); title('Random Bar Chart'); subplot(3, 1, 3); % 第三行位置放置第三个子图 histogram(randn(1e4, 1)); title('Gaussian Distribution Histogram'); ``` 这段脚本先建立了由垂直排列的三个部分组成的界面结构,接着依次填充各个分区内应呈现的内容[^3]。 #### 数据保存与加载 完成计算过程后,有时希望保留某些中间结果以便后续分析或分享成果。此时可以通过`save`指令将工作区内的特定变量导出至外部文件;而要读取之前存储的数据,则借助于`load`操作。 ```matlab v = randperm(100, 10); % 生成随机整数数组 v 包含十个元素 save('myData.mat', 'v'); % 将变量 v 存储到 myData.mat 文件当中 clear v; % 清除内存中的原始副本 whos % 查看当前环境下的所有变量列表 load('myData.mat') % 重新导入先前保存过的数据项 disp(v); % 显示已恢复的数值集合 ``` 这里介绍了怎样简单地管理临时性的实验记录或是长期项目资料库,确保研究工作的连续性和可重复性[^2].
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值