setting xaxis labels to only integers

15 次查看(过去 30 天)
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 🎉🎉🎉

请先登录,再进行评论。

更多回答(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