How to fully display y-axis labels if they do not fit inside of the figure window when specifying a figure size and using subsubplot?
37 views (last 30 days)
Show older comments
Austin M. Weber
on 3 Oct 2023
Commented: Austin M. Weber
on 4 Oct 2023
I am using the subsubplot function from the Climate Data Toolbox to make a figure that is 3 inches wide by 4 inches tall, but the y-axis labels are only partially displayed within the figure box. Here is an example (I am using R2022a):
% Create data
xdata = (0:18:180)';
ydata1 = randi(40,size(xdata));
ydata2 = randi(90,size(xdata));
ydata3 = randi(35,size(xdata));
ydata = [ydata1,ydata2,ydata3];
% Y-axis label names
ydata_label = {'Sample 1','Sample 2','Sample 3'};
ydata_units = {'(%)','(%)','(%)'};
% Plot figure
figure('units','inches','position',[1,1,3,4])
numRows = size(ydata,2);
for i = 1:numRows
subsubplot(numRows,1,i)
plot(xdata,ydata(:,i))
xlabel('Measurement')
ylabel({ydata_label{i};ydata_units{i}})
if mod(i,2) == 0 % i.e., when 'i' is an even number
set(gca,'YAxisLocation','right')
end
set(gca,'FontName','Calibri Light','FontSize',10,'TickDir','out')
end
The above code will result in something like this:
I can stretch the figure horizontally and the labels will eventually display correctly, but the figure no longer has the desired size:
How can I resize the axes within the figure window so that the y-axis labels are visible while maintaining a 3 inch by 4 inch figure size?.
2 Comments
Dyuman Joshi
on 3 Oct 2023
Replace subsubplot() command with subplot() and check if that works for you or not.
Accepted Answer
Anton Kogios
on 3 Oct 2023
This is happening due to this line, which makes the figure window smaller than the default axis size:
figure('units','inches','position',[1,1,3,4])
One workaround would be to also change the position of the axes by adding this to the end of your for loop:
a = gca;
a.Position(1:2) = a.Position(1:2)+.1;
a.Position(3:4) = [.6,.2];
I haven't fine-tuned the values but you should be able to get it to work.
There are some alternatives you can explore, such as adding this after your subsubplot function instead of the above:
a = gca
a.PositionConstraint = "outerposition";
However, this doesn't align the axes like I think you would like.
More Answers (0)
See Also
Categories
Find more on Annotations 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!