필터 지우기
필터 지우기

How to label dates on X-axis as string on monthly time step?

조회 수: 3 (최근 30일)
Hydro
Hydro 2017년 10월 31일
답변: Nurhaida Nurhaida 2019년 11월 1일
Hello, I would like to see dates on axis as a string in a month-year format with jumping steps for figure enhancement. Here is my code so far but when I tried to execute it, it generates year1 with steps 1,2 3 (see the plot).
Date=unique(cellstr(datestr((datenum('1981-01-01', 'yyyy-mm-dd'):datenum('2010-12-31', 'yyyy-mm-dd'))', 'yyyy-mm')));
figure();
hold on
fill([(1:360) fliplr(1:360)],[Gauge4(:,3)' fliplr(Gauge4(:,2)')],'c','FaceColor',[0.83 0.82 0.78],'EdgeColor',[0.83 0.82 0.78]);
plot(Gauge4(:,1),'-r','LineWidth',1.25);
plot(Gauge4(:,4),'--b','LineWidth',1.25);
axis tight
set(gca,'XTick',1:36:360,'XTickLabel',Date);

답변 (2개)

KL
KL 2017년 11월 1일
You already have year and month on the xaxis of the attached image but anyway, you could directly plot with the datetime on xaxis,
plot(Date,guage(:,3))
datetick('x','yyyy-mm')
  댓글 수: 1
Hydro
Hydro 2017년 11월 2일
Hello KL, Yes, the dates shown are month year however, its not what i want. I have 30 years data on monthly time step (1981-2010) equalling to 360 data points. In my code above, i am providing 36 months spacing for each tick- so the first tick should have x-axis value of Jan-1981, the second Jan 1984, the third Jan 1987 and so on. In total i would get 360/36=10 ticks with its corresponding date on the x-axis. Hope this is clear now? I also tried your code, it didnt work. Thanks for looking at the code though.

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


Nurhaida Nurhaida
Nurhaida Nurhaida 2019년 11월 1일
Hi..KL..try this
set(gca,'XTick',1:36:360,'XTickLabel',Date(1:36:360));
Nur

카테고리

Help CenterFile Exchange에서 Line Plots에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by