MATLAB Answers

How do I put minor ticklabel on and ticklabel off ?

25 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

Show 1 older comment
dpb
dpb on 16 Nov 2016
Probably can't get the desired result with that big of a hammer--even with HG2 have virtually no control over minor ticks other than whether they're on/off. See <Possible-to-label-the-last-minor-tick?> for a previous comment.
How about attaching a figure that shows the problem? Perhaps someone will have some ideas seeing the specifics. I'd note that owing to the above, it's not possible to read the minor tick positions programmatically; if the range is consistent across plots you may be able to figure out where they are heuristically but if the values change drastically that'll be a challenge, too. The minor ticks, unfortunately, are minor indeed in TMW's treatment of them and we're not supposed to care... :(
Sara Antonio
Sara Antonio on 16 Nov 2016
Thank you for replying. This is the kind of plots I´m trying to do. As you can see, the labels on the y ticks goes under the adjacent plot label. Again, I know how to fix it manually by setting the position of the ticks, but I have a lot of similar plots with different y values.
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 :)

  0 Comments

Sign in to comment.


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

  0 Comments

Sign in to comment.


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.

  0 Comments

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!