Xticks as Days for specific data points

Dear all,
I am computing Value at Risk estimates for different time horizons and I would like to plot them against regular days. I know that you can specify the ticks of the x-axis somehow via datetime, but i don't seem to get the syntax correctly. You can see my code below.
I calculated different VaRs for 10, 30, 125 and 250 days and plot those data points against those 250 days, but I only manage to do that evenly spaced (e.g. same space between 10 and 30 and 30 and 125) which is obviously wrong.
set(gca,'XTick',datetime(1,1,250),'Xticklabel',[10 30 125 250])
Any help would be highly appreciated.
Leo

Risposte (1)

Something along the lines of that code should work if you plotted your data with datetime X data.
daysSinceJanuary1st = randi([0 364], 1, 10)
daysSinceJanuary1st = 1×10
104 26 143 299 28 272 344 295 69 213
x = datetime(2021, 1, 1) + days(daysSinceJanuary1st)
x = 1×10 datetime array
15-Apr-2021 27-Jan-2021 24-May-2021 27-Oct-2021 29-Jan-2021 30-Sep-2021 11-Dec-2021 23-Oct-2021 11-Mar-2021 02-Aug-2021
h = plot(x, 1:10);
xticks(datetime(2021, 1:12, 1))
xlim(datetime([2021, 2022], 1, 1))
If you want to customize the ticks or the tick labels further, you may need or want to work with the ruler for the X axis directly.
theAxes = ancestor(h, 'axes');
theRuler = theAxes.XAxis
theRuler =
DatetimeRuler with properties: Limits: [01-Jan-2021 01-Jan-2022] TickValues: [01-Jan-2021 01-Feb-2021 01-Mar-2021 01-Apr-2021 01-May-2021 01-Jun-2021 01-Jul-2021 01-Aug-2021 01-Sep-2021 01-Oct-2021 01-Nov-2021 01-Dec-2021] TickLabelFormat: 'MMM' Show all properties

1 Commento

Thanks so far, but that doesn't seem to work for me. Maybe datetime is not suitable for what i'm looking for. I just need 250 days (or time increments) on the x axis and then I would like to match 10, 30, 125 and 250 with my calculated VaR estimates.
Do you have any suggestions for that?
Thank you!

Accedi per commentare.

Prodotti

Release

R2020b

Richiesto:

il 5 Feb 2021

Commentato:

il 5 Feb 2021

Community Treasure Hunt

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

Start Hunting!

Translated by