Remove top and bottom ticks AND maintain ylabel and xlabel inside the figure

1 view (last 30 days)
Hi, I am trying to remove the ticks on the top and right axis (which I managed to obtain from a previous posting); however, upon doing so, the xlabel and ylabel seem to lie outside the figure, and don't shift accordingly when I re-scale the figure (rather they get cropped out):
the code used to produce the figure is as follows:
a1 = axes('box','on','xtick',[],'ytick',[]); %Under axis, used as background color and box;
a2 = axes(); %Over axis, used for ticks, labels, and to hold data
propLink = linkprop([a1 a2],'position'); %Keep the positions the same
bar(a2, [1 2 3 4], [1 2 3 4], 0.4, 'FaceColor',[0.5 0.3 1])
ylabel('Rate (mmol_{consumed} g_{cat}^{-1} min^{-1})','FontSize', 14)
xlabel('Wavelength','fontsize',14)
set(gca,'TickDir','out','fontsize',11)
ylim([0 10])
set(gcf,'Position',[0,0,360,350])
set(a2,'box','off','color','none'); %Set top axes props last ("plot" overrides some properties)
Is there some way to avoid this and have the labels adjust accordingly when re-sizing the figure like in a regular matlab plot?
Many thanks,
Mark

Accepted Answer

Benjamin Kraus
Benjamin Kraus on 1 Jun 2022
The issue you are having stems from the call to linkprop to keep the two axes positions synchronized, but in this case it is not clear why you need two axes. You can produce an identical picture using a single axes, and by removing the call to linkprop you will resolve the issue you are having.
bar([1 2 3 4], [1 2 3 4], 0.4, 'FaceColor',[0.5 0.3 1])
ylabel('Rate (mmol_{consumed} g_{cat}^{-1} min^{-1})','FontSize', 14)
xlabel('Wavelength','fontsize',14)
set(gca,'TickDir','out','fontsize',11)
ylim([0 10])
But, if there is a reason you need to use two axes, there is a way to do what you are trying to do while letting the labels adjust accordingly when re-sizing the figure.
The issue is that there are two "positions" on the axes: The outer position (which includes the labels and ticks) and the inner position (which does not include the ticks). By default, the axes layout starts with the outer position (which fills the entire figure), determines how much space is needed to accomodate the labels and ticks, and then automatically adjusts the inner position to make enough room. The outer position is controlled by the OuterPosition property while the inner position is controlled by the Position property (or InnerPosition, which is an alias for Position on regular axes). When you set the Position property you are telling MATLAB "I don't care how much space the labels and ticks take up, I want the center white part of the axes at this position, even if it means clipping the labels."
In this case you are not setting the Position property directly, but you are indirectly setting the Position property via the call to linkprop. In your code the call to linkprop is telling MATLAB to synchronize the (inner) position of both axes, which causes it to ignore the labels and ticks when determining the position.
A better way to do this in R2019b or later is using tiledlayout to keep the two axes aligned. If you put two axes into the same tile of a tiledlayout, it will do the work to keep the two axes aligned, but it is also smart enough to make enough room to accomodate ticks and labels, and also handle resizing the figure the way you want.
t = tiledlayout(1,1);
a1 = nexttile(t);
set(a1,'box','on','xtick',[],'ytick',[]); %Under axis, used as background color and box;
a2 = axes(t); %Over axis, used for ticks, labels, and to hold data
a2.Layout.Tile = 1; % Overlap the two axes
bar(a2, [1 2 3 4], [1 2 3 4], 0.4, 'FaceColor',[0.5 0.3 1])
ylabel('Rate (mmol_{consumed} g_{cat}^{-1} min^{-1})','FontSize', 14)
xlabel('Wavelength','fontsize',14)
set(gca,'TickDir','out','fontsize',11)
ylim([0 10])
set(gcf,'Position',[0,0,360,350])
set(a2,'box','off','color','none');
But again, in this case the second axes is completely unnecessary, and adds a lot of complication.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!

Translated by