Plotting an array of dates on the x-axis
조회 수: 4 (최근 30일)
이전 댓글 표시
I am am writting a program where I read data from a txt file that has dates that correspond to different data sets. This program has to be reusable for other txt files so for getting my plot with dates on the x-axis I create a cell that fills with all the dates it reads caled dates.
'31-May-2019 11:37:29'
'31-May-2019 11:45:25'
'22-Jul-2019 18:05:55'
'22-Jul-2019 18:06:31'
'24-Jul-2019 18:00:07'
'24-Jul-2019 18:00:30'
For example this. How do I get these dates on the x-axis from the cell containing them.
Thanks
댓글 수: 0
답변 (1개)
Steven Lord
2020년 7월 21일
Use datetime.
x = {'31-May-2019 11:37:29'
'31-May-2019 11:45:25'
'22-Jul-2019 18:05:55'
'22-Jul-2019 18:06:31'
'24-Jul-2019 18:00:07'
'24-Jul-2019 18:00:30'}
dt = datetime(x)
plot(dt, (1:6).^2)
댓글 수: 3
Steven Lord
2020년 7월 21일
When I ran the code I posted, the X axes ticks included the date information from the datetime array x.
Do you want 108 ticks, each with the full date and time information? That seems like the plot will be quite crowded. It is possible to do, but I'm not certain that's what you really want.
% Make sample data
x = datetime('now')+minutes(0:107);
% Plot the sample data
h = plot(x, 1:108);
% Set the ticks -- notice they overlap quite a bit
xticks(x);
% Let's rotate the ticks. We need the handle to the axes to do this.
ax = ancestor(h, 'axes');
ax.XTickLabelRotation = 90;
% Note that this plots the times but not the date in each label
% (except maybe if you're running this close enough to midnight.)
% If you want the date in each and every label:
xticklabels(string(x))
If this isn't what you're trying to do, can you provide a bit more information about how you're trying to make the axes appear? If you have a picture that shows what you want, a link to that would be useful.
참고 항목
카테고리
Help Center 및 File Exchange에서 Annotations에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!