How do I increase X-axis tick marks to include every year in the data?

Hi, I am making a tiled layout plot of a time series from 1998-2019 and would like the x-axis to have a tick mark and label for each year on ax1 and ax3. I've tried using xticks command but keep getting error messages. See code below for what I have so far, the image attached is what the code produces. Thanks!!!
x = Date;
y1 = HypVol_1;
y2 = HypVolAnom_1;
y3 = HypVolAnom_2;
y4 = HypVolAnom_3;
t = tiledlayout(4,1);
ax1 = nexttile;
T1 = plot(ax1,x,y1);
hold on
T2 = plot(x,HypVol_2);
T3 = plot(x,HypVol_3);
ylabel('Hypoxic Volume (%)')
hold off
set(T1,'Marker','.')
set(T2,'Marker','.')
set(T2,'Color','k')
set(T3,'Marker','.')
legend('section 1','section 2','section 3')
legend('Location','EastOutside')
ax2 = nexttile;
M1 = plot(ax2,x,y2);
ylabel('Section 1 Anomaly')
set(M1,'Marker','.')
line(xlim(), [0,0], 'LineWidth', 0.5, 'Color', 'k');
ax3 = nexttile;
S1 = plot(ax3,x,y3);
ylabel('Section 2 Anomaly')
set(S1,'Marker','.')
line(xlim(), [0,0], 'LineWidth', 0.5, 'Color', 'k');
ax4 = nexttile
H1 = plot(ax4,x,y4)
ylabel('Section 3 Anomaly')
set(H1,'Marker','.')
line(xlim(), [0,0], 'LineWidth', 0.5, 'Color', 'k');
xticklabels(ax4,{})
xticklabels(ax2,{})
xlabel(t,'Time (Years)')
t.TileSpacing = 'compact';

4 Comments

What did you try, and what error message did you receive?
More significant what is Date (x)?
whos Date
would be informative.
As a matter of style, I'd strongly encourage you to use arrays and subscripting for similar variables rather than sequentially-named variables...
hAx(1)=nexttile;
...
hAx(2)=nexttile;
...
for example. One can then, if do similar with the other variables, write compact loops or even vector expressions instead of having to repeat the same code over umpteen times...
dpb has asked the important question about the contents of Date. I had two stylistic suggestions:
M1 = plot(ax2,x,y2);
ylabel('Section 1 Anomaly')
set(M1,'Marker','.')
line(xlim(), [0,0], 'LineWidth', 0.5, 'Color', 'k');
The first and third lines of that excerpt from your code can be combined into one.
M1 = plot(ax2, x, y2, 'Marker', '.');
Also if you're using a release that includes it, I'd use the yline function for the fourth line of that excerpt.
yline(0, 'LineWidth', 0.5, 'Color', 'k')
Or if you're using it to simulate the X axes being drawn at y = 0, use the XAxisLocation axes property instead.
>> axes('XAxisLocation', 'origin')
>> axis([0 360 -1 1])
Date is month/year and is in datetime format. There is a data point for each month of the year. So first point starts at Jan 1998 and last point ends on Dec 2019. Thank you for the stylistic suggestions! I am new to making plots in matlab so I definitley appreciate the help!

Sign in to comment.

Answers (1)

Here's a demo you can apply to your data. It sets the x-tick values to January 1st of each year spanning the range of dates in your data. So, if your dates range from March 16, 2000 to September 24, 2020, the date ticks will range from January 1, 2000 to January 1, 2021.
% Create vector of dates and y-values
dates = datetime(2000,1,1) + days(0:15:365*20);
y = rand(size(dates));
% plot the data
clf()
ax = axes();
plot(ax, dates, y, 'o')
% Compute the first day of each year that spans the range of your dates
% The "-days(1)" is to prevent an extra year from appearing at the max
% x-tick if your dates end on January 1st of any year.
yearTicks = dateshift(min(dates), 'Start', 'year') : calyears(1) : dateshift(max(dates)-days(1), 'Start', 'year')+calyears(1);
% Set x ticks
ax.XTick = yearTicks
ax.XLim = [yearTicks(1), yearTicks(end)];

Tags

Asked:

on 26 Aug 2020

Edited:

on 31 Aug 2020

Community Treasure Hunt

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

Start Hunting!