How do I put minor ticklabel on and ticklabel off ?

10 次查看(过去 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 个评论
Sara Antonio
Sara Antonio 2016-11-16
编辑:Sara Antonio 2016-11-16
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 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
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 :)

Nick Counts
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

Massimo Zanetti
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.

类别

Help CenterFile Exchange 中查找有关 Axis Labels 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by