1

I have a list of datetime.date and a list of datetime.time values. I would like the date on the x axis and the time on the y axis.

x = [datetime.date(2018, 1, 15), datetime.date(2018, 1, 16), datetime.date(2018, 1, 17)]
y = [datetime.time(18, 15), datetime.time(17, 15), datetime.time(17, 47)]

When I try to plot the time on the y axis and the date on the x I get a numpy error:

"TypeError: float() argument must be a string or a number"

To get around it, I converted both values into datetimes, adding an arbitrary date to the time and a time of zero to the date:

x_new = [datetime.datetime.combine(d, datetime.datetime.min.time()) for d in x]
y_new = [datetime.datetime.combine(datetime.datetime.now().date(), t) for t in y]

That's fine, but when I plot this my y axis shows no minutes or seconds and instead has the format 'dd HH:MM' e.g: '01 17:16' if I plot today. If I zoom in I can see the minutes and seconds, but that's no good. I want only hour, min, second to show, no date.

I hoped that using the plot_date option rather than just plot, I might find what I need, but that hasn't helped me.

I hope my question makes sense. Any help would be much appreciated. I saw an example where someone had their times formatted as strings, but I had some issues with that (another story), and I'd rather use datetimes really if possible.

Thanks!

Hamo
  • 99
  • 2
  • 11
  • Can't reproduce; works like a charm. `plt.plot(x,y)` Can you provide information about your versions? – Uvar Mar 01 '18 at 12:00
  • Hi. I will make a slight correction to the above that in fact if I plot the exact data above, I get the y axis format 'dd HH:MM' (full dataset is longer). So the above gives day, hour minute, and no seconds. Sorry. Nevertheless, it is still not right. You get just HH:MM:SS? It's python 2.7.12, matplotlib version '2.1.1', numpy version '1.13.3'. thanks! – Hamo Mar 01 '18 at 12:18
  • So, the only tip I can give you is to switch to Python 3. ^^ you'll have to sooner or later anyhow. You can still try looking at some of the answers on SO about custom tick string formatting, but it's going to be a tough ride even if it is ultimately possible. – Uvar Mar 01 '18 at 12:33
  • 1
    Take a look at this (not a duplicate, but helpful) - https://stackoverflow.com/questions/35663705/how-to-plot-time-on-y-axis-in-hm-format-in-matplotlib – Udayraj Deshmukh Mar 01 '18 at 12:42
  • @UdayrajDeshmukh Thankyou! By adding the line ax.yaxis.set_major_formatter(DateFormatter('%H:%M')) I am able to control how it initially shows the axis ticks using that format string. I can do seconds if I want too. Thanks to both of you. This has solved it. – Hamo Mar 01 '18 at 12:59
  • Feel free to answer the question yourself. Stackoverflow promotes that by giving you a badge :) Just reframe your question in a better, understandable way. If you haven't looked yet, [check this](https://stackoverflow.com/help/mcve) – Udayraj Deshmukh Mar 01 '18 at 13:01
  • Nice. I was actually expecting the simple call to `set_major_formatter` to fail and was thinking along other routes.. :$ should just have given it a try – Uvar Mar 01 '18 at 13:02

0 Answers0