0

I have a list of datetime events which have both time and date information. The list includes dates which are identical, with only time changing from one list item to the next.

I want to display a scatter plot with the dates on the X axis and the time of day on the Y axis. I can get the X axis by extracting the date data from the datetime list, like this:

x = [e.date() for e in events]

However, I can't do the same with the time data:

# Results in TypeError:
# float() argument must be a string or a number, not 'datetime.time'
y = [e.time() for e in events]

I can obtain the result I need visually by doing the following, after reading this answer on SO:

import matplotlib.dates as mdates
y = [mdates.date2num(e) % 1 for e in events]

However, this displays the labels on the Y axis as the floats that they are after the date2num conversion, from 0.0 to 1.0.

What can I do in order to display the labels as 00:00 to 23:59, hopefully with configurable tick frequency (e.g. every hour, every 4 hours). I couldn't reproduce the result in the SO link above.

Thank you.

Cirrocumulus
  • 520
  • 3
  • 15
  • Should I delete the question then? It doesn't seem that plotting on the X and Y axis behave in the same way. That's why I wrote a new question. – Cirrocumulus Sep 14 '18 at 20:39
  • How are the datetime objects constructed? Passing the .time() and .date() method outputs work fine for me. I'm guessing the problem is before the date2num conversion, as matplotlib can natively handle datetime objects. – K. W. Cooper Jan 27 '19 at 13:05

1 Answers1

-1

You can follow the procedure explained in this post

Additionally to have time on y axis:

plt.scatter(y,x)
Dadep
  • 2,796
  • 5
  • 27
  • 40
Manmeet Singh
  • 405
  • 2
  • 11