How can I modify my subplot positions to stop them overlapping each other?
68 次查看(过去 30 天)
显示 更早的评论
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;
0 个评论
采纳的回答
Voss
2022-7-10
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;
0 个评论
更多回答(1 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Data Distribution Plots 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!