How to plot years on x axis

I have monthly mean temperature data for about 31 years from 1975-2005. Each year I am having 12 values. How to plot this data having years on x-axis.
The format of the data is shown below:
1975 -66.0577
1975 -66.3039
1975 -65.4235
1975 -66.6143
1975 -66.1449
1975 -70.4912
1975 -73.0337
1975 -73.8322
1975 -69.5114
1975 -65.6445
1975 -65.815
1975 -65.5371
1976 -63.9651
1976 -66.7147
1976 -66.5698
1976 -66.4407
1976 -66.4912
1976 -69.1049
1976 -71.5498
1976 -73.0278
1976 -70.1491
1976 -69.9583
1976 -68.8625
1976 -63.2717

 Respuesta aceptada

Star Strider
Star Strider el 21 de En. de 2019

0 votos

There are several ways to approach that, including datetime (link) arrays.
A simple solution is:
M = [ 1975 -66.0577
1975 -66.3039
1975 -65.4235
1975 -66.6143
1975 -66.1449
1975 -70.4912
1975 -73.0337
1975 -73.8322
1975 -69.5114
1975 -65.6445
1975 -65.815
1975 -65.5371
1976 -63.9651
1976 -66.7147
1976 -66.5698
1976 -66.4407
1976 -66.4912
1976 -69.1049
1976 -71.5498
1976 -73.0278
1976 -70.1491
1976 -69.9583
1976 -68.8625
1976 -63.2717];
T = datenum([M(:,1) ceil(rem(1:size(M,1),12.1))', zeros(size(M,1),3) ones(size(M,1),1)]);
figure
plot(T, M(:,2))
datetick('x', 'yyyy')
xlim([min(T), max(T)])
Experiment to get the result you want.

8 comentarios

Vivek
Vivek el 22 de En. de 2019
This code is working well but when try on long term data the time series graph does't come in proper way.
Vivek
Vivek el 22 de En. de 2019
When apply on long term data the figure comes like this....can you please explain the error.
untitled.jpg
Star Strider
Star Strider el 22 de En. de 2019
I have no idea what the error is.
It could be that some of the data are out of order, or perhaps some months were missing from your data.
Vivek
Vivek el 22 de En. de 2019
Find the attached data file here...
Star Strider
Star Strider el 22 de En. de 2019
This works:
M = load('m.txt');
T = datetime(M(1,1),1,1) : calmonths(1) : datetime(M(end,1),12,1);
figure
plot(T, M(:,2))
xlim([min(T), max(T)])
The datetime (link) functions were introduced in R2014b.
Vivek
Vivek el 23 de En. de 2019
Thanks...it is working well
Star Strider
Star Strider el 23 de En. de 2019
As always, my pleasure.
I should have done that to begin with, rather than creating my own vector.
Apostolos Ampountolas
Apostolos Ampountolas el 6 de Abr. de 2019
Unfortunately, it doesn't work for me.
I downloaded the file, copy, run the provided code, however I am always getting the same error:
Error using plot
Vectors must be the same length.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Etiquetas

Preguntada:

el 21 de En. de 2019

Comentada:

el 6 de Abr. de 2019

Community Treasure Hunt

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

Start Hunting!

Translated by