Legend for varying parameters in a loop

5 views (last 30 days)
Is it possible to have legends such that for xi=-0.06 (I have 4 different plots for various times). I want the lines to have the same color(say blue) and for xi=-0.05 (I have 4 different plots for various times) I want the lines to have the same color(say red)? See the lines concern and I have also attached my codes
f1 = @(xi) theta(1:round(nt/4):nt,:);
hold on
for i = 1:numel(xi)
plot(z, f1(xi(i)))
xlabel('t (seconds)')
ylabel('\theta(z,t)(rad)')
legend('\xi=-0.06', '\xi=-0.05')
end
hold off

Answers (1)

Walter Roberson
Walter Roberson on 16 Sep 2022
plot(z, f1(xi(i)))
assign the result of f1(xi(i)) to a variable, say f1i. Then
h(i, :) = plot(z, f1i(:, 1), 'b', f1i(:, 2), 'r');
Now delete the legend call inside the loop.
After the loop
legend(h(1,:), {'\xi=-0.06', '\xi=-0.05'})
  24 Comments
University Glasgow
University Glasgow on 19 Sep 2022
The code I have here gives legend with same color for corresponding values of xi, which is okay. But I want the legend to appear such that for xi=-0.06 (r), for xi=-0.2 (b) and for xi=-0.06 (c) with only one line representing the 3 lines instead of 3 lines for each value of xi. I don't know if this is possible.
hold on
for ixi = 1:3
if ixi == 1
plot(z, squeeze(THETA(ixi,1:round(nt/3):nt,:)),'r--', 'DisplayName','\xi=-0.06');
elseif ixi==2
plot(z, squeeze(THETA(ixi,1:round(nt/3):nt,:)),'b', 'DisplayName','\xi=-0.2');
else
plot(z, squeeze(THETA(ixi,1:round(nt/3):nt,:)),'c', 'DisplayName','\xi=-0.1');
end
xlabel('t (seconds)')
ylabel('\theta(z,t)(rad)')
end

Sign in to comment.

Products


Release

R2022b

Community Treasure Hunt

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

Start Hunting!