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

KSSV
KSSV 2020년 9월 10일
Show us the code which you have tried.
Adam Danz
Adam Danz 2020년 9월 10일
편집: Adam Danz 2020년 9월 10일
Copy of the figure so others don't have to download it.
Sewwandhi Chandrasekara
Sewwandhi Chandrasekara 2020년 9월 10일
I gave the limit as
xlim([1 47]);
Adam Danz
Adam Danz 2020년 9월 10일
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.

댓글을 달려면 로그인하십시오.

답변 (1개)

Adam Danz
Adam Danz 2020년 9월 10일
편집: Adam Danz 2020년 9월 10일

0 개 추천

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

Sewwandhi Chandrasekara
Sewwandhi Chandrasekara 2020년 9월 10일
Dear Adam,
I tried this way and then the y values are changed as yours. But i have to keep the limits of y between 0.00215 to 0.00265 with the x limits between 1973 to 2019.
As an example [1973, 0.0024]..............[2019, 0.0025]
Adam Danz
Adam Danz 2020년 9월 10일
편집: Adam Danz 2020년 9월 14일
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.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터 및 File Exchange에서 Dates and Time에 대해 자세히 알아보기

태그

질문:

2020년 9월 10일

편집:

2020년 9월 14일

Community Treasure Hunt

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

Start Hunting!

Translated by