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 Center および File Exchange2-D and 3-D Plots についてさらに検索

タグ

製品


リリース

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by