change y values in figure to names

2 vues (au cours des 30 derniers jours)
HG-NU
HG-NU le 6 Mai 2020
Commenté : Kelly Kearney le 7 Mai 2020
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

Réponse acceptée

Kelly Kearney
Kelly Kearney le 7 Mai 2020
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 commentaires
HG-NU
HG-NU le 7 Mai 2020
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 le 7 Mai 2020
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:

Connectez-vous pour commenter.

Plus de réponses (1)

BN
BN le 7 Mai 2020
Modifié(e) : BN le 7 Mai 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 commentaires
HG-NU
HG-NU le 7 Mai 2020
This is the result of X axis :
Unfortunately, it does not solve the issue.
BN
BN le 7 Mai 2020
Modifié(e) : BN le 7 Mai 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 "[]"

Connectez-vous pour commenter.

Catégories

En savoir plus sur 2-D and 3-D Plots dans Help Center et File Exchange

Tags

Produits


Version

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by