how to set xticks and xline

8 次查看(过去 30 天)
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