How to get position coordinates of axes' ticks?

24 Ansichten (letzte 30 Tage)
Dominik Mattioli
Dominik Mattioli am 19 Jul. 2021
Kommentiert: dpb am 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 Kommentare
dpb
dpb am 20 Jul. 2021
Bearbeitet: dpb am 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 am 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;
...

Melden Sie sich an, um zu kommentieren.

Antworten (0)

Kategorien

Mehr zu Graphics Object Properties finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by