필터 지우기
필터 지우기

Subplot panels which have several axes linked together using "tiledlayout"

조회 수: 2 (최근 30일)
A LL
A LL 2022년 8월 12일
답변: Shivansh 2022년 10월 8일
I want to suplot panels a and b.
Each panel has 3 different axes that I link together (1-colormap for continents, 2-colormap for data, 3-black contour). If I plot each panel as a single figure it works just fine (see attached figure for panel a and b).
However, when I try to do a subplot it does not work and it shows only the last panel.
I guess the problem comes from the different axes each panel as because I never had trouble with subplotting before.
My panel a:
My panel b:
When I try to subplot my panels it does not work and only shows the last panel...
Here is the code I use to produce my figure and I have attached the data in the file "data_plot.mat":
%Panel a
figure(1)
%attempt to subplot:
%t = tiledlayout(1,2,'TileSpacing','none');
%t1 = nexttile;
%hold on;
%ax 1: continents
ax1 = axes;
pcolor(Land);
axis ij;
axis equal;
box on;
axis off;
shading flat;
%ax 2: SIC
ax2 = axes;
hold on;
pcolor(xx,yy,sicEASE_w29_2007);
mask = contour(xx,yy,Mask,1,'LineWidth',1,'Color','black');
axis ij;
axis equal;
box on;
axis off;
shading flat;
%%Link them together
linkaxes([ax1,ax2])
%%Hide the top axes
ax2.Visible = 'off';
ax2.XTick = [];
ax2.YTick = [];
%%Give each one its own colormap
colormap(ax1,cmap)
colormap(ax2,'parula')
xlim([110 270]);
ylim([70 260]);
text(225,79,'W29','Fontsize',18);
text(115,79,'(a)','Fontsize',18);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%5
%Panel b
figure(2) %comment to subplot
%attempt to subplot:
%t2 = nexttile;
%hold on;
%ax 3: continents
ax3 = axes;
pcolor(Land);
axis ij;
axis equal;
box on;
axis off;
shading flat;
%ax 4: SIC
ax4 = axes;
hold on;
pcolor(xx,yy,sicEASE_w32_2007);
mask = contour(xx,yy,Mask,1,'LineWidth',1,'Color','black');
axis ij;
axis equal;
box on;
axis off;
shading flat;
%%Link them together
linkaxes([ax3,ax4])
%%Hide the top axes
ax4.Visible = 'off';
ax4.XTick = [];
ax4.YTick = [];
%%Give each one its own colormap
colormap(ax3,cmap)
colormap(ax4,'parula')
xlim([110 270]);
ylim([70 260]);
text(225,79,'W32','Fontsize',18);
text(115,79,'(b)','Fontsize',18);
How do I fix this and subplot my panels with their respective axes?
Thank you
  댓글 수: 2
Cris LaPierre
Cris LaPierre 2022년 8월 12일
Can you also share your data? Save your variables to a mat file and attach them to your post using the paperclip icon.
A LL
A LL 2022년 8월 12일
I have attached my data to the question above :)

댓글을 달려면 로그인하십시오.

답변 (1개)

Shivansh
Shivansh 2022년 10월 8일
The code given below can help you to get the desired result. (I have added comment for the lines which I have added).
ax1 = axes;
%added line to set the position of axes1
ax1.Position=[0.1300,0.1100,0.3875,0.8150];
p=pcolor(Land);
axis ij;
axis equal;
box on;
axis off;
shading interp
%ax 2: SIC
ax2 = axes;
%added line to set the position of axes2
ax2.Position=[0.1300,0.1100,0.3875,0.8150];
hold on;
j=pcolor(xx,yy,sicEASE_w29_2007);
j.LineWidth = 6;
axis ij;
axis equal;
box on;
axis off;
shading flat;
%%Link them together
linkaxes([ax1,ax2])
%%Hide the top axes
ax2.Visible = 'off';
ax2.XTick = [];
ax2.YTick = [];
%%Give each one its own colormap
colormap(ax1,cmap)
colormap(ax2,'parula')
xlim([110 270]);
ylim([70 260]);
text(225,79,'W29','Fontsize',18);
text(115,79,'(a)','Fontsize',18);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%5
%Panel b
%attempt to subplot:
%hold on;
%ax 3: continents
ax3 = axes;
%added line to set the position of axes3
ax3.Position=[0.6,0.1100,0.3875,0.8150];
p=pcolor(Land);
p.FaceColor="texturemap";
axis ij;
axis equal;
box on;
axis off;
shading flat;
%ax 4: SIC
ax4 = axes;
%added line to set the position of axes4
ax4.Position=[0.6,0.1100,0.3875,0.8150];
hold on;
q=pcolor(xx,yy,sicEASE_w32_2007);
mask = contour(xx,yy,Mask,1,'LineWidth',1,'Color','black');
axis ij;
axis equal;
box on;
axis off;
shading flat;
%%Link them together
linkaxes([ax3,ax4])
%%Hide the top axes
ax4.Visible = 'off';
ax4.XTick = [];
ax4.YTick = [];
%%Give each one its own colormap
colormap(ax3,cmap)
colormap(ax4,'parula')
xlim([110 270]);
ylim([70 260]);
text(225,79,'W32','Fontsize',18);
text(115,79,'(b)','Fontsize',18);
The output of the code is -
I have just manually set the position of all the axes.
The reason why tiled layout is not giving the desired result is that the line-
t = tiledlayout(1,2,'TileSpacing','none');
t1 = nexttile;
will create t1 axes as shown in the picture given below-
then when you run line
ax1 = axes;
The ax1 axes will cover and thus hide t1 axes as shown in the picture.
So to summarize, in your code, t1 is hidden by ax1. similarly ax1 is hidden by ax2 which is subsequently hidden by ax3 and ax4. So in your figure all the four axes(ax1,ax2,ax3,ax4) are present but you are able to see only figure b because ax3 and ax4 are hiding ax1 and ax2.But when you are plotting in individual figure you are getting desired result because there is no such case of one axes hiding another axes.
So the only workaround I can see as of now is to manually set the position of each axes.
Another workaround can be that you can use subfigure function instead of subplot but that will give you seperate windows for individual figure so that might not help you much. The link for the file of subfigure function is attached below-
Hope that helps!

카테고리

Help CenterFile Exchange에서 Blue에 대해 자세히 알아보기

제품


릴리스

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by