How to change my x axis value
조회 수: 117 (최근 30일)
이전 댓글 표시
Nurul Ain Basirah Zakaria
2021년 1월 14일
댓글: Nurul Ain Basirah Zakaria
2021년 1월 15일
Hello. Is it possible for me to change my x axis value? My data is from 1982-2017. I plot and came out like this:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/488063/image.png)
but I want it to be like this:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/488068/image.png)
The x-axis showing the year.
Thank you in advance!
댓글 수: 0
채택된 답변
the cyclist
2021년 1월 14일
편집: the cyclist
2021년 1월 14일
It would be helpful if you posted your data, so we know exactly how it is stored (e.g. do you just have the years stored as numeric values, or do you have whole dates stored as datetime type?).
But, it looks like you did
plot(y)
which will default to plotting x = 1:N, where N is the number of data points in y.
Instead, you should
plot(date_data,y)
댓글 수: 12
the cyclist
2021년 1월 14일
Assuming that the dates generated like that do correspond to the dates you want to associate with your data, then yes it works:
load 'SPI3 PBS.mat'
t1 = datetime(1982,01,01);
t2 = datetime(2017,12,31);
date_months=t1:calmonths(1):t2;
plot(date_months,SPI3)
I would recommend against using date as the name of your variable, since date() is the name of a MATLAB function.
추가 답변 (1개)
WalterWhite
2021년 1월 14일
xticks(1980:5:2020)
댓글 수: 5
WalterWhite
2021년 1월 14일
xticks(1980:5:2020) %try pasing this code under your plot(spi3)
the cyclist
2021년 1월 14일
편집: the cyclist
2021년 1월 14일
To be clear about what I was saying, if OP has plotted with one variable, like this
rng default
figure
plot(rand(1,9))
they will get the plot
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/488138/image.png)
and this plot will not be fixed by adding
xticks(1980:5:2020)
which will create ticks at x-axis positions 1980:5:2020, and the plot will then look like this
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/488143/image.png)
because the data are at 1:9, and the ticks are far away to the right at 1980-2020. Instead, they could use
xticklabels(1980:5:2020)
to change the labels of the existing tick marks at 1:9 into 1980:5:2020, yielding
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/488148/image.png)
But I think it is fundamentally better to actually plot the date data, as suggested in my solution.
참고 항목
카테고리
Help Center 및 File Exchange에서 Annotations에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!