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
However, if I try to use the subtightplot function it does not work:
% Main plot
x = linspace(0, 10, 100);
plot(x, sin(x), 'LineWidth', 1.5);
Warning: Graphics acceleration hardware is unavailable. Graphics quality and performance might be diminished. See MATLAB System Requirements.
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
Unrecognized function or variable 'subtightplot'.

Error in solution>@(m,n,p)subtightplot(m,n,p,[0.05,0.05],[0.05,0.05],[0.05,0.05]) (line 19)
subplot = @(m,n,p) subtightplot(m, n, p, [0.05 0.05], [0.05 0.05], [0.05 0.05]);

Respuestas (1)

You need to download the subtightplot.m function to your MATLAB path.

2 comentarios

Thanks @Star Strider :-) ...I did it, but, unfortunately, it does not work well yet (I do not see the main plot)...
addpath('/.../subtightplot');
% 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
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.

Iniciar sesión para comentar.

Preguntada:

Sim
el 27 de Ag. de 2026 a las 15:48

Comentada:

hace alrededor de 22 horas

Community Treasure Hunt

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

Start Hunting!

Translated by