setting xaxis labels to only integers

14 Ansichten (letzte 30 Tage)
nina
nina am 12 Jun. 2025
Kommentiert: Mathieu NOE am 13 Jun. 2025
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?

Akzeptierte Antwort

Mathieu NOE
Mathieu NOE am 12 Jun. 2025
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 Kommentare
nina
nina am 13 Jun. 2025

You’re an angel! Thank you 🎉🎉🎉

Mathieu NOE
Mathieu NOE am 13 Jun. 2025
thank you for the nice words !!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (1)

Katy Weihrich
Katy Weihrich am 12 Jun. 2025
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 Kommentare
Katy Weihrich
Katy Weihrich am 12 Jun. 2025
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 am 12 Jun. 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

Melden Sie sich an, um zu kommentieren.

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by