How do I put minor ticklabel on and ticklabel off ?
4 次查看(过去 30 天)
显示 更早的评论
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 个评论
dpb
2016-11-16
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. :(
回答(3 个)
Nick Counts
2016-11-16
编辑:Nick Counts
2016-11-16
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 个评论
Nick Counts
2016-11-16
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 个评论
Massimo Zanetti
2016-11-17
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 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Axis Labels 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!