setting xaxis labels to only integers

조회 수: 14 (최근 30일)
nina
nina 2025년 6월 12일
댓글: Mathieu NOE 2025년 6월 13일
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?

채택된 답변

Mathieu NOE
Mathieu NOE 2025년 6월 12일
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
nina
nina 2025년 6월 13일

You’re an angel! Thank you 🎉🎉🎉

Mathieu NOE
Mathieu NOE 2025년 6월 13일
thank you for the nice words !!

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Katy Weihrich
Katy Weihrich 2025년 6월 12일
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
Katy Weihrich
Katy Weihrich 2025년 6월 12일
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 2025년 6월 12일
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

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Axis Labels에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by