フィルターのクリア

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?

30 ビュー (過去 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
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 件のコメント
Dyuman Joshi
Dyuman Joshi 2023 年 10 月 3 日
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 2023 年 10 月 3 日
@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.

サインインしてコメントする。

採用された回答

Anton Kogios
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.
  1 件のコメント
Austin M. Weber
Austin M. Weber 2023 年 10 月 4 日
@Anton Kogios, The first method seems to work, it just takes some fine-tuning as you say. Thanks for the help!

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeSubplots についてさらに検索

製品


リリース

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by