How to get position coordinates of axes' ticks?

30 views (last 30 days)
Dominik Mattioli
Dominik Mattioli on 19 Jul 2021
Commented: dpb on 20 Jul 2021
I would like to know the intersection points of the X and Y major grid lines for an axis, with respect to the parent figure. I have some code that does this, but I'm wondering if there is a getter call for the axis object, given that the 'grid on' and 'grid minor' calls plot lines that intersect with the desired points:
% Init fig and axis.
f = figure( 'Color', 'w', 'WindowState', 'Maximized', 'Units', 'Points' );
ax = axes( 'Parent', f, 'Layer', 'Bottom', 'Units', 'Points',...
'XTick', 0:.1:1, 'XTickLabels', strsplit( num2str( 0:.1:1 ) ),...
'YTick', 0:.1:1, 'YTickLabels', strsplit( num2str( 0:.1:1 ) ) );
axP = ax.get( 'Position' );
% Get coordinates of each grid line.
xRuler = ax.get( 'XAxis' );
xRulerTickIndices = xRuler.get( 'TickValues' );
xRulerTickPos = linspace( axP( 1 ), axP( 1 ) + axP( 3 ), numel( xRulerTickIndices ) );
yRuler = ax.get( 'YAxis' );
yRulerTickIndices = yRuler.get( 'TickValues' );
yRulerTickPos = linspace( axP( 2 ), axP( 2 ) + axP( 4 ), numel( yRulerTickIndices ) );
[XGrid, YGrid] = meshgrid( xRulerTickPos, yRulerTickPos );
widths = diff( xRulerTickPos( 1:2 ) ) / 4;
heights = diff( yRulerTickPos( 1:2 ) ) / 4;
% Plot axes at each point.
newAx = gobjects( size( XGrid ) );
for idx = 1:size( XGrid, 1 )
for jdx = 1:size( XGrid, 2 )
newAxPos = horzcat( XGrid( idx, jdx ) - widths/2, YGrid( idx, jdx ) - heights/2, widths, heights );
newAx( idx, jdx ) = axes( 'Parent', f, 'Layer', 'Top', 'Units', 'Points', 'Position', newAxPos );
end
end
Edit: this code assumes tick marks that are equidistant from each other and from the box limits, so this code is not generalizable.
Edit2: The axes that I would plot at these points would vary in size while still aligning with the ticks of the main axis. I don't think subplot() or tiledlayout() will be useful for my purpose.
  3 Comments
dpb
dpb on 20 Jul 2021
Edited: dpb on 20 Jul 2021
" if there is a getter call for the axis object, given that the 'grid on' and 'grid minor' calls plot lines that intersect with the desired points:"
The grid lines are in axes units, not in physical coordinates, so I'm virtually positive there is no object handle that returns those. But, you can use Yair Altman's FEX submission to explore what is inside the axes object but hidden to your heart's content.
dpb
dpb on 20 Jul 2021
I'd note the above code could be written much more compactly --
% Init fig and axis.
hF= figure( 'Color', 'w', 'WindowState', 'Maximized', 'Units', 'Points' );
hAx=axes(hF, 'Layer','Bottom', 'Units','Points'); % default ticks, tick labels match 0:0.1:1
axP = hAx.Position; % dot notation more concise
% Get coordinates of each grid line.
xRuler = hAx.XAxis;
xRulerTickIndices=xticks;
...

Sign in to comment.

Answers (0)

Categories

Find more on Graphics Object Properties in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!