setting xaxis labels to only integers

Hello,
I have a plot where I am trying to make it so that the xaxis is going to be integers and steps of 1.
figure();
adcValues_flipped = flip(adcValues);
xlabels_flipped = flip(num2str(x_mm_flipped));
b0_flipped = flip(b0Values);
plot(1:numel(x_mm), adcValues_flipped);
set(gca, 'XTick', 1:numel(x_mm), 'XTickLabel', xlabels_flipped);
Right now the code about gives me an xaxis labels of the following:
'-4.95'
' -4.5'
'-4.05'
' -3.6'
'-3.15'
' -2.7'
'-2.25'
' -1.8'
'-1.35'
' -0.9'
'-0.45'
' 0'
' 0'
' 0'
' 0'
' 0'
' 0'
' 0.45'
' 0.9'
' 1.35'
' 1.8'
' 2.25'
' 2.7'
' 3.15'
' 3.6'
' 4.05'
' 4.5'
' 4.95'
' 5.4'
' 5.85'
Where I would instead like it to be -5, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6 as the xaxis labels and ticks.
Any ideas?

 Réponse acceptée

hello
using your data I tried to reproduce your plot in figure 1
the modified plot is figure 2
hope it helps !
x_mm = [-4.95
-4.5
-4.05
-3.6
-3.15
-2.7
-2.25
-1.8
-1.35
-0.9
-0.45
0
0
0
0
0
0
0.45
0.9
1.35
1.8
2.25
2.7
3.15
3.6
4.05
4.5
4.95
5.4
5.85];
adcValues_flipped = 1+0.25*sin(x_mm);
% remove duplicates
[x_mm,ind] = unique(x_mm);
adcValues_flipped = adcValues_flipped(ind);
figure(1) % your plot
plot(x_mm, adcValues_flipped);
set(gca, 'XTick', x_mm, 'XTickLabel', num2str(x_mm));
figure(2) % modified plot
xi = floor(min(x_mm)):1:ceil(max(x_mm));
plot(x_mm, adcValues_flipped);
set(gca, 'XTick', xi, 'XTickLabel', num2str(xi'));

11 commentaires

nina
nina le 12 Juin 2025
Hello,
Thanks for your help -- this won't work though because in the graphs I do not want the zeros collapsed.
Here is an example of what I would want on the right side. You can see that we still have all the zeros
Mathieu NOE
Mathieu NOE le 12 Juin 2025
ok seems I have already answered this question here :
so the only thing you need now is to change the xtick labels for integers ?
nina
nina le 12 Juin 2025
You've answered an old question, but yes, I am stuck on the xtick labels for integers.
so using the same logic , adapted to your new data , this is what I could suggest :
%% create the data
x_mm = [-4.95
-4.5
-4.05
-3.6
-3.15
-2.7
-2.25
-1.8
-1.35
-0.9
-0.45
0
0
0
0
0
0
0.45
0.9
1.35
1.8
2.25
2.7
3.15
3.6
4.05
4.5
4.95
5.4
5.85];
adcValues = 1+0.25*sin(x_mm); % dummy Adc readings
ind = abs(x_mm)<eps;
adcValues(ind) = 0; % create the 0 Adc values
%% create new x vector
indXzero = find(abs(x_mm)<eps);
xspacing_zero = 1; % delta x spacing between zero values
xspacing_zero_array = linspace(-xspacing_zero/2,xspacing_zero/2,numel(indXzero));
x_mm_new = x_mm;
x_mm_new(indXzero) = xspacing_zero_array;
% shift left hand side non zero x values (to have correct spacing)
indleft = (1:min(indXzero)-1);
x_mm_new(indleft) = x_mm(indleft) + xspacing_zero_array(1);
% shift right hand side non zero x values (to have correct spacing)
indright = (max(indXzero)+1:numel(x_mm));
x_mm_new(indright) = x_mm(indright) + xspacing_zero_array(end);
% plot
xlabels = num2str(x_mm); % plot "true" x_mm values at the specified ticks positions
%% plots
figure(1) % your original plot
plot(x_mm_new,adcValues,'-*');
set(gca,'XTick',x_mm_new,'XTickLabel',xlabels);
figure(2) % modified plot
xi = floor(min(x_mm_new)):1:ceil(max(x_mm_new));
plot(x_mm_new, adcValues,'-*');
set(gca, 'XTick', xi, 'XTickLabel', num2str(xi'));
nina
nina le 12 Juin 2025
thank you so so much!!!!!!!!!!!!
Mathieu NOE
Mathieu NOE le 12 Juin 2025
my pleasure !!!!!!!!!!!!!
nina
nina le 13 Juin 2025

Hey Mathieu,

Ok so getting close but this answer is also incorrect - the axis for the second answer is not giving the actual values for the plot. The dots should be like 0.45, 0.9 etc etc and they’re off quite significantly

nina
nina le 13 Juin 2025

Figure 1 is correct, but then we are at my original problem where I want the rounded numbers on the x axis.

oh yes I am sorry , my bad ,
try this instead : I have now plotted the two on the same figure to make sure the problem is now solved
NB that your "original" plot looks also a bit different as I also apply a unitary x spacing for it (and that is fortunate, otherwise the comparison with the modified plot becomes quite problematic !)
clc
clearvars
%% create the data
x_mm = [-4.95
-4.5
-4.05
-3.6
-3.15
-2.7
-2.25
-1.8
-1.35
-0.9
-0.45
0
0
0
0
0
0
0.45
0.9
1.35
1.8
2.25
2.7
3.15
3.6
4.05
4.5
4.95
5.4
5.85];
adcValues = 1+0.25*sin(x_mm); % dummy Adc readings
ind = abs(x_mm)<eps;
adcValues(ind) = 0; % create the 0 Adc values
%% create new x vector
indXzero = find(abs(x_mm)<eps);
xspacing_zero_array = 0:numel(indXzero)-1;
x_mm_new = x_mm;
x_mm_new(indXzero) = xspacing_zero_array;
% shift left hand side non zero x values (to have correct spacing)
indleft = (1:min(indXzero)-1);
x_mm_new(indleft) = x_mm(indleft) + xspacing_zero_array(1);
% shift right hand side non zero x values (to have correct spacing)
indright = (max(indXzero)+1:numel(x_mm));
x_mm_new(indright) = x_mm(indright) + xspacing_zero_array(end);
% original plot labels
xlabels = num2str(x_mm); % plot "true" x_mm values at the specified ticks positions
% new plot x labels
xi_left = (floor(min(x_mm)):-1);
xi_right = (1:ceil(max(x_mm)));
xi = [xi_left zeros(1,numel(indXzero)) xi_right] ;
xi_labels = num2str(xi');
xi_ticks = [xi_left (0:numel(indXzero)-1) xi_right+numel(indXzero)-1] ;
%% plots
figure(1)
% your original plot
subplot(2,1,1)
plot(x_mm_new,adcValues,'-*');
set(gca,'XTick',x_mm_new,'XTickLabel',xlabels);
grid on
% modified plot
subplot(2,1,2)
plot(x_mm_new,adcValues,'-*');
set(gca, 'XTick', xi_ticks, 'XTickLabel', xi_labels);
grid on
nina
nina le 13 Juin 2025

You’re an angel! Thank you 🎉🎉🎉

Mathieu NOE
Mathieu NOE le 13 Juin 2025
thank you for the nice words !!

Connectez-vous pour commenter.

Plus de réponses (1)

x_mm = [-4.95 -4.5 -4.05 -3.6 -3.15 -2.7 -2.25 -1.8 -1.35 -0.9 -0.45 0 0 0 0 0 0 0.45 0.9 1.35 1.8 2.25 2.7 3.15 3.6 4.05 4.5 4.95 5.4 5.85]';
% round x_mm to get to integers
x_mm_round = round(x_mm);
% get the absolute of the difference between x_mm and x_round. It should be
% below a vertein value (in this case 0.2) for all the values that are
% closest to or are a certain integer
idx = abs(x_mm-x_mm_round) < 0.2;
% remove the ticks that you are not interested in
fig_xticks = 1:numel(x_mm);
fig_xticks = fig_xticks(idx);
% create the corresponding xticklables
fig_xticklabels = num2str(x_mm);
% you could insert x_mm_round here instead to get the integers,
% but I would not recomment this, since it creates untruthfull labeling of the data
% if you would like to use integers here you would need to recalculate the tick positions
fig_xticklabels = fig_xticklabels(idx,:);
figure; axes; xlim([1 numel(x_mm)])
set(gca,'XTick',fig_xticks,'XTickLabel',fig_xticklabels,'XTickLabelRotation',90)

2 commentaires

Alternativly, if you would like to keep the ticks ,you can use:
x_mm = [-4.95 -4.5 -4.05 -3.6 -3.15 -2.7 -2.25 -1.8 -1.35 -0.9 -0.45 0 0 0 0 0 0 0.45 0.9 1.35 1.8 2.25 2.7 3.15 3.6 4.05 4.5 4.95 5.4 5.85]';
x_mm_round = round(x_mm);
idx = abs(x_mm-x_mm_round) < 0.2;
fig_xticks = 1:numel(x_mm);
fig_xticklabels = num2str(x_mm);
fig_xticklabels(~idx,:) = ' ';
figure; axes; xlim([1 numel(x_mm)])
set(gca,'XTick',fig_xticks,'XTickLabel',fig_xticklabels,'XTickLabelRotation',90)
nina
nina le 12 Juin 2025
But how would i do the rounding? Unforunately I already know how to make a plot like you did but am still running into the issue where i want the integers

Connectez-vous pour commenter.

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by