Adding years to my plot
Mostrar comentarios más antiguos
Hi, I had line plot and I need to add years starting from 1973 to 2019. Instead of years i can only get numbers from 1 to 47 (figure is attached).
How can i get the years representation to X axis.
Thanks in advance
4 comentarios
KSSV
el 10 de Sept. de 2020
Show us the code which you have tried.
Sewwandhi Chandrasekara
el 10 de Sept. de 2020
Adam Danz
el 10 de Sept. de 2020
Then x are indices or some other numeric value you supplied. You'll need to create a datetime vector to use as your x-data. My answer shows how to do that.
Respuestas (1)
This values 0:50 are probably indices (note that the first data point does not start at x=0). They could be durations too, I suppose. They could even be years using the 'yy' datetime format but I doubt it.
My best guess is that they are indices and you created the plot using
plot(y)
or
plot(x,y), where x are values 0:50, for example.
Instead, create a datetime vector of years and use that as your x-data. For example,
dt = datetime(1971,1,1) : calyears(1) : datetime(2019,1,1);
y = rand(size(dt));
plot(dt, y)
Another way of creating the datetime vector,
dt = datetime(1973,1,1) + calyears(0:46);

2 comentarios
Sewwandhi Chandrasekara
el 10 de Sept. de 2020
You can use whatever y values you want to use. The y values in my answer are only to demonstrate the method.
You can use xlim() with two datetime values to set the date range.
Categorías
Más información sobre Dates and Time en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
