How can I modify my subplot positions to stop them overlapping each other?

68 visualizaciones (últimos 30 días)
I am trying to create a subplot (2 plots) of maps next to each other , and my issue is with my placement of subplots: They are overwriting each other.
How can I modify the plot positions so they won't overwrite each other?
The code I'm using is:
%% Plot #1
figure; subplot(1,2,1)
% Main plot
contourf(XMesh1,YMesh1,ZMesh1,10000,'EdgeColor','none')
xlabel('X (Degree)'); ylabel('Y (Degree)')
ax1.Position=[0.1,0.1,0.6,0.6];
ax1.TickDir='out';
% X Axis histogram
ax2=axes('Parent',gcf);hold(ax2,'on')
[f,xi]=ksdensity(X_GS);
fill([xi,xi(1)],[f,0],[0.34 0.47 0.71],'FaceAlpha',...
0.3,'EdgeColor',[0.34 0.47 0.71],'LineWidth',1.2)
ax2.Position=[0.1,0.75,0.6,0.15];
ax2.YColor='none';
ax2.XTickLabel='';
ax2.TickDir='out';
ax2.XLim=ax1.XLim;
% Y Axis histogram
ax3=axes('Parent',gcf);hold(ax3,'on')
[f,yi]=ksdensity(Y_GS);
fill([f,0],[yi,yi(1)],[0.34 0.47 0.71],'FaceAlpha',...
0.3,'EdgeColor',[0.34 0.47 0.71],'LineWidth',1.2)
ax3.Position=[0.75,0.1,0.15,0.6];
ax3.XColor='none';
ax3.YTickLabel='';
ax3.TickDir='out';
ax3.YLim=ax1.YLim;
%% Plot #2
subplot(1,2,2)
% Main plot
contourf(XMesh2,YMesh2,ZMesh2,10000,'EdgeColor','none')
xlabel('X'); ylabel('Y')
ax1.Position=[0.1,0.1,0.6,0.6];
ax1.TickDir='out';
% X Axis histogram
ax2=axes('Parent',gcf);hold(ax2,'on')
[f,xi]=ksdensity(X_G);
fill([xi,xi(1)],[f,0],[0.34 0.47 0.71],'FaceAlpha',...
0.3,'EdgeColor',[0.34 0.47 0.71],'LineWidth',1.2)
ax2.Position=[0.1,0.75,0.6,0.15];
ax2.YColor='none';
ax2.XTickLabel='';
ax2.TickDir='out';
ax2.XLim=ax1.XLim;
% Y Axis histogram
ax3=axes('Parent',gcf);hold(ax3,'on')
[f,yi]=ksdensity(Y_G);
fill([f,0],[yi,yi(1)],[0.34 0.47 0.71],'FaceAlpha',...
0.3,'EdgeColor',[0.34 0.47 0.71],'LineWidth',1.2)
ax3.Position=[0.75,0.1,0.15,0.6];
ax3.XColor='none';
ax3.YTickLabel='';
ax3.TickDir='out';
ax3.YLim=ax1.YLim;

Respuesta aceptada

Voss
Voss el 10 de Jul. de 2022
The code you have is explicitly setting the positions of the axes to be the same (i.e., each axes in one set of three axes has the same position as an axes in the other set of three axes). To fix that, figure out what the positions need to be instead. Something like this maybe:
% dummy data
XMesh1 = [0 1];
YMesh1 = [0 1];
ZMesh1 = [1 2; 3 4];
f = [1 2];
xi = [0 1];
yi = [0 1];
XMesh2 = [0 1];
YMesh2 = [0 1];
ZMesh2 = [1 2; 3 4];
%% Plot #1
figure;
ax1 = subplot(1,2,1); % NB: this is ax1, I assume
% Main plot
contourf(XMesh1,YMesh1,ZMesh1,2,'EdgeColor','none') % NB: using 2 contours, not 10000 here
xlabel('X (Degree)'); ylabel('Y (Degree)')
ax1.Position=[0.1,0.1,0.25,0.6];
ax1.TickDir='out';
% X Axis histogram
ax2=axes('Parent',gcf);hold(ax2,'on')
% [f,xi]=ksdensity(X_GS);
fill([xi,xi(1)],[f,0],[0.34 0.47 0.71],'FaceAlpha',...
0.3,'EdgeColor',[0.34 0.47 0.71],'LineWidth',1.2)
ax2.Position=[0.1,0.75,0.25,0.15];
ax2.YColor='none';
ax2.XTickLabel='';
ax2.TickDir='out';
ax2.XLim=ax1.XLim;
% Y Axis histogram
ax3=axes('Parent',gcf);hold(ax3,'on')
% [f,yi]=ksdensity(Y_GS);
fill([f,0],[yi,yi(1)],[0.34 0.47 0.71],'FaceAlpha',...
0.3,'EdgeColor',[0.34 0.47 0.71],'LineWidth',1.2)
ax3.Position=[0.375,0.1,0.075,0.6];
ax3.XColor='none';
ax3.YTickLabel='';
ax3.TickDir='out';
ax3.YLim=ax1.YLim;
%% Plot #2
ax1 = subplot(1,2,2); % NB: this is ax1, I assume
% Main plot
contourf(XMesh2,YMesh2,ZMesh2,2,'EdgeColor','none') % NB: using 2 contours, not 10000 here
xlabel('X'); ylabel('Y')
ax1.Position=[0.6,0.1,0.25,0.6];
ax1.TickDir='out';
% X Axis histogram
ax2=axes('Parent',gcf);hold(ax2,'on')
% [f,xi]=ksdensity(X_G);
fill([xi,xi(1)],[f,0],[0.34 0.47 0.71],'FaceAlpha',...
0.3,'EdgeColor',[0.34 0.47 0.71],'LineWidth',1.2)
ax2.Position=[0.6,0.75,0.25,0.15];
ax2.YColor='none';
ax2.XTickLabel='';
ax2.TickDir='out';
ax2.XLim=ax1.XLim;
% Y Axis histogram
ax3=axes('Parent',gcf);hold(ax3,'on')
% [f,yi]=ksdensity(Y_G);
fill([f,0],[yi,yi(1)],[0.34 0.47 0.71],'FaceAlpha',...
0.3,'EdgeColor',[0.34 0.47 0.71],'LineWidth',1.2)
ax3.Position=[0.875,0.1,0.075,0.6];
ax3.XColor='none';
ax3.YTickLabel='';
ax3.TickDir='out';
ax3.YLim=ax1.YLim;

Más respuestas (1)

Image Analyst
Image Analyst el 9 de Jul. de 2022
It's best if you're going to use a gridded layout just use subplot or nexttile for all of them. Don't try to create axes on your own with axes(), like you did.
subplot(2,2,1);
% Plot first data
subplot(2,2,2);
% Plot second data
subplot(2,2,3);
% Plot third data
subplot(2,2,4);
% Plot fourth data

Categorías

Más información sobre Data Distribution Plots en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by