Is it possible to remove only specific tick mark(s) while keeping tick labels?
Show older comments
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
More Answers (1)
Steven Lord
on 10 Apr 2018
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
Adam
on 10 Apr 2018
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".
Steven Lord
on 10 Apr 2018
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.
dpb
on 10 Apr 2018
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. :(
Categories
Find more on Axis Labels in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!