Match Xticks to Datapoints
18 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Hello,
I am currently trying to plot some kind of term structure of Value at Risk estimates and I am struggling to fit the xticks correctly. My code correctly displays 500 days with the approriate spaces between the "days", but I don't get how to match my datapoints (6 in total) to the right period (i. e. 10 days estimate to 10 days point, 30 days estimate to 30 days point and so on). I tried a different approach with datetime, but then it is impossible to just display days and not specific dates.
TermStructure = [VaR1D; VaR10D; VaR30D; VaR125D; VaR1Y; VaR2Y]
x = linspace(0,500,length(TermStructure))
plot(x, TermStructure)
xticks([1 10 30 125 250 500])
Thanks a lot in advance for any suggestions.
Leo
0 Kommentare
Akzeptierte Antwort
Weitere Antworten (1)
dpb
am 7 Feb. 2021
Bearbeitet: dpb
am 7 Feb. 2021
"with datetime, but then it is impossible to just display days and not specific dates."
Au contraire, good buddy!
tt=array2timetable(rand(100,1),'RowTimes',datetime(date)+days(linspace(0,500)).'); % make up something
>> head(tt)
ans =
8×1 timetable
Time Var1
____________________ _______
07-Feb-2021 00:00:00 0.16931
12-Feb-2021 01:12:43 0.78235
17-Feb-2021 02:25:27 0.34228
22-Feb-2021 03:38:10 0.54597
27-Feb-2021 04:50:54 0.55591
04-Mar-2021 06:03:38 0.20465
09-Mar-2021 07:16:21 0.59334
14-Mar-2021 08:29:05 0.93012
>> figure; plot(tt.Time,tt.Var1) % and plot it
>> hAx=gca; % get axes handle
>> hAx.XAxis.TickLabelFormat % display default time axes format
ans =
'MMM u'
>> hAx.XAxis.TickLabelFormat='dd'; % set to days, two digits
yields

This time scale happens to have default tick spacing at beginnings of months so all are day one, but it shows only the day.
You may be wanting a duration here, not a date if is elapsed days since some point.
Then again, it isn't clear to me what "match my datapoints (6 in total) to the right period" really means, either; if it is that you have a set of calculations (6?) to some calculated time frame over which those were calculated, you may not want a time axes at all but simply an ordinal plot with labels to match the period lengths.
TermStructure = randi(300,[6,1]);
x=categorical({'1Day'; '10Days'; '30Days; '125Days'; '1Year'; '2Years'}, ...
{'1Day'; '10Days'; '30Days'; '125Days'; '1Year'; '2Years'},'ordinal',1));
plot(x, TermStructure)

5 Kommentare
dpb
am 7 Feb. 2021
"what exactly you are doing in the line with "make something up"
Just created a dummy timetable with one variable over a time period of 100 entries spread out over 500 days. It was so had something to use in plot command below.
Siehe auch
Kategorien
Mehr zu Annotations finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
