How do I put minor ticklabel on and ticklabel off ?

10 views (last 30 days)
Sara Antonio
Sara Antonio on 16 Nov 2016
Answered: Massimo Zanetti on 17 Nov 2016
I have some subplots glued together and the tick labels appear on the extremes of the axis overlaying the label of the adjacent subplot. I turn on the minor tick and I would like to give the tick labels to the minor ones and turn off the labels of the major ticks. Can I automatically do this with the 'ticklabel' 'on/off'?
I know I can set the labels manually, but I have a bunch of plots where my variable changes and it takes a lot of effort and time to know the size of the axis and where to put the ticks.
  4 Comments
dpb
dpb on 16 Nov 2016
Probably computing a percentage difference and computing the new desired ticks at a relative position based on the retrieved ylim values will be what will need to do here if you can't live with leaving some white space between the subplots.
One thing you could do that would also help would be to retrieve ylabel position of the plot with the largest-magnitude y-axis values and adjust the rest to align them vertically so they aren't scattered around as the default leaves them.
Ain't HG a pain!? :) It's such a hassle to be able to get at stuff that TMW hasn't thought of a priori. :(

Sign in to comment.

Answers (3)

Nick Counts
Nick Counts on 16 Nov 2016
Edited: Nick Counts on 16 Nov 2016
Ok, I have a possible workaround. It doesn't truly answer your question, but it might still achieve your aim?
You could shift the YTickLabels over so they don't overlap. I have two methods for doing this: one that only uses documented properties, but is a little messy, and one that uses an undocumented property:
Documented
This approach is to simply pad the YTickLabel strings with whitespace. You can get fancy trying to calculate how much whitespace, or just do a uniform padding:
% Make an example figure:
fig = figure;
fig.Units = 'centimeters';
fig.Position(3:4) = [13 20];
% Populate sample axes that are 'touching'
plotPositions = [ 3, 13, 7, 6;
3, 7, 7, 6;
3, 1, 7, 6];
for i = 1:size(plotPositions,1)
hAx(i) = subplot(3, 1, i);
hAx(i).Units = 'centimeters';
scatter( hAx(i), 1:10, randi(10, 10, 1), 'bo')
hAx(i).Position = plotPositions(i,:);
hAx(i).XGrid = 'on';
hAx(i).YGrid = 'on';
end
% Pad every other axis YTickLabel
for i = 1:2:size(plotPositions, 1)
yAxisLabels = hAx(i).YRuler.TickLabel;
paddingArray = repmat({' '},length(yAxisLabels),1);
yAxisLabelsPadded = strcat(yAxisLabels, paddingArray);
hAx(i).YRuler.TickLabel = yAxisLabelsPadded;
end
That example produces this result:
Undocumented Approach
In the HG2 system, there are a bunch of hidden properties for many graphics objects. Here I am using the TickLabelGapOffset property of the axes' YRuler objects.
% Make an example figure:
fig = figure;
fig.Units = 'centimeters';
fig.Position(3:4) = [13 20];
% Populate sample axes that are 'touching'
plotPositions = [ 3, 13, 7, 6;
3, 7, 7, 6;
3, 1, 7, 6];
for i = 1:size(plotPositions,1)
hAx(i) = subplot(3, 1, i);
hAx(i).Units = 'centimeters';
scatter( hAx(i), 1:10, randi(10, 10, 1), 'bo')
hAx(i).Position = plotPositions(i,:);
hAx(i).XGrid = 'on';
hAx(i).YGrid = 'on';
end
% Change the Tick Label Gap for every other axis
for i = 1:2:size(plotPositions, 1)
oldGapOffset = hAx(i).YRuler.TickLabelGapOffset;
hAx(i).YRuler.TickLabelGapOffset = oldGapOffset + 20;
end
This approach looks very much the same, and produces this result:
Hopefully this helps! Good luck with your complex plotting :)

Nick Counts
Nick Counts on 16 Nov 2016
I'm a dummy - I completely forgot about HideEndTicksIfOutside !
Setting this to 'on' will hide both the Major tick mark and associated label :)
% Make an example figure:
fig = figure;
fig.Units = 'centimeters';
fig.Position(3:4) = [13 20];
% Populate sample axes that are 'touching'
plotPositions = [ 3, 13, 7, 6;
3, 7, 7, 6;
3, 1, 7, 6];
for i = 1:size(plotPositions,1)
hAx(i) = subplot(3, 1, i);
hAx(i).Units = 'centimeters';
scatter( hAx(i), 1:10, randi(10, 10, 1), 'bo')
hAx(i).Position = plotPositions(i,:);
hAx(i).XGrid = 'on';
hAx(i).YGrid = 'on';
end
% Hide outer tick marks on alternating plots
for i = 1:2:size(plotPositions, 1)
hAx(i).YRuler.HideEndTicksIfOutside = 'on';
end

Massimo Zanetti
Massimo Zanetti on 17 Nov 2016
One solution can be to set y-limits to larger values, and then force the labels to be the original ones. This way, the labels will result inside the limits, and not on the edge positions.

Community Treasure Hunt

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

Start Hunting!