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)
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
Unrecognized function or variable 'subsubplot'.
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
Dyuman Joshi on 3 Oct 2023
Why are you using a Toolbox for doing something that can be done by subplot or tiledlayout?
Replace subsubplot() command with subplot() and check if that works for you or not.
Austin M. Weber
Austin M. Weber on 3 Oct 2023
@Dyuman Joshi The reason I'm using the Toolbox is because I am working with climate data and the subsubplot function does a better job at visualizing multiple time series with the same x-axis than the subplot function does.
That said, my question also applies to the subplot function. The example I gave doesn't seem to have the ylabel issue, but if I try to plot my actual data with subplot the problem still exists.

Sign in to comment.

Accepted Answer

Anton Kogios
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)

Products


Release

R2022a

Community Treasure Hunt

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

Start Hunting!