Is it possible to remove only specific tick mark(s) while keeping tick labels?

I want to remove some number of specific tick marks, keeping all others, and keeping all the tick labels.
Is this possible?
As a side note, I know that one can set the TickLength property to [0 0], but that affects all the tick marks.

 Accepted Answer

No; removing a tick clears the associated label; there's an inbuilt 1:1 correlation; "no tickee, no lablee".
To do this would require either
  1. drawing the ticks specifically; as you note there isn't sufficient granularity in HG2 to specify individual tick lengths or colors to simulate the effect, or
  2. deleting the desired ticks then text in the labels for those locations
Oh, alternatively, you could probably overlay two axes to achieve the combined effect between the two...
hAx=axes; % new axes
xt=hAx.XTick; % retrieve ticks
hAx.XTick=xt(1:2:end); % clear some
hAx(2)=axes('position',hAx.Position'); % second axes on top of first
hAx(1).XTickLabel=[]; % turn labels of first off
hAx(2).TickLength=[0,0]; % set ticks to zero length 2nd
hAx(2).YTick=[]; % get rid duplicates for y
axes(hAx(1)) % bring first to foreground
results in
Overall, one of the first two options is probably simpler in the end...

5 Comments

Having two overlaid axes can result in some unexpected behaviour for people who are not experts in axes too so that option does require some expertise, especially if, for example you want to zoom in. If you do that with these multiple axes you get some unpleasant behaviour from the ticks. It can be overcome, but the amount of work required to support the usual axes behaviour properly can grow quite quickly.
Yes, for the novice, indeed. linkaxes can help in a lot of those issues altho I'll admit I've never built complex callbacks nor any guis in 30 years w/ Matlab so there's a lot in that area that I'm just totally unfamiliar with. As you say, "there be dragons!"; just OP asked how to get an effect so I showed the ways I could think of... :)
I'd guess there'd be some unexpected cases show up with either of the first two on zoom as well with what it does by default on ticks/labels, albeit only one axes to deal with would definitely be simpler, agreed.
Might as well throw the increased granularity over the fence as enhancement request; it'll never get acted on, but...
I have worked with a system of two axes in the past so it is definitely worth throwing out there as an option. I just remember having to constantly go back to my class that contained the two axes and keep making tweaks for behaviour I had not expected or forgotten would happen. If it is better than alternatives and the behaviour is desperately wanted though it is doable :)
This is what I suspected. I had come across the textBox and multiple axes solutions in my search previously. I just hoped there would exist some less hacky answer.
Fortunately, I am not going to be resizing or moving my figures, so I think working with 2 axes and linkprop() is probably what I want. If this results in too many problems I might just do the forbidden and remove the unwanted ticks in Illustrator...
Thank you all for the thoughtful discussion.
As long as HG2 is structured as is, there is no other "less hacky" procedure; the labels and ticks are intrinsically tied together internally and the tick mark attributes themselves are a global-to-the-axis property rather than by individual tick. All those would have to be turned into arrays with the compounding storage associated with them to be able to do that and the overhead is already heavy.

Sign in to comment.

More Answers (1)

You can, but if you've customized the labels it's a bit trickier. Ideally you want to change the XTick and XTickLabel properties simultaneously, to keep them synchronized.
% Sample data
v = 1:10;
C = num2cell('A':'J');
% Plot 1 with the tick labels automatically generated
h = plot(v, v.^2);
ax = ancestor(h, 'axes');
xt = ax.XTick;
labelsToKeep = [1:6 9:10];
ax.XTick = xt(labelsToKeep);
Because the tick labels were automatically generated, the tick labels remain unchanged as we jump from x = 6 to x = 9.
% Plot 2 with the tick labels manually set
figure
h = plot(v, v.^2);
ax = ancestor(h, 'axes');
ax.XTickLabel = C;
xt = ax.XTick;
xtl = ax.XTickLabel;
labelsToKeep = [1:6 9:10];
ax.XTick = xt(labelsToKeep);
In this case, we don't jump directly from F to I. We "jump" from F to G. If your next line set XTickLabel appropriately, you may see a flicker if the display updates between those two lines.
ax.XTickLabel = xtL(labelsToKeep);
If you want to update them both at once, you can use set.
% Plot 3 with the tick labels manually set and changed simultaneously
figure
h = plot(v, v.^2);
ax = ancestor(h, 'axes');
ax.XTickLabel = C;
xt = ax.XTick;
xtL = ax.XTickLabel;
labelsToKeep = [1:6 9:10];
set(ax, 'XTick', xt(labelsToKeep), 'XTickLabel', xtL(labelsToKeep));
In the case of the tick and tick label properties, changing the properties simultaneously isn't quite as important as if you were adding or deleting points by directly modifying the XData and YData properties. If the *Tick and *TickLabel properties are different lengths, MATLAB will compensate (only using the first part of the *TickLabel property or repeating the *TickLabel labels.) It's much more important to change the *Data properties together to avoid a warning.
% Warning
figure
h = plot(v, v.^2);
x = h.XData;
y = h.YData;
labelsToKeep = [1:6 9:10];
h.XData = x(labelsToKeep);
drawnow
h.YData = y(labelsToKeep);
% no warning
figure
h = plot(v, v.^2);
x = h.XData;
y = h.YData;
labelsToKeep = [1:6 9:10];
set(h, 'XData', x(labelsToKeep), 'YData', y(labelsToKeep));

4 Comments

Doesn't they remove the labels as well as the ticks though? Or am I missing something?
None of those meet OP's request, though, Steven. As Adam notes, they remove the ticks and labels both; once you clear a tick there's not going to be any way to put a TickLabel at that point with HG TickLabel property for that axes; they are paired, "no tickee, no lablee".
Ah, I misunderstood the question. Yes, ticks and labels come as a matched set. You could "hack" it by putting a text object in the correct location, but that's not necessarily going to be easy to manage with respect to zooming, panning, and other axis manipulations.
Ayup...those were the discussion points after the Answer posting the possible routes to produce the initial desired appearance...raises the question of just how much more would even be more than theoretically possible to unbundle in HG; to turn the rest of the tick properties into arrays as well as just the present ones so could have the granularity to do such micro-refinements. There's always reason on occasion for yet further customization for a particular plot for a particular, specific purpose; when does the proliferation become totally unmanageable--it's close already what with the introduction of the lower-level ruler behind the axes.
I just wish some the most basic little nits that have been around since the beginning would get fixed; like the inconsistency in default decimal in the formatting -- as shown in the image I attached; those kinds of uglies are trivial but are still around after 30 years. :(

Sign in to comment.

Products

Asked:

on 10 Apr 2018

Commented:

dpb
on 11 Apr 2018

Community Treasure Hunt

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

Start Hunting!