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?
조회 수: 33 (최근 30일)
이전 댓글 표시
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:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1501210/image.jpeg)
I can stretch the figure horizontally and the labels will eventually display correctly, but the figure no longer has the desired size:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1501215/image.jpeg)
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
Dyuman Joshi
2023년 10월 3일
Replace subsubplot() command with subplot() and check if that works for you or not.
채택된 답변
Anton Kogios
2023년 10월 3일
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.
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Subplots에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!