Legend for multiple plots varying parameters

Pleas, how do I make my legend to match with my data? I have attached the figure. I want the legend data1 to corresponds to xi = -0.06, t=0, data2 to corresponds to xi=-0.05, t=0, data3: xi = -0.06, t=25, data4: xi = -0.05, t=25 and so on ..
% matlab and maple for theta plot for different xi values
figure
subplot(2,1,1)
hold on
for ixi = 1:2
if ixi == 1
plot(z, squeeze(THETA(ixi,it,:)), 'c', z2, thetasolxi,'r');
else
plot(z, squeeze(THETA(ixi,it,:)),'g--', z2, thetasolxi2, 'b--');
end
xlabel('z (\mu m)')
ylabel('\theta(z,t)(rad)')
title('Comparison of \theta(z,t) plot for \xi for maple and matlab' )
end
legend('show')
% matlab and maple for v plot for different xi values
subplot(2,1,2)
hold on
for ixi = 1:2
if ixi == 1
plot(z, squeeze(V(ixi,it,:)), 'c', z2, vsolxi2,'r--');
else
plot(z, squeeze(V(ixi,it,:)),'g', z2, vsolxi, 'b--');
end
xlabel('z (\mu m)')
ylabel('v(z,t)(m/s)')
title('Comparison of v(z,t) plot for \xi for maple and matlab' )
end
legend('show')

2 件のコメント

Sharmin Kibria
Sharmin Kibria 2022 年 9 月 25 日
You can try
legend('xi = -0.06, t=0', 'xi=-0.05, t=0', 'xi = -0.06, t=25', 'xi = -0.05, t=25') . The number of character vectors in legend must be equal to the number of data you are plotting. Otherwise it will error out.
University Glasgow
University Glasgow 2022 年 9 月 25 日
Okay, thank you

サインインしてコメントする。

 採用された回答

Walter Roberson
Walter Roberson 2022 年 9 月 25 日

0 投票

% matlab and maple for theta plot for different xi values
figure
subplot(2,1,1)
hold on
for ixi = 1:2
xi_part = "xi = " + Xis(ixi);
t_part = ", t = " + Times(it);
dn = xi_part + t_part;
if ixi == 1
plot(z, squeeze(THETA(ixi,it,:)), 'c', z2, thetasolxi,'r', 'DisplayName', dn);
else
plot(z, squeeze(THETA(ixi,it,:)),'g--', z2, thetasolxi2, 'b--', 'DisplayName', dn);
end
xlabel('z (\mu m)')
ylabel('\theta(z,t)(rad)')
title('Comparison of \theta(z,t) plot for \xi for maple and matlab' )
end
legend('show')
% matlab and maple for v plot for different xi values
subplot(2,1,2)
hold on
for ixi = 1:2
xi_part = "xi = " + Xis(ixi);
t_part = ", t = " + Times(it);
dn = xi_part + t_part;
if ixi == 1
plot(z, squeeze(V(ixi,it,:)), 'c', z2, vsolxi2,'r--', 'DisplayName', dn);
else
plot(z, squeeze(V(ixi,it,:)),'g', z2, vsolxi, 'b--', 'DisplayName', dn);
end
xlabel('z (\mu m)')
ylabel('v(z,t)(m/s)')
title('Comparison of v(z,t) plot for \xi for maple and matlab' )
end
legend('show')

5 件のコメント

University Glasgow
University Glasgow 2022 年 9 月 25 日
Hi, thank you for this but i it returned this error:
Unrecognized function or variable 'Xis'.
I have attached my code
Walter Roberson
Walter Roberson 2022 年 9 月 25 日
編集済み: Walter Roberson 2022 年 9 月 25 日
you have
for xi = [-0.05, -0.06]
Instead,
Xis = [-0.05, - 0.06];
for xiidx = 1:length(Xis)
xi = Xis(xiidx);
I am unable to find a definition for "it" in your code, so I am not sure what should be used to initialize Times
University Glasgow
University Glasgow 2022 年 9 月 25 日
編集済み: University Glasgow 2022 年 9 月 25 日
Thank you. My it = 1:round(nt/4):nt. The code it is working but the times are still the same. Find attached the figure
Torsten
Torsten 2022 年 9 月 25 日
編集済み: Torsten 2022 年 9 月 25 日
We cannot see where and how you use this
it = 1:round(nt/4):nt
in your plotting part.
As far as I can see, "it" is never set in the code you attached.
University Glasgow
University Glasgow 2022 年 9 月 25 日
Thank you , after the defining the it, Is working now.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

製品

リリース

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by