Home > Mobile >  plot in matlab with similar xlabels spaces
plot in matlab with similar xlabels spaces

Time:01-11

I have a figure in matlab, where xlabel is 2 power n values, where n is an integer and ylable includes any vector values, I need to plot those values and keep x vector to be equal spaced. What I do now is correct but small spaces in xlabel are shown when x values are small while that becomes larger with higher values of x values.

That is what I did:

M = [2, 4, 8, 16, 32, 64, 128, 256];
s1 = [104   136   168   200   232   264   296   328]; 

figure(1) 
semilogy(M, s1, 'ko-', 'LineWidth', 1); hold on;   %


grid; 
axis ([0 256 0 600])
xticks(M);

enter image description here

CodePudding user response:

One way to have evenly spaced xticks is to use a linear vector (0,1,2,3,etc...) the same length as your data. You plot your y data versus this xa (X Apparent) vector, then just modify the xticklabels to display the proper power of 2 corresponding to each point.

So for your code:

max_xa = numel(M)-1 ; % number of "x" points (-1 because we'll start at 0)
xa = 0:max_xa ;       % generate the x-apparent vector

figure
% Plot your "sl" values versus "xa"
semilogy(xa, s1, 'ko-', 'LineWidth', 1); grid on ; 
axis ([0 max_xa 0 600]) % adust limits
xticklabels(M);         % adjust X tick labels

will generate the following figure: even X ticks

Note: We started the X apparent vector at 0 instead of 1 because otherwise Matlab would automatically insert a xtick at the value 0 and our xticklabels would all be offset by one position.

CodePudding user response:

Your issues will remain unless you drop some xticklabels or better use loglog to plot which retains the feature of parabolic data.

Use following lines in your code: loglog(M, s1, 'ko-', 'LineWidth', 1); hold on; %

You will get this result 1

  • Related