Time series graph with best fit line

How can I make a time series graph with year on the x axis and temperature on the y-axis. I would like to add a best fit line to show the trend. The date of the data had converted into the matlab date format. But I would like to display as year(e.g. 1999) on my x axis.

 Accepted Answer

Stephan
Stephan on 1 May 2018
Great,
always when i try to help people here i learn a lot ;-)
The files are attached. After converting to the correct datetime format i fitted a linear model. Not all values could be used - so the vectors have different length. Due to this the coefficients of the linear model are taken to calculate a new vector for the values of the trend line. After this operation the data can be plotted over the datetime in years.
I did this for one of the records I called "low". You can continue working according to this pattern.
I assume that these are daily maximum values and daily low values, respectively. Of course, one could also form the mean of these two values and continue to work with them - but that was not your question.
I hope I could help you.
Best regards
Stephan

2 Comments

Dear Stephan
Thanks a lot for your help
Stephan
Stephan on 1 May 2018
Edited: Stephan on 1 May 2018
Hi,
no problem. If it worked for you please accept answer. That makes it easier for others with the same problem to find a solution here.
Best regards
Stephan

Sign in to comment.

More Answers (3)

Stephan
Stephan on 29 Apr 2018
Edited: Stephan on 29 Apr 2018
Hi,
insert:
ax = gca;
ax.XAxis.TickLabelFormat = 'yyyy';
after your plot command.
See also here for further information:
Best regards
Stephan

3 Comments

Aoyi
Aoyi on 29 Apr 2018
Edited: Aoyi on 29 Apr 2018
Here is my code to make the graph but without the best fit line
MIDAS00525temp = load('../TimeseriesProject/MIDAS_srcid00525_daily_temperature.txt');
time = MIDAS00525temp(:,2);
temp = MIDAS00525temp(:,3);
mdl = fitlm(time, temp, 'linear', 'RobustOpts','on');
plot(time, temp, '+k', 'MarkerSize', 16, 'LineWidth',1.5);
ax = gca;
ax.XAxis.TickLabelFormat = 'yyyy';
But Matlab show this error when I try to make the year on x axis
Error setting property 'TickLabelFormat' of class 'NumericRuler': Value must be a supported tick label format specification. For example '%.2f m' formats with 2 digits after the decimal point and appends the suffix ' m'. Error in Time series Project (line 74) ax.XAxis.TickLabelFormat = 'yyyy';
Do you know why it happens? Thanks for your help
Hi,
Can you attach your file?
Best regards
Stephan
Dear Stephen
Thanks a lot for your help

Sign in to comment.

Stephan
Stephan on 29 Apr 2018
Edited: Stephan on 29 Apr 2018
Hi,
how do you convert this data to datetime format in matlab?
is the start of this file 30-Nov-1982?
best regards
Stephan

1 Comment

It is my assessment and the data has already been converted by my professor.

Sign in to comment.

Stephan
Stephan on 29 Apr 2018
Edited: Stephan on 29 Apr 2018
Hi,
it took a little time to convert your .txt file to something matlab really could create a datetime-dataformat from ;-). File is attached. Is this correct so far? Do you start in the endof 1982 and look into the 2060's? If yes we could take the next step and think about, how the regression line could work.
The code so far is:
load('date_time_data.mat'); % file is attached . please check ;-)
subplot(2,1,1);
plot(datetime, temp);
subplot(2,1,2);
plot(datetime, temp,'+k', 'MarkerSize', 16, 'LineWidth',1.5);
ax = gca;
ax.XAxis.TickLabelFormat = 'yyyy';
Hope we are on the right way...
Best regards
Stephan

4 Comments

As the data has already converted to matlab time format by my professor, and I do not know how to convert it back to year. So I don't know when it starts.
I would also like to projected to 2060 if it is possible.
Thanks a lot for your help.
Ask him then we can take the next steps.
The time is from 1882-2018
year = datevec(time);
Then it will show the year in another file. But I don't know how to use it to make the graph.
Thanks a lot for your help

Sign in to comment.

Categories

Asked:

on 28 Apr 2018

Edited:

on 1 May 2018

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!