Clear Filters
Clear Filters

Variable X-Tick Labeling

3 views (last 30 days)
Howard
Howard on 28 Jul 2020
Edited: Adam Danz on 28 Jul 2020
This may seem like a simple question, but I am having issues trying to do this. I have a dataset of 213 months of data from Oct 2002 to June 2020; what I would like to do is to plot the 213 points on the Y-axis, but on the X-axis, I would like the date ticks to show only 18 fiscal year (Oct-Sep) numbers as follows: '03 '04 '05 '06...through '20. So, is there some way that I can use the following command 'xticklabel',{'03','04','05','06','07','08','09','10','11','12','13,''14','15','16','17','18','19','20'}) to plot just these along the x-axis that would be spaced 12 months apart so that the data plotted on the y-axis match up to these spaced fiscal years?

Accepted Answer

Adam Danz
Adam Danz on 28 Jul 2020
Edited: Adam Danz on 28 Jul 2020
The cleanest solution IMO is to convert your datetime values to the fiscal year values and use the fiscal year values to plot your data. Data unit conversion is almost always cleaner than messing with XTickLabel.
Demo
Create data.
dt are 213 date time values spanning from Oct 15, 2002 to July 28, 2020.
y are 213 random integers between 1 and 100.
dt = linspace(datetime(2002,10,15), datetime(2020,07,28), 213);
y = randi(100,1,213);
Set starting date of fiscal year 1.
startDate = datetime(2002,10,1); % Oct 1, 2002
Convert the datetime values to fiscal year values. The +1 is there to start with fiscal year number 1. If you want to start with 0, remove the +1.
fiscalYears = years(dt-startDate) + 1;
Plot the data using fiscal year units
plot(fiscalYears, y, 'o')
xlabel(sprintf('Fiscal year starting on %s', datestr(startDate)))
ylabel('random data')
Note two ambiguities with this plot.
  1. Is x=1 the beginning or ending of the first fiscal year? This can be solved with a better x-axis label or additional text on the figure.
  2. If you're starting with x=1, what does x=0 mean? This can be solved by either setting the lower x axis limit to 1 or starting with x=0 which may be more intuitive.
  2 Comments
Howard
Howard on 28 Jul 2020
Very helpful and many thanks!
Adam Danz
Adam Danz on 28 Jul 2020
Edited: Adam Danz on 28 Jul 2020
Glad I could help.

Sign in to comment.

More Answers (0)

Categories

Find more on Dates and Time in Help Center and File Exchange

Tags

Products


Release

R2017b

Community Treasure Hunt

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

Start Hunting!