Have multiple axes nested within a subplot

25 ビュー (過去 30 日間)
Noah Leonardo
Noah Leonardo 2021 年 7 月 8 日
回答済み: Avni Agrawal 2024 年 2 月 14 日
I am trying to have a loop which adds 16 subplots (4,4) to a figure. Alongside each figure I want two seperate axes to be nested within the plots each plot for a total of 16 subplots and 32 sub-subplots.
I basically want each subplot to look like this:
cpDisplay = figure;
x = linspace(0,30);
x1 = sin(x/2);
x2 = cos(x/3);
x3 = sin(2*x/5);
tmpsub = axes;
plot(tmpsub,x1);
ax2 = axes('Position',[tmpsub.Position(1)*5.5,tmpsub.Position(2)*6.5,tmpsub.Position(3)/5,tmpsub.Position(4)/4]);
plot(ax2,x2)
ax3 = axes('Position',[tmpsub.Position(1)*1.5,tmpsub.Position(2)*6.5,tmpsub.Position(3)/4,tmpsub.Position(4)/4]);
plot(ax3,x3)
If I try and impliment a tiledlayout with this code it does not run an error; however, the sub-subplots aren't visible and after the second iteration the whole figure resembles the top picture:
cpDisplay = figure;
x = linspace(0,30);
x1 = sin(x/2);
x2 = cos(x/3);
x3 = sin(2*x/5);
tiledlayout(4,4);
for qqq=1:16
tmpsub = nexttile;
plot(tmpsub,x1);
ax2 = axes('Position',[tmpsub.Position(1)*5.5,tmpsub.Position(2)*6.5,tmpsub.Position(3)/5,tmpsub.Position(4)/4]);
plot(ax2,x2)
ax3 = axes('Position',[tmpsub.Position(1)*1.5,tmpsub.Position(2)*6.5,tmpsub.Position(3)/4,tmpsub.Position(4)/4]);
plot(ax3,x3)
  2 件のコメント
David
David 2022 年 1 月 21 日
編集済み: David 2022 年 1 月 21 日
An important concept is that the axes don't nest -- they are all first level children of the containing figure, and need their positions defined in terms of the figure, not other/parent axes.
Maybe initialize a row of tile handles, and then cycle through them:
cpDisplay = figure(1);
clf
x = linspace(0,30);
x1 = sin(x/2);
x2 = cos(x/3);
x3 = sin(2*x/5);
tiledlayout(4,4);
for qqq=1:16;
th(qqq) = nexttile;
end;
for qqq=1:16;
tmpsub = th(qqq);
ax(qqq)=plot(tmpsub,x1);
end;
for qqq=1:16;
tmpsub = th(qqq);
ax2 = axes('Position',[tmpsub.Position(1)*5.5,tmpsub.Position(2)*6.5,tmpsub.Position(3)/5,tmpsub.Position(4)/4]);
plot(ax2,x2)
ax3 = axes('Position',[tmpsub.Position(1)*1.5,tmpsub.Position(2)*6.5,tmpsub.Position(3)/4,tmpsub.Position(4)/4]);
plot(ax3,x3)
end;
I didn't do the sub-position offset math, so I think the sub-sub-plots all end up stacking on top of each other:
Here's some matrix sub-plot positioning math:
for qqq=1:16;
tmpsub = th(qqq);
% left bottom width height
p=tmpsub.Position;
%ax2 = axes('Position',[tmpsub.Position(1),tmpsub.Position(2),tmpsub.Position(3)/5,tmpsub.Position(4)/4]);
ax2 = axes('Position',p.*[1 1 .2 .25]+[p(3:4).*[.2 .25] 0 0]);
plot(ax2,x2)
ax3 = axes('Position',p.*[1 1 .2 .25]+[p(3:4).*[1-.2 1-.25] 0 0]);
%ax3 = axes('Position',[tmpsub.Position(1)*1.5,tmpsub.Position(2)*6.5,tmpsub.Position(3)/4,tmpsub.Position(4)/4]);
plot(ax3,x3)
end;
and:
Danylyna Shpakivska
Danylyna Shpakivska 2023 年 1 月 24 日
thank you!

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

回答 (1 件)

Avni Agrawal
Avni Agrawal 2024 年 2 月 14 日
Hi Noah,
I understand that you want to plot multiple axes nested within the subplot. The `tiledlayout` function creates a grid of plots with consistent spacing, but when you add nested axes using the `axes` function, you must specify the position manually.
Here is an example code that creates a figure with 16 subplots, and within each subplot, it adds two nested sub-subplots. The positions of the nested axes are calculated relative to the parent subplot. Note that the factors used to adjust the position of the nested axes may need to be fine-tuned based on your figure's size and desired layout.
cpDisplay = figure;
% Create a 4x4 tiled layout
t = tiledlayout(4, 4);
% Data for plotting
x = linspace(0, 30);
x1 = sin(x/2);
x2 = cos(x/3);
x3 = sin(2*x/5);
% Loop through each tile and create subplots
for qqq = 1:16
% Create main subplot
tmpsub = nexttile(t);
plot(tmpsub, x, x1); % Plot x1 in the main subplot
% Calculate positions for the nested subplots relative to the main subplot
% Adjust these values as needed to avoid overlap and to fit within the main subplot
mainPos = get(tmpsub, 'Position');
ax2Pos = [mainPos(1) + mainPos(3) * 0.5, mainPos(2) + mainPos(4) * 0.5, mainPos(3) * 0.4, mainPos(4) * 0.4];
ax3Pos = [mainPos(1) + mainPos(3) * 0.5, mainPos(2), mainPos(3) * 0.4, mainPos(4) * 0.4];
% Create nested subplots and plot data
ax2 = axes(cpDisplay, 'Position', ax2Pos);
plot(ax2, x, x2); % Plot x2 in the first nested subplot
ax3 = axes(cpDisplay, 'Position', ax3Pos);
plot(ax3, x, x3); % Plot x3 in the second nested subplot
% Format the nested subplots if needed (e.g., remove ticks and labels)
set(ax2, 'XTick', [], 'YTick', [], 'Box', 'on');
set(ax3, 'XTick', [], 'YTick', [], 'Box', 'on');
end
% Adjust the layout to prevent overlap of nested axes with main axes
t.TileSpacing = 'compact';
t.Padding = 'compact';
I hope this helps!

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by