Why is one of my subplots being deleted?

26 次查看(过去 30 天)
The second subplot is deleted after setting the first, but why? The bottom of the first subplot is at 0.3, whereas the top of the second subplot is at 0.19. There shouldn't be any overlap that would result in deletion.
figure;
ax1 = subplot(2,1,1,'Position', [0.05, 0.3, 0.92, 0.64]);
ax2 = subplot(2,1,2,'Position', [0.05, 0.09, 0.92, 0.1]);
Also, I get the following warning:
Warning: subplot ignores grid location arguments when Position is specified. This syntax will no longer be supported in a future release.

采纳的回答

Les Beckham
Les Beckham 2022-2-25
编辑:Les Beckham 2022-2-25
If you know the position of the two axes already, you don't need to use the subplot function to position them for you.
Just do this:
figure
ax(1) = axes('position', [0.05, 0.3, 0.92, 0.64]);
ax(2) = axes('position', [0.05, 0.09, 0.92, 0.1]);
Note that I created ax as an array of axis handles.
Now you can plot into those axes.
plot(ax(1), 0:0.01:2*pi, sin(0:0.01:2*pi))
plot(ax(2), 0:10, 0:10)

更多回答(2 个)

Voss
Voss 2022-2-25
The problem: only one axes was created
figure;
ax1 = subplot(2,1,1,'Position', [0.05, 0.3, 0.92, 0.64]);
Warning: subplot ignores grid location arguments when Position is specified. This syntax will no longer be supported in a future release.
ax2 = subplot(2,1,2,'Position', [0.05, 0.09, 0.92, 0.1]);
Warning: subplot ignores grid location arguments when Position is specified. This syntax will no longer be supported in a future release.
findall(gcf(),'Type','axes')
ans =
Axes with properties: XLim: [0 1] YLim: [0 1] XScale: 'linear' YScale: 'linear' GridLineStyle: '-' Position: [0.0500 0.0900 0.9200 0.1000] Units: 'normalized' Show all properties
% (only one axes was created)
A solution: if you are specifying the positions directly, you can use axes() directly to create the axes rather than subplot():
figure;
ax1 = axes('Parent',gcf(),'Units','normalized','Position',[0.05, 0.3, 0.92, 0.64]);
ax2 = axes('Parent',gcf(),'Units','normalized','Position', [0.05, 0.09, 0.92, 0.1]);
findall(gcf(),'Type','axes')
ans =
2×1 Axes array: Axes Axes
% (two axes were created)

Matt J
Matt J 2022-2-25
编辑:Matt J 2022-2-25
Not sure, but I think you're better off using tiledlayout(),
tiledlayout(74,1)
ax1 = nexttile([64,1]);
ax2 = nexttile([10,1]);
plot(ax1, 0:0.01:2*pi, sin(0:0.01:2*pi))
plot(ax2, 0:10, 0:10)

产品


版本

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by