在 MATLAB 的另一个子图中绘制一个子图
Plotting a subplot within another subplot in MATLAB
我正在尝试在 MATLAB 的另一个子图中绘制一个子图。问题是最终的子图只显示了较小子图的一部分。在网上搜索一番后,似乎这里的一个选择是将子图保存到一个临时图形中,然后将其添加到最终的子图中。另一种方法是手动指定最终图中绘图的位置,即滚动您自己的子图。我想知道是否有更优雅的方法。如果这不是 possible/desirable 行为,请提前致歉。我对代码 运行 的想法是:
someData = linspace(0,10);
subplot(2,2,1);
plotThing(someData);
subplot(2,2,2);
plot(cos(someData));
function [ out ] = plotThing( someData )
y1 = sin(someData);
y2 = sin(2*someData);
y3 = sin(4*someData);
y4 = sin(8*someData);
f = figure(4);
subplot(2,2,1)
plot(someData,y1)
subplot(2,2,2)
plot(someData,y2)
subplot(2,2,3)
plot(someData,y3)
subplot(2,2,4)
plot(someData,y4)
out = f;
end
我想要的行为是在最终图中有两个东西,四个项目的子图在上面,单个图在下面。任何建议都会很棒。
编辑 - 已接受答案的问题是它需要大量的子图(在我的情况下为 12x12)。经过一些思考和工作,我发现可以将一个subplot的内容封装到一个uipanel中。这给出了子图的期望行为,可以将其视为较大比例图中的一个单元。完成此操作的代码类似于:
plotThing(someData, [0, 0.5, 1, 0.5]);
panel2 = uipanel('Position', [0, 0, 1, 0.5]);
subplot(1,1,1,'Parent', panel2); %Strange but necessary as one cannot set the parent of a plot directly
plot(cos(someData));
function [ out ] = plotThing( someData, position )
panel = uipanel('Position', position);
y1 = sin(someData);
y2 = sin(2*someData);
y3 = sin(4*someData);
y4 = sin(8*someData);
subplot(2,2,1, 'Parent', panel)
plot(someData,y1)
subplot(2,2,2, 'Parent', panel)
plot(someData,y2)
subplot(2,2,3, 'Parent', panel)
plot(someData,y3)
subplot(2,2,4, 'Parent', panel)
plot(someData,y4)
end
这个通用解决方案的结果如下:
results
您应该可以通过如下方式告诉 MATLAB 子图的跨度来做到这一点。
subplot(4,2,1); % create a plot with subplots in a grid of 4 x 2
plot(someData,y1); % subplot at first row, first column
subplot(4,2,2);
plot(someData,y2); % subplot at first row, second column
subplot(4,2,3);
plot(someData,y3);
subplot(4,2,4);
plot(someData,y4);
subplot(4,2,[5 6 7 8]); % subplot spanning the entire third and fourth row
plot(someData,y5); % change [5, 6, 7, 8] to change the span
你仍然会使用 subplot 命令,如果你想让它占用超过 1,只需使用矢量作为位置 space:
subplot(2,4,1)
plot(someData,y1)
subplot(2,4,2)
plot(someData,y2)
subplot(2,4,3)
plot(someData,y3)
subplot(2,4,4)
plot(someData,y4)
subplot(2,4,[5:8])
plot(bigPlot,y5)
应该做。
可以避免使用精细网格并在多个字段上跨越更大的图:即使在同一图中也可以使用不同的网格大小调用子图。但是应该先绘制最小网格中的子图,否则我会遇到一些重叠(在 Matlab 2017 中)。您只需确保您的网格适合在一起。
例如:
subplot(4,2,1)
plot(someData)
subplot(4,2,3)
plot(someData)
subplot(2,2,2)
plot(someData)
subplot(2,2,3)
plot(someData)
subplot(2,2,4)
plot(someData)
我正在尝试在 MATLAB 的另一个子图中绘制一个子图。问题是最终的子图只显示了较小子图的一部分。在网上搜索一番后,似乎这里的一个选择是将子图保存到一个临时图形中,然后将其添加到最终的子图中。另一种方法是手动指定最终图中绘图的位置,即滚动您自己的子图。我想知道是否有更优雅的方法。如果这不是 possible/desirable 行为,请提前致歉。我对代码 运行 的想法是:
someData = linspace(0,10);
subplot(2,2,1);
plotThing(someData);
subplot(2,2,2);
plot(cos(someData));
function [ out ] = plotThing( someData )
y1 = sin(someData);
y2 = sin(2*someData);
y3 = sin(4*someData);
y4 = sin(8*someData);
f = figure(4);
subplot(2,2,1)
plot(someData,y1)
subplot(2,2,2)
plot(someData,y2)
subplot(2,2,3)
plot(someData,y3)
subplot(2,2,4)
plot(someData,y4)
out = f;
end
我想要的行为是在最终图中有两个东西,四个项目的子图在上面,单个图在下面。任何建议都会很棒。
编辑 - 已接受答案的问题是它需要大量的子图(在我的情况下为 12x12)。经过一些思考和工作,我发现可以将一个subplot的内容封装到一个uipanel中。这给出了子图的期望行为,可以将其视为较大比例图中的一个单元。完成此操作的代码类似于:
plotThing(someData, [0, 0.5, 1, 0.5]);
panel2 = uipanel('Position', [0, 0, 1, 0.5]);
subplot(1,1,1,'Parent', panel2); %Strange but necessary as one cannot set the parent of a plot directly
plot(cos(someData));
function [ out ] = plotThing( someData, position )
panel = uipanel('Position', position);
y1 = sin(someData);
y2 = sin(2*someData);
y3 = sin(4*someData);
y4 = sin(8*someData);
subplot(2,2,1, 'Parent', panel)
plot(someData,y1)
subplot(2,2,2, 'Parent', panel)
plot(someData,y2)
subplot(2,2,3, 'Parent', panel)
plot(someData,y3)
subplot(2,2,4, 'Parent', panel)
plot(someData,y4)
end
这个通用解决方案的结果如下: results
您应该可以通过如下方式告诉 MATLAB 子图的跨度来做到这一点。
subplot(4,2,1); % create a plot with subplots in a grid of 4 x 2
plot(someData,y1); % subplot at first row, first column
subplot(4,2,2);
plot(someData,y2); % subplot at first row, second column
subplot(4,2,3);
plot(someData,y3);
subplot(4,2,4);
plot(someData,y4);
subplot(4,2,[5 6 7 8]); % subplot spanning the entire third and fourth row
plot(someData,y5); % change [5, 6, 7, 8] to change the span
你仍然会使用 subplot 命令,如果你想让它占用超过 1,只需使用矢量作为位置 space:
subplot(2,4,1)
plot(someData,y1)
subplot(2,4,2)
plot(someData,y2)
subplot(2,4,3)
plot(someData,y3)
subplot(2,4,4)
plot(someData,y4)
subplot(2,4,[5:8])
plot(bigPlot,y5)
应该做。
可以避免使用精细网格并在多个字段上跨越更大的图:即使在同一图中也可以使用不同的网格大小调用子图。但是应该先绘制最小网格中的子图,否则我会遇到一些重叠(在 Matlab 2017 中)。您只需确保您的网格适合在一起。
例如:
subplot(4,2,1)
plot(someData)
subplot(4,2,3)
plot(someData)
subplot(2,2,2)
plot(someData)
subplot(2,2,3)
plot(someData)
subplot(2,2,4)
plot(someData)