how to have labeled and unlabeled ticks on the x axis
51 次查看(过去 30 天)
显示 更早的评论
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?
0 个评论
采纳的回答
Kevin Cahill
2019-7-29
1 个评论
dpb
2019-7-30
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... :)
更多回答(5 个)
dpb
2019-7-28
编辑:dpb
2019-7-28
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.
0 个评论
dpb
2019-7-28
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 个评论
dpb
2019-7-28
编辑:dpb
2019-7-28
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()
dpb
2019-7-28
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!"
0 个评论
Steven Lord
2019-7-28
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 个评论
dpb
2019-7-28
Ah! The one aberration I didn't try, Steven! I didn't think axes had been made "string-smart" yet....
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Axis Labels 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!