Filter löschen
Filter löschen

Legend for varying parameters in a loop

3 Ansichten (letzte 30 Tage)
University Glasgow
University Glasgow am 16 Sep. 2022
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

Antworten (1)

Walter Roberson
Walter Roberson am 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 Kommentare
University Glasgow
University Glasgow am 19 Sep. 2022
Thank you so much
University Glasgow
University Glasgow am 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

Melden Sie sich an, um zu kommentieren.

Kategorien

Mehr zu Graphics Performance finden Sie in Help Center und File Exchange

Produkte


Version

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by