how to have labeled and unlabeled ticks on the x axis

74 views (last 30 days)
How can one have an x-axis that has labeled ticks at 0, 500, 1000, ..., 2500, and also has unlabeled ticks at 100, 200, 300, ..., 2400?

Accepted Answer

Kevin Cahill
Kevin Cahill on 29 Jul 2019
ax=gca;
axis([0 2500 0 7000]);
ax.XMinorTick='on';
This is what Matlab suport told me, and it works.
  1 Comment
dpb
dpb on 30 Jul 2019
Well, for a certain definition of "works"...you can only turn the minor ticks on/off, if they happen to be where you want them (or you can live with whatever number of minor ticks it does decide on).
It's also one of the answers I provided... :)

Sign in to comment.

More Answers (5)

dpb
dpb on 28 Jul 2019
Edited: dpb on 28 Jul 2019
Actually, the expedient way may be to have two axes...the one with the desired tick labels and the second overlays it with the ones not labelled. Then you can just clear the tick labels entirely from the second. linkaxes to keep the two in synch numerically.
This will still likely have some issues if you try to get too carried away, but probably simpler route than either of the above (although the text() solution isn't that bad, either).
figure
hAx=axes;
hAx(2)=axes('Position',hAx.Position,'Color','none');
linkaxes(hAx)
xlim([0 2500])
hAx(2).XTick=[0:100:2500];
hAx(2).XTickLabel='';
is starting point...enjoy! :)
NB: You get the modulo 500 ticks by default when set the xlim() value...if change it will probably have to manually set hAx(1) ticks as well.

Kagan Eröz
Kagan Eröz on 28 Jul 2019
try this one
Ticks = 0:500:2500;
set(gca, 'XTickMode', 'manual', 'XTick', Ticks)

dpb
dpb on 28 Jul 2019
Not quite as easy as one might think...at least unless somebody else can come up with more expedient route...
figure % new figure to play in
hAx=axes; % and axes handle
xlim([0 2500]) % set limits
hAx.XTick=[0:100:2500]; % and tick locations
ix=find(mod(hAx.XTick,500)~=0); % who do we want to annihilate
xtkstr=num2str(hAx.XTick.'); % here's the rub...had to get in char() string form
xtkstr(ix,:)=repmat(blanks(4),length(ix),1); % and wipe out the ones not wanted to show
hAx.XTickLabel=xtkstr; % set to the array
Couldn't just set the individual elements to ' ' or empty or even use cell strings, at least in R2017b; had to be the Nx4 char() array.
More granularity for XTick and friends would be nice but ML only built in so much that is modifieable...at least, easily. Like you can't just set a color or visible attribute except for the whole axis, not for elements thereof.
  2 Comments
dpb
dpb on 28 Jul 2019
Edited: dpb on 28 Jul 2019
I simply illustrated how to get the effect...you'll just have to 'spearmint and probably do all the drawing first before fixing up the tick labels.
As noted, the they're (tick labels, that is) just character text and have a 1:1: association with the actual tick marks simply by sequence order, there's no logic to retain any numerical association whatsoever.
And, PLOT() as a high-level function resets a lot of behind-the-scenes stuff including causing the ticks to auto-scale again which screws everything just done all up. You could try LINE() at lower level plotting, but probably the only way this will ever work is to do the plotting first, then try to fixup the axes.
In the end, it might be simpler to just set the tick positions, blank all the tick labels and write them with TEXT()

Sign in to comment.


dpb
dpb on 28 Jul 2019
DOH!
figure
hAx=axes;
xlim([0 2500])
hAx.XMinorTick='on';
Now, this may or may not produce the desired number of minor ticks depending upon Xlim and XTick values chosen as you don't have control over how many minor ticks HG2 thinks there should be--only whether they're displayed or not. For the values in your example, it works as desired; "don't look gift horse in the mouth!"

Steven Lord
Steven Lord on 28 Jul 2019
If you can use a string array, there's a slightly simpler version of one of dpb's answers. Let's make an array of data and plot it.
x = 0:100:2500;
plot(x, x);
Specify you want ticks at each element in x. The automatic labels will likely overlap.
xticks(x);
Construct a string array from x. Replace all but those that are multiples of 500 with a string with no characters. Then set the string array to be the tick labels of the axes.
S = string(x);
S(mod(x, 500)~=0) = "";
xticklabels(S)
  1 Comment
dpb
dpb on 28 Jul 2019
Ah! The one aberration I didn't try, Steven! I didn't think axes had been made "string-smart" yet....

Sign in to comment.

Products


Release

R2019a

Community Treasure Hunt

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

Start Hunting!