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]);

 采纳的回答

Thanks a lot @Star Strider! :-)
I tried to have another go, and I might have found a potential solution, even if not elegant.... The idea is the following: It appears that subtightplot deletes any overlapping axes in the figure it runs in, which would wipe out the main plot created above. So I built the "subplots grid" in a separate, hidden figure instead, then moved each axis over to the main figure afterward and rescaled it to fit inside inset_box... I hope this is not a too weak solution...
addpath('/.../subtightplot');
% Main plot
fig = figure;
x = linspace(0, 10, 100);
plot(x, sin(x), 'LineWidth', 1.5);
xlabel('X'); ylabel('Y');
% Inset made of subplots
fig_temp = figure('Visible', 'off', 'Position', [100 100 300 300]);
inset_box = [0.6 0.6 0.3 0.3];
subplot_fan = @(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_fan(2, 2, i); % create axis inside "fig_temp"
pos_full = ax.Position; % its position normalized to "fig_temp"
ax.Parent = fig; % re-parent onto the main figure
ax.Position = [inset_box(1) + pos_full(1)*inset_box(3), ...
inset_box(2) + pos_full(2)*inset_box(4), ...
pos_full(3)*inset_box(3), ...
pos_full(4)*inset_box(4)]; % re-map into "inset_box" coordinates
plot(ax, x, sin(i*x), 'r');
end
close(fig_temp); % temp figure no longer needed
figure(fig); % bring focus back to the main figure

更多回答(0 个)

类别

在 帮助中心 和 File Exchange 中查找有关 Creating, Deleting, and Querying Graphics Objects 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by