Clear Filters
Clear Filters

Why is one of my subplots being deleted?

26 views (last 30 days)
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.

Accepted Answer

Les Beckham
Les Beckham on 25 Feb 2022
Edited: Les Beckham on 25 Feb 2022
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)

More Answers (2)

Voss
Voss on 25 Feb 2022
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 on 25 Feb 2022
Edited: Matt J on 25 Feb 2022
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)

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!