Within a plot, how can I create an inset containing multiple subplots using subtightplot?
显示 更早的评论
If I use the built-in suplot function, it works:
% Main plot
x = linspace(0, 10, 100);
plot(x, sin(x), 'LineWidth', 1.5);
xlabel('X'); ylabel('Y');
% Inset made of subplots
hp = uipanel('Parent', gcf, 'Position', [0.6 0.6 0.3 0.3]);
for i = 1:4
ax = subplot(2, 2, i, 'Parent', hp);
plot(ax, x, sin(i*x), 'r');
end
% Main plot
x = linspace(0, 10, 100);
plot(x, sin(x), 'LineWidth', 1.5);
xlabel('X'); ylabel('Y');
% Inset made of subplots
hp = uipanel('Parent', gcf, 'Position', [0.6 0.6 0.3 0.3]);
subplot = @(m,n,p) subtightplot(m, n, p, [0.05 0.05], [0.05 0.05], [0.05 0.05]);
for i = 1:4
ax = subplot(2, 2, i);
ax.Parent = hp;
plot(ax, x, sin(i*x), 'r');
end
回答(1 个)
Star Strider
2026-8-27,16:02
1 个投票
You need to download the subtightplot.m function to your MATLAB path.
2 个评论
Star Strider
2026-8-27,20:08
You probably need to check to be certain that your code is essentially the same as that in subtightplot_demo.m .
Also, rather that starting off with uipanel, see if you can get it to work natively, for example similar to the demo code.
Another consideration is that the subtightplot code was last modified in 2013. The convention known as 'Handle Graphics 2' (HG2) was adopted in 2016, and there have been other modifications since. It may simply not work correctly with all the changes to MATLAB graphics in the intervening 13 years.
It might be best for you to use either subplot (that works, as you noted), or tiledlayout instead, that could give you the tight grouping you appear to want.
类别
在 帮助中心 和 File Exchange 中查找有关 Subplots 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!


