Legend for varying parameters in a loop

7 次查看(过去 30 天)
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

回答(1 个)

Walter Roberson
Walter Roberson 2022-9-16
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 个评论
University Glasgow
University Glasgow 2022-9-19
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

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Legend 的更多信息

产品


版本

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by