How to remove white space from beginning of a plot and adjust x-axis to start from 'January'?

10 views (last 30 days)
I have a plot with monthly data over multiple years. But there is a white space in the beginning of the plot that I want to remove. If I use 'axis tight' or 'xlim([1 12])', then 'January' from the x-axis disappears (refer to image 'Temp_fig.jpg'). I want the x axis as 'Jan Feb Mar Apr Oct Nov Dec'.
T = table(datestr(Temp6ONDJFMAcorrect.DATE,'dd/mm/yyyy',),Temp6ONDJFMAcorrect.Temp);
[y,m,d] = datevec(T.Var1,'dd/mm/yyyy');
Tdate = table(y,m,d,'VariableName',{'year','month','day'});
TT = [Tdate,T(:,{'Var2'})];
TT.Properties.VariableName{4} = 'Temp';
yrs = TT.year;
yr = ismember(str2double(string(TT.year)),yrs);
x = reshape(TT.monthly(yr),7,[]);
y = reshape(TT.Temp(yr),7,[]);
plot(x,y)
xticklabels({'Jan', 'Feb','Mar','Apr','Oct','Nov','Dec'})
%% xlim([1,12]) %% this removes the white space but 'Jan' from x-ticks also disappears (see 'Temp_fig_1.jpg')

Accepted Answer

VBBV
VBBV on 14 Feb 2023
xticks(1:7)
xticklabels({'Jan', 'Feb','Mar','Apr','Oct','Nov','Dec'})
You can try the above
  2 Comments
Anwesha Sharma
Anwesha Sharma on 14 Feb 2023
Edited: Anwesha Sharma on 14 Feb 2023
It's still not working, the axis labels are not equally spaced and there is still a space in front of the plot after I run:
xticks(1:7)
xticklabels({'Jan', 'Feb','Mar','Apr','Oct','Nov','Dec'}) .
I am not sure how to fix that.
VBBV
VBBV on 14 Feb 2023
Edited: VBBV on 15 Feb 2023
plot(1:7) % your plot
xlim([1 12]) % try with both
xticks(1:12) % use 1:12 instead of 1:7
xticklabels({'Jan', 'Feb','Mar','Apr','Oct','Nov','Dec'})

Sign in to comment.

More Answers (1)

Cris LaPierre
Cris LaPierre on 17 Feb 2023
Edited: Cris LaPierre on 17 Feb 2023
Perhaps a bit more difficult to understand at first, but here's another way to do it using findgroups and splitapply.
file = 'https://www.mathworks.com/matlabcentral/answers/uploaded_files/1295380/Temp_6_ONDJFMA_correct.csv';
opts = detectImportOptions(file);
opts = setvaropts(opts, "DATE","InputFormat","dd/MM/yyyy");
T = readtable(file,opts);
% Need to create x values that are shared across all years so they overlap
% Here, I create a copy of DATE and change the year so all are 1960
T.plotDATE = T.DATE;
T.plotDATE.Year = year(T.DATE(1))
T = 441×3 table
DATE Temp plotDATE __________ _______ __________ 01/01/1960 -5.6962 01/01/1960 01/02/1960 -10.261 01/02/1960 01/03/1960 -16.191 01/03/1960 01/04/1960 -21.453 01/04/1960 01/10/1960 -19.267 01/10/1960 01/11/1960 -12.483 01/11/1960 01/12/1960 -8.3811 01/12/1960 01/01/1961 -9.1333 01/01/1960 01/02/1961 -8.1571 01/02/1960 01/03/1961 -21.066 01/03/1960 01/04/1961 -20.587 01/04/1960 01/10/1961 -17.296 01/10/1960 01/11/1961 -9.8484 01/11/1960 01/12/1961 -8.5044 01/12/1960 01/01/1962 -8.6625 01/01/1960 01/02/1962 -12.851 01/02/1960
% Find groups
G = findgroups(year(T.DATE));
% Plot. Need 'figure' since 'hold on' is used before first plot command
figure
hold on
splitapply(@(x,y)plot(x,y),T(:,["plotDATE","Temp"]),G)
hold off
% A datetime axis will include the year. This removes that by manually
% labeling X Ticks
ax = gca;
xlbls = ax.XTickLabel;
ax.XTickLabel = xlbls;
Since you have to manually label the XTicks anyway, another way to do this is create a Month column with the month names as categoricals. The categorical function lets you specify an ordered set. The plot is a little bit different (space between Y axis and Jan)
T2 = readtable(file,opts);
T2.Month = categorical(month(T2.DATE,"monthofyear"),...
1:12,{'Jan','Feb','Mar','Arp','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'})
T2 = 441×3 table
DATE Temp Month __________ _______ _____ 01/01/1960 -5.6962 Jan 01/02/1960 -10.261 Feb 01/03/1960 -16.191 Mar 01/04/1960 -21.453 Arp 01/10/1960 -19.267 Oct 01/11/1960 -12.483 Nov 01/12/1960 -8.3811 Dec 01/01/1961 -9.1333 Jan 01/02/1961 -8.1571 Feb 01/03/1961 -21.066 Mar 01/04/1961 -20.587 Arp 01/10/1961 -17.296 Oct 01/11/1961 -9.8484 Nov 01/12/1961 -8.5044 Dec 01/01/1962 -8.6625 Jan 01/02/1962 -12.851 Feb
G = findgroups(year(T2.DATE));
figure
hold on
splitapply(@(x,y)plot(x,y),T2(:,["Month","Temp"]),G)
hold off

Categories

Find more on Dates and Time 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!