Hello.
I am having some problems with my x axis.
Y=NaN(12,nyr);
%
for i=1:nyr
%
Y(:,i)=mnCML((i-1)*12+1:i*12);
%
end
%
figure('NumberTitle','off','Name','boxes')
%
boxplot(Y), hold on
%
plot([0 35],mean(mnCML)*[1 1],'r--')
%
xtics=[0:5:35];
xticlab={'1980' '1985' '1990' '1995' '2000' '2005' '2010' '2015'};
set(gca,'XTick',xtics,'XTickLabel',xticlab)
I have 34 years of data (1981 - 2014). I am displaying yearly box plots. I want the axis to start at 1980 and end at 2015. I have attached the current graph, which starts at 1985 and ends at 2010. I essentially want to have labels at the borders of the chart before and after the data years. Any advice? Thanks!

 채택된 답변

Tommy
Tommy 2020년 5월 4일

0 개 추천

The x axis limits are from 1 to 34, based on your box plot. When you call hold on and then plot a line from 0 to 35, the limits don't update. One option is to update the limits after everything is plotted by adding axis tight at the end:
Y=NaN(12,nyr);
%
for i=1:nyr
%
Y(:,i)=mnCML((i-1)*12+1:i*12);
%
end
%
figure('NumberTitle','off','Name','boxes')
%
boxplot(Y), hold on
%
plot([0 35],mean(mnCML)*[1 1],'r--')
%
xtics=[0:5:35];
xticlab={'1980' '1985' '1990' '1995' '2000' '2005' '2010' '2015'};
set(gca,'XTick',xtics,'XTickLabel',xticlab)
axis tight

댓글 수: 4

Katharine S
Katharine S 2020년 5월 4일
That does work for the x axis, thank you. But how do I preserve the y axis? It also tightens the y axis to where the whiskers at the bottom are now hard to see.
Good point. One way is to store the y limits before calling axis tight and then reset the y limits afterwards:
limy = ylim;
axis tight
ylim(limy)
Or, better to work with graphics object handles rather than gca and gcf:
f = figure('NumberTitle','off','Name','boxes');
%
ax = axes(f);
boxplot(ax, Y), hold(ax, 'on')
%
plot(ax,[0 35],mean(mnCML)*[1 1],'r--')
%
xtics=[0:5:35];
xticlab={'1980' '1985' '1990' '1995' '2000' '2005' '2010' '2015'};
set(ax,'XTick',xtics,'XTickLabel',xticlab)
limy = ylim(ax);
axis(ax, 'tight')
ylim(ax, limy)
Katharine S
Katharine S 2020년 5월 4일
Thank you! I also found that adding in xlim([0 35]) works too after you mentioned the limits.
Tommy
Tommy 2020년 5월 4일
Happy to help!

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Data Distribution Plots에 대해 자세히 알아보기

태그

질문:

2020년 5월 4일

댓글:

2020년 5월 4일

Community Treasure Hunt

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

Start Hunting!

Translated by