How can I include the legends within the for loop and how can I set the xticks in MATLAB?

2 次查看(过去 30 天)
I have below the following data
Q = [16,32,64,128,256,512,1024];
VEC_5 = [0.2380 0.2380 0.2380 0.2380 0.2380 0.2380 0.2380];
VEC_10 = [1.1898 1.1898 0.2380 0.2380 0.2380 0.2380 0.2380];
VEC_15 = [2.1416 2.1416 2.1416 1.1898 1.1898 0.2380 0.2380];
VEC_20 = [4.9970 3.0934 2.1416 2.1416 2.1416 1.1898 1.1898];
VEC_25 = [4.9970 4.9970 4.9970 3.0934 2.1416 2.1416 1.1898];
VEC_30 = [8.8043 5.9488 4.9970 4.9970 3.0934 2.1416 1.1898];
VEC_35 = [10.7079 8.8043 6.9007 4.9970 4.9970 2.1416 2.1416];
VEC_40 = [14.5152 10.7079 8.8043 6.9007 4.9970 3.0934 2.1416];
VEC_45 = [16.4188 11.6597 10.7079 8.8043 4.9970 4.9970 2.1416];
VEC_50 = [19.2742 14.5152 10.7079 8.8043 5.9488 4.9970 2.1416];
VEC_55 = [21.1779 16.4188 13.5634 10.7079 6.9007 4.9970 2.1416];
VEC_60 = [23.5574 16.4188 14.5152 10.7079 8.8043 4.9970 2.6175];
I drew them with semilogx function in Matlab as shown below:
as demonstrated in the code below:
VEC = [VEC_5;VEC_10;VEC_15;VEC_20;VEC_25;VEC_30;VEC_35;VEC_40;VEC_45;VEC_50;VEC_55;VEC_60];
figure
cmap = jet(12);
hold on
for k = 1:12
semilogx(Q,VEC(k,:), 'Color', cmap(k, :),'LineWidth',2,'MarkerEdgeColor','b');
end
hold off
xticks(Q)
xlabel('Q order');
ylabel('Coverage area');
legend('\theta_1_/_2 = 5','\theta_1_/_2 = 10','\theta_1_/_2 = 15','\theta_1_/_2 = 20','\theta_1_/_2 = 25','\theta_1_/_2 = 30','\theta_1_/_2 = 35','\theta_1_/_2 = 40','\theta_1_/_2 = 45','\theta_1_/_2 = 50','\theta_1_/_2 = 55','\theta_1_/_2 = 60')
grid on;
So, how can I include legends with the for loop instead of writing it outside? and the values in the x-axis are too close to each other how can I separate them to something like below? and how can I add markers on the lines?

采纳的回答

Matt J
Matt J 2022-9-25
编辑:Matt J 2022-9-25
So, how can I include legends with the for loop instead of writing it outside?
You are assuming here that the for loop is the only way to automatically generate the legend strings. Not so:
Q = [16,32,64,128,256,512,1024];
VEC_5 = [0.2380 0.2380 0.2380 0.2380 0.2380 0.2380 0.2380];
VEC_10 = [1.1898 1.1898 0.2380 0.2380 0.2380 0.2380 0.2380];
VEC_15 = [2.1416 2.1416 2.1416 1.1898 1.1898 0.2380 0.2380];
VEC_20 = [4.9970 3.0934 2.1416 2.1416 2.1416 1.1898 1.1898];
VEC_25 = [4.9970 4.9970 4.9970 3.0934 2.1416 2.1416 1.1898];
VEC_30 = [8.8043 5.9488 4.9970 4.9970 3.0934 2.1416 1.1898];
VEC_35 = [10.7079 8.8043 6.9007 4.9970 4.9970 2.1416 2.1416];
VEC_40 = [14.5152 10.7079 8.8043 6.9007 4.9970 3.0934 2.1416];
VEC_45 = [16.4188 11.6597 10.7079 8.8043 4.9970 4.9970 2.1416];
VEC_50 = [19.2742 14.5152 10.7079 8.8043 5.9488 4.9970 2.1416];
VEC_55 = [21.1779 16.4188 13.5634 10.7079 6.9007 4.9970 2.1416];
VEC_60 = [23.5574 16.4188 14.5152 10.7079 8.8043 4.9970 2.6175];
VEC = [VEC_5;VEC_10;VEC_15;VEC_20;VEC_25;VEC_30;VEC_35;VEC_40;VEC_45;VEC_50;VEC_55;VEC_60];
figure
cmap = jet(12);
hold on
for k = 1:12
plot(log2(Q),VEC(k,:), 'Color', cmap(k, :),'LineWidth',2,'MarkerEdgeColor','b');
end
hold off
xticks(log2(Q))
xticklabels(string(Q))
xlabel('Q order');
ylabel('Coverage area');
legstr="\theta_1_/_2 ="+(5:5:60);
legend(legstr)
grid on;
  5 个评论

请先登录,再进行评论。

更多回答(0 个)

类别

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

标签

Community Treasure Hunt

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

Start Hunting!

Translated by