inserting long xtick label
4 views (last 30 days)
Show older comments
Hi,
I wanted to insert long time series of xticklabel for my plot.
I tried manually using:
set(gca, 'xticklabels' ,{'1/1/1990', '1/2/1990', '3/1/1990', . . . since the date length is long (15 years) i could not type all time series. so what is the short cut to inser this xtick?
Thank you
2 Comments
Accepted Answer
dpb
on 7 Mar 2020
Edited: dpb
on 8 Mar 2020
Don't label manually at all; use a datetime variable for the x-axis variable in plot() and you'll get datetime ticklabels automagically.
ADDENDUM:
t=1:4383; t=t.';t=t-t(1); % number days vector
Qobs=randn(size(t)); % dummy data to match
ts1 = timeseries(Qobs,t); % create the ts object
ts1.TimeInfo.Units='days';
ts1.TimeInfo.StartDate='01-Jan-2000';
ts1.TimeInfo.Format='mmmyy'; % bug/implementation issue -- uses datenum() strings internally
plot(ts1)
hAx=gca;
xl=xlim; % retrieve xlimits
xl(2)=xl(2)+days(1); % make RH axis end at 1/1 next year
xlim(xl) % update xlim, now draws last tick label
produced
Finish up as desired.
Not knowing what X is, no way to know what to do about adding a bar plot at this point...
NB: The bug/implementation snafu on the TS date format string. Setting it to 'MMMyy' as is correct for datetime object produced warning/error when tried to plot as
>> plot(ts1)
Error using matlab.internal.datetime.cnv2icudf (line 157)
Unrecognized minute format. Format: MMMyy.
Error in timeseries/plot (line 142)
datetimeTickFormat = matlab.internal.datetime.cnv2icudf(char(h.TimeInfo.Format));
>>
Cap-M is minute field for datenum variables, NOT datetime class which is what the timeseries and new-fangled DatetimeRuler object is for the datetime aware plot functions. In fact, after the plot() line above,
>> hAx.XAxis
ans =
DatetimeRuler with properties:
Limits: [Jan 01, 2000 Jan 01, 2012]
TickValues: [Jan 01, 2000 Jan 01, 2002 Jan 01, 2004 Jan 01, 2006 Jan 01, 2008 Jan 01, 2010 Jan 01, 2012]
TickLabelFormat: 'MMMyy'
Show all properties
>>
shows it was converted.
More Answers (0)
See Also
Categories
Find more on Annotations 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!