0

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:

enter image description here

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?

enter image description here

SH_IQ
  • 631
  • 5
  • 14
  • Don’t create variable names like `VEC_5`, `VEC_10`, etc. put all your arrays into a cell array instead. See e.g. here: https://stackoverflow.com/a/32467170/7328782 – Cris Luengo Sep 25 '22 at 13:02
  • @CrisLuengo I got your meaning sir; I am gonna try it. – SH_IQ Sep 25 '22 at 13:05
  • The easiest way might be to have a variable called `legendTitles`. You can update this in each interation of the `for` loop using something along the lines of `legendTitles = [legendTitles; currentTitle];` and then just make the legend once at the end using `legend(legendTitles);` – magnesium Sep 26 '22 at 05:08
  • If you have a look at you axes ticks - you will see that they are not actually logarithmic! i.e. `2^5 = 32` should be halfway between `2^0 = 1` and `2^10=1032`. Your axes have defaulted to a linear scale because of your call to `hold` before plotting using `semilogx`. If you move your call to `hold on` after your call to `semilogx` inside the for loop (i.e. `for k = 1:12; semilogx(Q, VEC...); hold on; end;` that will fix the issue with the x-axis spacing. Then follow this up with `grid on` to add in the gridlines. – magnesium Sep 26 '22 at 05:17
  • Finally, there are two options that I know of for adding markers to the lines. 1) You can add in some markers using the `"LineSpec"` Name-Value pairing e.g. `plot(..., 'LineSpec', '^')` to create a triangular marker. 2) You can combine calls to `plot` (for the line) with calls to `scatter` to create the markers e.g. `for k = 1:12; plot(Q, VEC(:,k)); hold on; scatter(Q, VEC(:,k), 'd'); end;`. NB. Note that creating a different marker for each line might take a little more care/code/thought. – magnesium Sep 26 '22 at 05:22

0 Answers0