I am creating a multi-panel figure in MATLAB (with multiple axes of different size). I would like all tick marks to have the same absolute size across all subplots.
According to the MATLAB user guide, tick length is normalized with respect to the longest axis:
TickLength. Tick mark length, specified as a two-element vector of the form [2Dlength 3Dlength]. [...] Specify the values in units normalized relative to the longest of the visible x-axis, y-axis, or z-axis lines.
In order to make all ticks of the same length, I am running the following code:
fixlen = 0.005; % Desired target length
for i = 1:numel(h) % Loop over axes handles
rect = get(h(i),'Position'); % Get the axis position
width = rect(3); % Axis width
height = rect(4); % Axis height
axislen = max([height,width]); % Get longest axis
ticklen = fixlen/axislen; % Fix length
set(h(i),'TickDir','out','TickLength',ticklen*[1 1]);
end
Unfortunately, the above code does not produce a figure in which all tick lengths are equal. Perhaps I am missing something?
Solution. There were two problems in my code.
First of all, I needed to switch from
Normalized
units to some fixed units (such as pixels). See the answer below.In some part of the code, before the above snippet, I was resizing the figure and I had a
drawnow
to update it. However, MATLAB would reach the code snippet before the graphics commands had been executed, and therefore the reported sizes were not correct. I solved the issue by placing apause(0.1)
command after thedrawnow
.