change y values in figure to names
2 次查看(过去 30 天)
显示 更早的评论
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
0 个评论
采纳的回答
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 个评论
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
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 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Dates and Time 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!