2 views (last 30 days)

Muhammad Fakhruddin Abd Rahim
on 14 Nov 2019

Commented: Muhammad Fakhruddin Abd Rahim
on 14 Nov 2019

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 :

The colour of the legend is the same. How can I change it?

Walter Roberson
on 14 Nov 2019

Your a looks to have multiple columns, so S1 would have multiple columns.

plot(f,S1,'b');

would generate one line object per column.

Walter Roberson
on 14 Nov 2019

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')

Sign in to comment.

Sign in to answer this question.

Opportunities for recent engineering grads.

Apply Today
## 4 Comments

## Direct link to this comment

https://nl.mathworks.com/matlabcentral/answers/491030-colour-of-legend-doesn-t-change#comment_767173

⋮## Direct link to this comment

https://nl.mathworks.com/matlabcentral/answers/491030-colour-of-legend-doesn-t-change#comment_767173

## Direct link to this comment

https://nl.mathworks.com/matlabcentral/answers/491030-colour-of-legend-doesn-t-change#comment_767174

⋮## Direct link to this comment

https://nl.mathworks.com/matlabcentral/answers/491030-colour-of-legend-doesn-t-change#comment_767174

## Direct link to this comment

https://nl.mathworks.com/matlabcentral/answers/491030-colour-of-legend-doesn-t-change#comment_767180

⋮## Direct link to this comment

https://nl.mathworks.com/matlabcentral/answers/491030-colour-of-legend-doesn-t-change#comment_767180

## Direct link to this comment

https://nl.mathworks.com/matlabcentral/answers/491030-colour-of-legend-doesn-t-change#comment_767183

⋮## Direct link to this comment

https://nl.mathworks.com/matlabcentral/answers/491030-colour-of-legend-doesn-t-change#comment_767183

Sign in to comment.