5

i would like to plot the times from a datetime64 series, where the y-axis is formatted as '%H:%M, showing only 00:00, 01:00, 02:00, etc.

this is what the plot looks like without customizing the y-axis formatting.

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from matplotlib.dates import DateFormatter
from matplotlib.dates import HourLocator

df = pd.DataFrame(data=dict(a=pd.date_range('1/1/2011',periods=1440000,freq='1min')))
df = df.iloc[np.arange(0,1440*100,1440)+np.random.randint(1,300,100)]

plt.plot(df.index,df['a'].dt.time)
plt.show()

enter image description here

After reading around on the topic on SO, I attempted the following but without success.

ax = plt.subplot()
ax.yaxis.set_major_locator(HourLocator())
ax.yaxis.set_major_formatter(DateFormatter('%H:%M'))
plt.plot(df.index,df['a'].dt.time)
plt.show()

ValueError: DateFormatter found a value of x=0, which is an illegal date.  This usually occurs because you have not informed the axis that it is plotting dates, e.g., with ax.xaxis_date()

Could anyone advise me?

themachinist
  • 1,413
  • 2
  • 17
  • 22

2 Answers2

8

For that to work you need to pass datetime objects (and I mean datetime, not datetime64). You can convert all timestamps to the same date and then use .tolist() to get the actual datetime objects.

y = df['a'].apply(lambda x: x.replace(year=1967, month=6, day=25)).tolist()
ax = plt.subplot()
ax.plot(df.index, y)
ax.yaxis.set_major_locator(HourLocator())
ax.yaxis.set_major_formatter(DateFormatter('%H:%M'))

enter image description here

Stop harming Monica
  • 12,141
  • 1
  • 36
  • 56
  • Importing pandas implicitly installs converters to deal with datetime64 correctly (by laundering them through the pandas dt64 boxed datatype). – tacaswell Feb 27 '16 at 22:02
  • thanks @tcaswell, but pandas (0.17.0) was imported in my example above. any suggestions on why it didnt work? – themachinist Feb 27 '16 at 22:13
  • @tcaswell Not sure what you mean, I had to add the `.tolist()` call to make `HourLocator` work. I noticed that when plotting `.df.time` matplotlib used pandas stuff for ticks and labels, but I did not find an easy way to customize them to met the requirements. – Stop harming Monica Feb 27 '16 at 23:16
0

You can try two things: 1) It should be ax.xaxis.... not ax.yaxis.... 2) Use set_major_locator() instead of set_major_formatter() for Locator. Example is shown below.

min = 15
ax.xaxis.set_major_locator(MinuteLocator(byminute=range(0,60,min)) )
ax.xaxis.set_major_formatter( DateFormatter('%H:%M') )
Hun
  • 3,707
  • 2
  • 15
  • 15