0

I have a list of date and time values with the format '2019-08-24 08:57:18.550' for example. I have successfully converted them into numbers that matplotlib understands using datetime with the code matplotlib.dates.date2num(points) however I am having trouble getting matplotlib to plot only the time, not the associated date. The graph it creates has tick marks with labels such as 08-24 12 which I assume has the format "month-date hour". I would like it to only plot the time, ideally with the format "hour:minute" or something along those lines. How do I get matplotlib to do this?

E0440
  • 1
  • 1
  • Feed in the dates as your have them, but create a custom date formatter for the axis: https://stackoverflow.com/a/33746112/1552748 – Paul H May 18 '20 at 21:57

1 Answers1

0

If I understood correctly, and it is the current date/time that you are looking for, then:

>>> from datetime import datetime
>>> current_time = datetime.now()
>>> current_time
datetime.datetime(2020, 5, 18, 22, 4, 41, 425538)

#################(year, month, day, hour, minute, second, microsecond)

You can then format it (this is what I didn't quite understand what you were asking), but if you wanted hour:minute format then:

from datetime import datetime

time = datetime.now()

hour = time.hour
minute = time.minute

print(f"{hour}:{minute}")

You should note that datetime.datetime(2020, 5, 18, 22, 4, 41, 425538) was not iterable.

Emma H
  • 68
  • 7
  • How would I get matplotlib to label the tick marks on the graph using that format? The way it is labeling points on the graph is `08-24 12`, but I don't want the date to show up in the labels. So if the time of a given point was 12:10 (so hour is 12, minute is 10) then I want the label on the graph to be `12:10`, without the date. Sorry if the post was confusing. – E0440 May 18 '20 at 21:26