필터 지우기
필터 지우기

how to set xticks and xline

조회 수: 8 (최근 30일)
OMAR MOUSA
OMAR MOUSA 2023년 6월 29일
댓글: Rik 2023년 6월 29일
I would like to set xticks and and xline in the figure but seems diffcult to set them
I have a data with 1 second every sample for 6 days so the number of samples is 6 days*24 hours*60 minutes*60 seconds = 518400
how to make x ticks at every 12 hours and xline every 24 hours? I use this code below to plot the figure
[minA,maxA] = bounds(Pc5); % The maximum and minimum values of aggregate data
plot(t,Pc1,'r',t,Pc2,'g',t,Pc3,'m',t,Pc4,'b',t,Pc5,'y',t,Pc6,'c',t,Pc7,'k',t,Pmain,'k');
xticks(linspace(43200,475200,6))
xticklabels({'Monday','Tuesday','Wendsday','Thursday','Friday', 'Saturday'}) % Label xtick at every 12 hours
xlim([-10000 518400])
ylim([minA,maxA+50])
ylabel('Active power (W)')
title('Weekly Patterns')
x=linspace(86400,475200,5); % Draw a vertical dashed line for each day
xline(x,'--k')
grid, grid minor
and show this figure which is not prefect for xticks and xline
  댓글 수: 1
Rik
Rik 2023년 6월 29일
Your problems seem to be originating from the way you define the x-postitions in your plot.
How did you determine the cause is with xticklabels and xline?
Determining the days based on the hardcoded sampling rates seems very fragile to me. Why not rescale the values in t to range between 0 and 6? That way you rescale your data to the days, instead of rescaling the days to fit your sampling rate.

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

채택된 답변

Steven Lord
Steven Lord 2023년 6월 29일
If you have data that is time-based, why not plot the data using a datetime array then specify the ticks and tick labels using datetime values?
T = datetime('today');
d = T:hours(6):(T + days(4));
y = 1:length(d);
plot(d, y, 'o-')
xticks(d(1:4:end))
xline(d(3:4:end), '--k')
xline(T+(hours(12):hours(18):days(4)), 'c:', 'LineWidth', 4) % Make it wider so it is more visible

추가 답변 (0개)

카테고리

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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by