MATLAB Answers

Colour of legend doesn't change?

This is my plotting code
%%
%The Spectrum
figure(5);
S1 = (a.^2).*0.5;
plot(f,S1,'b');
hold on;
plot(f,S,'r');
legend ('Initial spectrum','JONSWAP Spectrum');
title('The Spectrum')
xlabel('Frequency (Hz)')
ylabel('Wave spectrum in (m^2/s)')
When I run it I got this one :
figure 5.PNG
The colour of the legend is the same. How can I change it?

  4 Comments

Show 1 older comment
Walter Roberson
on 14 Nov 2019 at 8:10
Is it possible that there is already something drawn in figure 5, and that "hold on" is set? After the figure(5) try cla()
clc
clear all
close all
myData = importdata('bathymetry.dat');
x = myData(:,1);
z = myData(:,2);
%Plot data
figure(1)
plot(x,z,'b')
grid on
axis tight
xlabel ('Longitude {\circ}')
ylabel ('Water depth (m)')
title('Bathymetry Profile')
%%
%Fully-developed sea
load('FDS.mat') %range from -40 to -2.4(the observer)
longitude = FDS(:,1);
depth = -FDS(:,2);
figure(2);
plot(longitude,-depth);
grid on
axis tight
xlabel ('Longitude {\circ}')
ylabel ('Water depth (m)')
title('Fully-Developed Sea Profile')
%Constant variable
gravity = 9.81; %m/s^2
EarthRadius = 6371000; %in m
% Define the frequency vector
Tmin = 3;
Tmax = 30;
nFreq = 30;
fmin = 1/Tmax;
fmax = 1/Tmin;
f = linspace(fmin,fmax,nFreq);
% Define JONSWAP spectrum parameters
Hs = 5; % in m
Tp = 20; % in s
gam = 3; % gamma factor is always greater than 1
%%
% In this function, f=0 should be excluded
S = JONSWAP_spectrum(f,Hs,Tp,gam);
%
figure(3);
plot(f,S)
xlabel('Frequency (Hz)')
ylabel('Wave spectrum in (m^2/s)')
title('The JONSWAP Spectrum')
%set(findall(gcf, '-property', 'FontSize'), 'FontSize', 14)
%}
%%
% Decomposition of the wave spectrum into regular waves
% The row of dt is 30
nWaves = numel(f);
df = f(2) - f(1);
for i = 1:nWaves
a(i,1) = sqrt(2*S(i)*df);
omega(i,1) = 2*pi*f(i);
k(i,1) = kfromw(omega(i,1), depth(1), gravity);
cg(i,1) = 0.5*omega(i,1)/k(i); % check the 1/2 in the formula
xg(i,1) = 0; % Initial position
dt(i,1) = 0; % Initial position
end
%%
% Evolution equations
% The column of dt is 2257
ndepth = size(FDS,1);
for it = 2:ndepth %(in hours)
for i = 1:nWaves
a(i,it) = a(i,1); % no dissipation / dispersion
omega(i,it) = omega(i,1); % the frequency is not changed
k(i,it) = kfromw(omega(i,it), depth(it),gravity); % Wave number associated with depth
cg(i,it) = 0.5*omega(i,1)/k(i,it); % group velocity
dtheta = (longitude(it)-longitude(it-1))/180*pi;
dx = EarthRadius*dtheta;
dt(i,it) = dx/cg(i,it);
end
end

Sign in to comment.

1 Answer

Answer by Walter Roberson
on 14 Nov 2019 at 8:54
 Accepted Answer

Your a looks to have multiple columns, so S1 would have multiple columns.
plot(f,S1,'b');
would generate one line object per column.

  3 Comments

S1 has 2257 column, my problem is the colour of the legend
Walter Roberson
on 14 Nov 2019 at 9:37
If S1 has 2257 columns, then plot(f,S1) will produce 2257 line() objects. When you legend() providing only two texts, then by default legend() will pick up the first two of those, both of which are the ('b') line() objects, so both of them are given blue entries in the legend text.
You have some options:
1)
H1 = plot(f,S1,'b');
hold on;
H = plot(f,S,'r');
legend([H1(1), H(1)], 'Initial spectrum','JONSWAP Spectrum');
2) It is possible to merge all of the first drawing into a single line object by putting NaN between the segments to be drawn, such as
Stemp = S;
Stemp(end+1,:) = nan;
and then plotting Stemp(:) against a version of f that has been adjusted to have appropriate number of copies of the coordinates
3) Leave your existing graphics, but afterwards do
L = plot(nan, nan, 'b', nan, nan, 'r');
legend(L, 'Initial spectrum','JONSWAP Spectrum')
Thank you,I managed to plot it

Sign in to comment.