MATLAB图例不适用于绘制2个圆圈

时间:2017-02-12 08:43:23

标签: matlab legend

close all; clc; clear all;
A0 = 1.5; % meters
lambda = 100 % meters
k = (2*pi)/lambda;
T = 3600 % Period in seconds
ome = 2*pi/T; % omega
x = 0; z = 0;
t = linspace(0,7200,100); % 2 periods, 100 data
zz=0;
for z = 0:20:20;
   zz = zz+1;
   % multiplied by 100, unit in cm/s
   u= 100.*ome*A0*exp(-k*z)*sin(k*x - ome*t);
   w = 100.*-ome*A0*exp(-k*z)*cos(k*x - ome*t);
   uu(zz,:) = u; % size(uu) 2   100
   ww(zz,:) = w; % size(ww) 2   100
end

figure(1)
color = -0.8;
for zz = 1:2
   color = color + 0.8;
   for i=1:3:49; % plot circle for one period
      plot([uu(zz,i) uu(zz,i+3)],[ww(zz,i) ww(zz,i+3)], 'color',([color+0.2 0 0]), 'linewidth', 2)
      hold on
   end
end
title('Plot of lines from (0,0) to (u(i), v(i). Radius or amplitude in cm/s')
axis equal;
grid on;
legend('radius at surface','radius at depth 20')%

此脚本绘制2个圆圈:小圆圈为红色,另一个为黑色圆圈。但传说并不是与这些圈子的一致。

1 个答案:

答案 0 :(得分:1)

这是因为你在轴上创建了许多线对象(当你执行plot时每次循环迭代)并且图例函数处理这些线对象。 sp line1和line2仍然是你绘制的多边形的一部分。

我会回答您的问题,但要知道您的代码不是最佳的,并且它不是绘制2个圆或多边形的最佳方式。

因此,只需对代码进行最少的更改,就可以执行以下操作:

 ....
figure(1)
color = -0.8;
for zz = 1:2
   color = color + 0.8;
   for i=1:3:49; % plot circle for one period
      h(zz)=plot([uu(zz,i) uu(zz,i+3)],[ww(zz,i) ww(zz,i+3)], 'color',([color+0.2 0 0]), 'linewidth', 2)
      hold on
   end
end
title('Plot of lines from (0,0) to (u(i), v(i). Radius or amplitude in cm/s')
axis equal;
grid on;
legend([h(1) h(2)],{'radius at surface','radius at depth 20'});  

enter image description here