How to add error bars to a timeseries plot using datetime values
19 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Mairead Smith
el 14 de Jul. de 2021
Comentada: Cris LaPierre
el 14 de Jul. de 2021
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 comentarios
Respuesta aceptada
Cris LaPierre
el 14 de Jul. de 2021
Editada: Cris LaPierre
el 14 de Jul. de 2021
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 comentarios
Cris LaPierre
el 14 de Jul. de 2021
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).
Más respuestas (1)
Sulaymon Eshkabilov
el 14 de Jul. de 2021
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
2 comentarios
Ver también
Categorías
Más información sobre Calendar en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!