change y values in figure to names

조회 수: 2 (최근 30일)
HG-NU
HG-NU 2020년 5월 6일
댓글: Kelly Kearney 2020년 5월 7일
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

채택된 답변

Kelly Kearney
Kelly Kearney 2020년 5월 7일
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
HG-NU
HG-NU 2020년 5월 7일
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?
Kelly Kearney
Kelly Kearney 2020년 5월 7일
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:

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

추가 답변 (1개)

BN
BN 2020년 5월 7일
편집: BN 2020년 5월 7일
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
HG-NU
HG-NU 2020년 5월 7일
This is the result of X axis :
Unfortunately, it does not solve the issue.
BN
BN 2020년 5월 7일
편집: BN 2020년 5월 7일
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 "[]"

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

카테고리

Help CenterFile Exchange에서 Dates and Time에 대해 자세히 알아보기

태그

제품


릴리스

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by