Clear Filters
Clear Filters

Problems with xaxis ticks and labels

3 views (last 30 days)
Katharine S
Katharine S on 4 May 2020
Commented: Tommy on 4 May 2020
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!

Accepted Answer

Tommy
Tommy on 4 May 2020
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 Comments
Katharine S
Katharine S on 4 May 2020
Thank you! I also found that adding in xlim([0 35]) works too after you mentioned the limits.

Sign in to comment.

More Answers (0)

Categories

Find more on Specifying Target for Graphics Output in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!