Legend does not match plot

2 ビュー (過去 30 日間)
André
André 2016 年 6 月 22 日
コメント済み: Walter Roberson 2016 年 6 月 22 日
I used the following code to generate my plot (and I want it later to include in my LateX document):
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]);
set(groot, 'defaultAxesTickLabelInterpreter','latex'); set(groot, 'defaultLegendInterpreter','latex');
The graph looks like:
Clearly, the 'bound long wave' legend does not match the color and line specification in the graph. As far as I know, it has something to do with scalar/vector but I cannot figure out where the error is.
How to proceed further?

採用された回答

Walter Roberson
Walter Roberson 2016 年 6 月 22 日
Inside the plot, time,[-1;1]*env generates two lines, for a total of 4 overall. But you only legend() for three lines. The fix is:
h = plot(time, wave3, 'k', time, [-1;1]*env, '--k', time, bound, '-.r', 'Linewidth', 1.2);
Lh = legend(h([1, 2, 4]), {'Short waves','Wave group envelope','Bound long wave'});
set(Lh, 'FontSize',20);

その他の回答 (1 件)

Muhammad Usman Saleem
Muhammad Usman Saleem 2016 年 6 月 22 日
let's fix this bug in your code, replace with this
legend = legend('Short waves','Wave group envelope','Bound long wave');
  3 件のコメント
Guillaume
Guillaume 2016 年 6 月 22 日
Indeed, there is absolutely no difference in passing the legend strings as a comma-separated list or as a cell array (or a char array)
Walter Roberson
Walter Roberson 2016 年 6 月 22 日
Passing as a cell array can be used to pass in some legend strings that would otherwise be interpreted as options.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeLegend についてさらに検索

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by