inserting long xtick label

5 views (last 30 days)
TESFALEM ALDADA
TESFALEM ALDADA on 7 Mar 2020
Edited: dpb on 8 Mar 2020
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
Star Strider
Star Strider on 7 Mar 2020
See the documentation for plot timeseries.
The other option (if you did not use a datetime array) is to use the datetick function.
TESFALEM ALDADA
TESFALEM ALDADA on 8 Mar 2020
Edited: dpb on 8 Mar 2020
ok, i am working to plot using the left and right axis where this method is not woking for me.
furthermore it shows error message when i use a set(gca, ...) option to further process,
maybe see below please
yyaxis left
ts1 = timeseries(Qobs,1:4383);
ts1.Name = 'Daily Count';
ts1.TimeInfo.Units = 'days';
ts1.TimeInfo.StartDate = '01-Jan-2000'; % Set start date.
ts1.TimeInfo.Format = 'mmm dd, yy'; % Set format for display on x-axis.
ts1.Time = ts1.Time - ts1.Time(1); % Express time relative to the start date.
hL=plot(X,Qobs, 'g', X,Qsim);
ylabel('Flow (m^3/s)')
xlabel('Daily flow')
% legend('Observed','Simulated')
% hL.FaceColor = [1 0 0];
set(gca,'xtick',1:4383,'ylim', [0,12])
plot(ts1)
yyaxis right
hR=bar(X,P,5, 'k'); %
set(gca,'YColor', 'k','Ydir','reverse','ylim',[0,50],'xlim',[1,4383], 'xtick',1:500:4383)
ylabel('Rainfall (mm)')
xlim([1 4383])

Sign in to comment.

Accepted Answer

dpb
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.
  1 Comment
TESFALEM ALDADA
TESFALEM ALDADA on 8 Mar 2020
i tried this way but it dont works for daily time frames. please can you give me some example, and also i have two axis plot; one using plot function and the other axis has bar plot.
how can i adjust for this.
best

Sign in to comment.

More Answers (0)

Categories

Find more on Formatting and Annotation 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!