How to customize the subplots?

조회 수: 116 (최근 30일)
BN
BN 2020년 2월 5일
댓글: dpb 2020년 2월 5일
I searched a lot and read the documentation in Matlab, in order to plot 6 plot in one figure. Now it is done but really hard to view:
I want to set 5 years intervals for the x-axis in all plots and small the font size of the x-axis and y-axis in all subplots. Also, I would like to show each subplot with a specific color.
Here is my code:
figure;
subplot(2,3,1);
plot(Date,SI_table.SPI_1month, 'linewidth',1);
xlabel('time step')
ylabel('SPI 1month')
xticks(Date(1:12:end))
ax=gca;
set(ax,'XTickLabelRotation',90)
set(gca,'XTick',(5))
subplot(2,3,2);
plot(Date,SI_table.SPI_3month, 'linewidth',1);
xlabel('time step')
ylabel('SPI 3month')
xticks(Date(1:12:end))
ax=gca;
set(ax,'XTickLabelRotation',90)
subplot(2,3,3);
plot(Date,SI_table.SPI_6month, 'linewidth',1);
xlabel('time step')
ylabel('SPI 6month')
xticks(Date(1:12:end))
ax=gca;
set(ax,'XTickLabelRotation',90)
subplot(2,3,4);
plot(Date,SI_table.SPI_12month, 'linewidth',1);
xlabel('time step')
ylabel('SPI 12month')
xticks(Date(1:12:end))
ax=gca;
set(ax,'XTickLabelRotation',90)
subplot(2,3,5);
plot(Date,SI_table.SPI_24month, 'linewidth',1);
xlabel('time step')
ylabel('SPI 24month')
xticks(Date(1:12:end))
ax=gca;
set(ax,'XTickLabelRotation',90)
subplot(2,3,6);
plot(Date,SI_table.SPI_48month, 'linewidth',1);
xlabel('time step')
ylabel('SPI 48month')
xticks(Date(1:12:end))
ax=gca;
set(ax,'XTickLabelRotation',90)
Thank you so much

채택된 답변

dpb
dpb 2020년 2월 5일
Just make the additional adjustments to axes/line properties in each subplot as you create it.
As you've written it, that means adding the same code line six different places in the code for every additional line of code you need; rather tedious.
If you want to set a property for all subplots, you can do that using the cell array syntax of handles/properties for the set function, but to do that you needed to have saved the handles to all the axes instead of using a single variable repetitively as you had done above.
It would be more efficient coding to use a loop...
plotMonths=[1,3,6,12,24,48].'; % list of month intervals desired for plot
plotColors=['r';'b';'k';'g';'o';'c']; % colors; use whatever desired or rgb triplet
figure;
for i=1:6
hAx(i)=subplot(2,3,i); % create subplot, save axes handle
hL(i)=plot(Date,SI_table.("SPI_"+plotMonths(i)+"month"), 'linewidth',1,plotColors(i));
xlabel('time step')
ylabel("SPI "+plotMonths(i)+"month")
xticks(Date(1:12:end))
set(hAx(i),'XTickLabelRotation',90)
end
  댓글 수: 2
BN
BN 2020년 2월 5일
편집: BN 2020년 2월 5일
Dear dpb
Nice and clear Idea, but unfortunately in this line:
hL(i)=plot(Date,SI_table.("SPI_"+plotMonths(i)+"month"), 'linewidth',1,plotColors(i));
I gor this error:
Error using plot
Data must be a single matrix Y or a list of pairs X,Y.
Error in Untitled5 (line 6)
hL(i)=plot(Date,SI_table.("SPI_"+plotMonths(i)+"month"),
'linewidth',1,plotColors(i));
dpb
dpb 2020년 2월 5일
Oh, yeah...a color triplet must precede any named parameters or once a named parameter is used, everything after that must also use the names...either
hL(i)=plot(Date,SI_table.("SPI_"+plotMonths(i)+"month"),plotColors(i),'linewidth',1);
or
hL(i)=plot(Date,SI_table.("SPI_"+plotMonths(i)+"month"),'color',plotColors(i),'linewidth',1);
will work...read the doc carefully.

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

추가 답변 (1개)

Alberto Mora
Alberto Mora 2020년 2월 5일
Have a look to XTrick and XTrickLabel
  댓글 수: 1
BN
BN 2020년 2월 5일
Thank you, Do they work in subplots? I thought they act only in the plot. I don't know how to customize each subplot.

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

카테고리

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

태그

제품


릴리스

R2018b

Community Treasure Hunt

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

Start Hunting!

Translated by