How to add error bars to a timeseries plot using datetime values
조회 수: 22 (최근 30일)
이전 댓글 표시
Hi all,
Please can someone help me figure out how to add error bars to my timeseries! I am gaving a problem figuring out what the x axis values should be. Would be much appreciated. (I'm new to Matlab!)
Context
I have a timeseries plot that runs Dec 17th - May 30th (alternate days). This is the code for my timeseries plot:
ts1 = timeseries(SSS_SGeast_anom_av_series, 1:2:166);
ts1.Name = ('Average salinity anomaly (psu)');
ts1.TimeInfo.Units = 'days';
ts1.TimeInfo.StartDate = '17-Dec-2020'; % Set start date.
ts1.TimeInfo.Format = 'dd mmm yy'; % Set format for display on x-axis.
ts.TimeInfo.Increment = '2';
ts1.Time = ts1.Time - ts1.Time(1); % Express time relative to the start date.
plot(ts1, '- k .', 'MarkerSize', 10, 'Linewidth', 2);
hold on
My standard deviation vector is a double numeric vector, size 83 x 1. I want to add this data as error bars on to my timeseries plot, using the command:
I have set y to by the timeseries y values (SSS_SGeast_anom_av_series), and err to be the standard deviation vector mentioned above.
Problem
I can't figure out what x values I need to suit the errorbar command above?
I think I need to somehow extract the datetime values from my original timeseries and then use these, but I can't figure out how to get those values.
Either that or make a new vector of the date values, and convert this to datetime. I'm not sure.
Please help if you can! Or let me know if I've been unclear.
댓글 수: 0
채택된 답변
Cris LaPierre
2021년 7월 14일
편집: Cris LaPierre
2021년 7월 14일
I have formatted errorbar to not draw a line connecting the data points. Technically, the marker isn't necessary either, but I wanted to keep this example simple.
% Create a data set
y = randn(83,10);
SSS_SGeast_anom_av_series = mean(y,2);
ts1 = timeseries(SSS_SGeast_anom_av_series, 1:2:166);
ts1.Name = ('Average salinity anomaly (psu)');
ts1.TimeInfo.Units = 'days';
ts1.TimeInfo.StartDate = '17-Dec-2020'; % Set start date.
ts1.TimeInfo.Format = 'dd mmm yy'; % Set format for display on x-axis.
ts.TimeInfo.Increment = '2';
ts1.Time = ts1.Time - ts1.Time(1); % Express time relative to the start date.
plot(ts1, '- k .', 'MarkerSize', 10, 'Linewidth', 2);
hold on
errorbar(ts1.Time,ts1.Data,std(y,[],2),'k.')
hold off
댓글 수: 7
Cris LaPierre
2021년 7월 14일
Possibly, but without knowing what the error message is, I wouldn't want to say. What happens if you copy and paste the code I've shared? Does that run without error?
Consider saving your variables to a mat file and attaching it to your post using the paperclip icon. Also copy/paste the code you are running (all the code needed to reproduce the error), as well as the full error message (all the red text).
추가 답변 (1개)
Sulaymon Eshkabilov
2021년 7월 14일
Maybe you should try to employ datenum(), e.g.:
x= datenum(ts1); % See help doc how to get datenum()
errorbar(x,y,ERR);
How to use datenum(): https://www.mathworks.com/help/matlab/ref/datenum.html#d123e278633
참고 항목
카테고리
Help Center 및 File Exchange에서 Calendar에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!