change y values in figure to names

I was able to plot temperatures of each hour in the year and got this result using this command:
plot(A(1:8760,5)')
X axis is the hours of the year
Y axis is the temperature vaule of each day
Question:
How can I change the crosses values in X axis to be the months names rather than hours?
i.e.
January instead of 1000
Febraruy instead of 2000
and so on

 Accepted Answer

You need to calculate where each month falls, in terms of hours:
ttk = datetime(2019,1:13,1);
set(gca, 'xtick', hours(ttk-datetime(2019,1,1)), 'xticklabel', cellstr(datestr(ttk,'mmm')))
Check the specific year of your data; the alignment will be different in leap years vs non-leap years.
Alternatively, if you actually specify datetime values for your x-values, Matlab will create a date axis that automatically labels things appropriately:
t = datetime(2019,1,1) + hours(1:8760);
plot(t, A(1:8760,5)');

4 Comments

Thanks @kelly. It works fine.
Is there any way to center the month's name to be on such day number 15?
Because each month is currently represented at hour 1 on day 1 of the month.
If you used option 2 (i.e. plotting with datetimes on the x-axis), you just need to change the location of the x-ticks accordingly:
set(gca, 'xtick', datetime(2019,1:12,15))
Unfortunately, the first option
ttk = datetime(2019,1:13,1);
set(gca, 'xtick', hours(ttk-datetime(2019,1,1)), 'xticklabel', cellstr(datestr(ttk,'mmm')))
not work. and this is the result
If I use the first option, then I cannot see how this option will read the data of temperatures from my table?
I suggest you take some time to really understand the commands you are using. I gave you a few different suggestions for how to modify the plotted axis, but you need to understand how Matlab is using your data to create a plot.
In your original example, you plotted the data without providing any x data. When you do this, Matlab automatically assigns the x-data to be integer values. In your case, that means the x-axis can be interpreted as hours relative to some reference start time, and you can assign your tick marks accordingly:
A = -cos(linspace(0,2.*pi,8760)) + randn(1,8760); % pseudo-data
% Option 1
subplot(2,1,1);
plot(A); % same as plot(1:8760,A)
ttk = datetime(2019,1:12,15); % desired tick locations, as datetimes
tklbl = cellstr(datestr(ttk, 'mmm')); % labels you want to match these ticks
xtk = hours(ttk - datetime(2019,1,1)); % tick locations in hours
set(gca, 'xtick', xtk, 'xticklabel', tklbl);
A second option would be to pass the plot some datetime values as x-data, so that Matlab will add a date axis to the plot and take care of formatting the tick labels.
subplot(2,1,2);
t = datetime(2019,1,1) + hours(1:8760);
plot(t, A);
set(gca, 'xtick', ttk);
Both of these options will get you the desired ticks:

Sign in to comment.

More Answers (1)

BN
BN on 7 May 2020
Edited: BN on 7 May 2020
Hello
In your question you said y but it seems you mean x; anyways,
Try this:
names = {'January'; 'February'; 'March'; 'April'; 'May'; 'June'; 'July'; 'August'; 'September';...
'October'; 'November'; 'December'};
set(gca,'xtick',[1:12],'xticklabel',names)
But:
You have 10 month ?
So you end in october? so this is:
names = {'January'; 'February'; 'March'; 'April'; 'May'; 'June'; 'July'; 'August'; 'September';...
'October'};
set(gca,'xtick',[1:12],'xticklabel',names)
I hope it helps you.

2 Comments

This is the result of X axis :
Unfortunately, it does not solve the issue.
BN
BN on 7 May 2020
Edited: BN on 7 May 2020
Try this:
set(gca,'xtick',1:10,...
'xticklabel',{'January'; 'February'; 'March'; 'April'; 'May'; 'June'; 'July';...
'August'; 'September'; 'October'})
% if you have 12 months change 10 to 12 and also type name of othee months
I think previous code not worked because of "[]"

Sign in to comment.

Categories

Products

Release

R2020a

Tags

Community Treasure Hunt

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

Start Hunting!