How to add error bars to a timeseries plot using datetime values
24 次查看(过去 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 中查找有关 Errorbars 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!


