Matlab 图例与情节不符
Matlab legend does not match plot
我使用以下代码生成我的绘图(我希望它稍后包含在我的 LateX 文档中):
clear all; close all; clc;
a1 = 1; a2 = 1; c1 = 2.0; c2 = 1.8; time = 0:0.1:300;
wave1 = a1 * sin(c1*time);
wave2 = a2 * sin(c2*time);
wave3 = wave1 + wave2;
y = hilbert(wave3);
env = abs(y);
bound = 0.1*cos(0.2*time-pi);
plot(time,wave3,'k',time,[-1;1]*env,'--k',time,bound,'-.r', 'Linewidth',1.2);
ylabel(' $\eta$ (m)');
xlabel(' Time (s)');
legend = legend({'Short waves','Wave group envelope','Bound long wave'});
set(legend, 'FontSize',20);
axis([15.7 110 -2.5 2.5]);
图表看起来像:
显然,'bound long wave' 图例与图中的颜色和线条规范不匹配。
据我所知,它与scalar/vector有关,但我无法找出错误所在。
如何进行下一步?
您实际上创建了四个绘图对象,但只为其中三个提供了图例。如果我们分解您的 plot
语句,我们可以计算这些图(请记住,使用矩阵图调用的 plot
将每个 列 作为不同的图)。
plot(time, wave3, 'k', ... <--- This is one plot
time, [-1;1]*env, '--k', ... <--- This is TWO plots (one negative, one positive)
time, bound, '-.r', ... <--- This is one plot
'Linewidth',1.2);
当您仅使用三个标签调用 legend
时,它只会为前三个绘图对象创建一个图例条目 。
由于您可能不希望信封有两个条目,因此您应该将 plot
的输出分配给一个变量和 pass the handles explicitly to the legend
.
p = plot(time, wave3, 'k', ...
time, [-1; 1] * env, '--k', ...
time, bound, '-.r', ...
'LineWidth', 1.2);
% Skip the third plot since that will just be the "negative" envelope
legend(p([1 2 4]), {'Short waves','Wave group envelope','Bound long wave'});
我使用以下代码生成我的绘图(我希望它稍后包含在我的 LateX 文档中):
clear all; close all; clc;
a1 = 1; a2 = 1; c1 = 2.0; c2 = 1.8; time = 0:0.1:300;
wave1 = a1 * sin(c1*time);
wave2 = a2 * sin(c2*time);
wave3 = wave1 + wave2;
y = hilbert(wave3);
env = abs(y);
bound = 0.1*cos(0.2*time-pi);
plot(time,wave3,'k',time,[-1;1]*env,'--k',time,bound,'-.r', 'Linewidth',1.2);
ylabel(' $\eta$ (m)');
xlabel(' Time (s)');
legend = legend({'Short waves','Wave group envelope','Bound long wave'});
set(legend, 'FontSize',20);
axis([15.7 110 -2.5 2.5]);
图表看起来像:
显然,'bound long wave' 图例与图中的颜色和线条规范不匹配。 据我所知,它与scalar/vector有关,但我无法找出错误所在。
如何进行下一步?
您实际上创建了四个绘图对象,但只为其中三个提供了图例。如果我们分解您的 plot
语句,我们可以计算这些图(请记住,使用矩阵图调用的 plot
将每个 列 作为不同的图)。
plot(time, wave3, 'k', ... <--- This is one plot
time, [-1;1]*env, '--k', ... <--- This is TWO plots (one negative, one positive)
time, bound, '-.r', ... <--- This is one plot
'Linewidth',1.2);
当您仅使用三个标签调用 legend
时,它只会为前三个绘图对象创建一个图例条目 。
由于您可能不希望信封有两个条目,因此您应该将 plot
的输出分配给一个变量和 pass the handles explicitly to the legend
.
p = plot(time, wave3, 'k', ...
time, [-1; 1] * env, '--k', ...
time, bound, '-.r', ...
'LineWidth', 1.2);
% Skip the third plot since that will just be the "negative" envelope
legend(p([1 2 4]), {'Short waves','Wave group envelope','Bound long wave'});