Plotting an array of dates on the x-axis

2 visualizaciones (últimos 30 días)
Austin Matuszewski
Austin Matuszewski el 21 de Jul. de 2020
Comentada: Austin Matuszewski el 22 de Jul. de 2020
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

Respuestas (1)

Steven Lord
Steven Lord el 21 de Jul. de 2020
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 comentarios
Steven Lord
Steven Lord el 21 de Jul. de 2020
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.
Austin Matuszewski
Austin Matuszewski el 22 de Jul. de 2020
so this is plotting dates with respect of time of eachother. The spacing should reflect how many indeces they are apart but not in time they are apart. It is plotting pointis at given times rather than given indexes.

Iniciar sesión para comentar.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by