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

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

采纳的回答

dpb
dpb 2018-4-10
编辑:dpb 2018-4-10
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 个评论
noahop
noahop 2018-4-11
编辑:noahop 2018-4-11
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.
dpb
dpb 2018-4-11
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.

请先登录,再进行评论。

更多回答(1 个)

Steven Lord
Steven Lord 2018-4-10
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 个评论
Steven Lord
Steven Lord 2018-4-10
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
dpb 2018-4-10
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. :(

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Grid Lines, Tick Values, and Labels 的更多信息

产品

Community Treasure Hunt

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

Start Hunting!

Translated by