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:
but I want it to be like this:
The x-axis showing the year.
Thank you in advance!

 Accepted Answer

the cyclist
the cyclist on 14 Jan 2021
Edited: the cyclist on 14 Jan 2021
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 Comments

it is a gridded data of precipitation from 1982-2017.
The dimension is 432x26x54. Where it is precipittaion (time)xlatxlon. Then I changed it into 2d. So, it became 432x877.
I tried the xtickslabel, but its not functioning for my data.
Can you show us the command you used to plot the picture in your question? I believe the cyclist is correct, that you called plot with one input instead of two.
I calculate spi index from a region with dimension 444x26x54. And then, the results is spi_3=432x877.
Then, I calculate the average of the region.
spi3=mean(spi_3,2)
it came out as;
spi3=432x1
and then I just plot:
plot(spi3)
Your plotting command, using one input variable, is short-hand notation for this command:
plot(1:432,spi3) % because spi3 is length 432
Instead, you should use
plot(date_date,spi3) % use date_data as x data
where date_data is a length-432 vector of dates corresponding to your y data
i tried to use this but it wont works :(
>>from_datenum = datenum(1982,1,31,0,0,0);
>> to_datenum = datenum(2017,12,31,0,0,0);
>> idxDatenum = find(from_datenum <= tDatenum & tDatenum <= to_datenum);
>> myPrepDatenum = tDatenum(idxDatenum);
>> plot(myPrepDatenum,SPI3)
Can you be more specific about what doesn't work? Do you get an error, or does it just not look as you want? If an error, what is the error message?
Is myPrepDatenum a vector of length 432? I can't tell from this code, but I am guessing not.
Again, are you able to just upload your data and your code here?
Hi sorry. This is my data. I just want to plot it and get as the second picture above.
The time should be from 1982-2017
should I use this?
t1 = datetime(1982,01,01);
t2 = datetime(2017,12,31);
t = t1:t2
but it is not 432x1
is this okkk?
t1 = datetime(1982,01,01);
t2 = datetime(2017,12,31);
date=t1:calmonths(1):t2;
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.
YES! Thank you very much sir!

Sign in to comment.

More Answers (1)

WalterWhite
WalterWhite on 14 Jan 2021
xticks(1980:5:2020)

5 Comments

I think you mean xticklabels.
i just set the range of the x-axis values between 1980-2020. I didn't label them. I was of the impression that the labels will be generated automatically in that range by matlab
xticks(1980:5:2020) %try pasing this code under your plot(spi3)
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
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
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
But I think it is fundamentally better to actually plot the date data, as suggested in my solution.

Sign in to comment.

Tags

Community Treasure Hunt

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

Start Hunting!