How to manually change plot x-axis to months

I have been trying to manually change the xtick label to show months instead of the array that i'm plotting, however it only shows a portion of the string and I can't get to show the months from Jan-Dec.
month = ["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"];
h(6) = figure;
plot(Day1Sum)
hold on
plot (Day2Sum,'r')
% xlim ([1 hrsyr])
legend ('Load', 'Load with DR')
ylabel ('Load (MWh)')
xticklabels(month)

3 comentarios

KSSV
KSSV el 13 de Ag. de 2018
what is size of your Day1Sum ?
hesham fageeh
hesham fageeh el 13 de Ag. de 2018
Editada: hesham fageeh el 13 de Ag. de 2018
(1,8760) , it's the number of hrs in one year.
xlim([1 12])
xticks(1:12);
xticklabels(month)
Because you have 12 labels, you need 12 xticks, which probably would not have happened automatically.

Iniciar sesión para comentar.

 Respuesta aceptada

jonas
jonas el 13 de Ag. de 2018
You need to pass a cell array to xticklabels instead of a string. You build a cell array with {} instead of []. For example
month = {"Jan","Feb","Mar",...}
Second, it is better to work with datetime format, as manipulating your ticklabels makes for buggy code. Will gladly show you how, if you provide your x-data.

3 comentarios

hesham fageeh
hesham fageeh el 13 de Ag. de 2018
I tried passing a cell array it still shows the same problem. Sure, please find the x-data attached.
jonas
jonas el 13 de Ag. de 2018
Editada: jonas el 13 de Ag. de 2018
Here's one way to do it with datetime.
data=load('Day1Sum.mat')
Day1Sum=data.Day1Sum;
t=datetime(0,1,1,1:8760,0,0)
plot(t,Day1Sum)
set(gca,'xtick',linspace(t(1),t(end),12))
xtickformat('MMM')
...and here is with the other approach
data=load('Day1Sum.mat')
Day1Sum=data.Day1Sum;
t=1:8760;
plot(Day1Sum)
set(gca,'xtick',linspace(t(1),t(end),12))
month = {'Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'};
xticklabels(month)
The number of ticklabels must be equal to the number of ticks
hesham fageeh
hesham fageeh el 13 de Ag. de 2018
Both work great. Thanks!

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Etiquetas

Preguntada:

el 13 de Ag. de 2018

Comentada:

el 13 de Ag. de 2018

Community Treasure Hunt

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

Start Hunting!

Translated by