I'd like to plot pandas DataFrame as a line chart with x-axis having '%H:%M' style.
However, when I try this:
import datetime
import pandas as pd
import numpy as np
d = pd.DataFrame([np.random.rand(3) for _ in range(9)])
d.index = [datetime.time(i, 0) for i in range(15,24)]
ax = d.plot(xticks=d.index)
plt.xticks([x.strftime("%H:%M") for x in d.index][0::4])
Chart with %H:%M:%s style x-axis is ploted:
Following this answer, I've added DateFormatter
:
import matplotlib.dates as md
d = pd.DataFrame([np.random.rand(3) for _ in range(9)])
d.index = [datetime.time(i, 0) for i in range(15,24)]
ax = d.plot(xticks=d.index)
#plt.xticks([x.strftime("%H:%M") for x in d.index][0::4])
ax.xaxis.set_major_formatter(md.DateFormatter('%H:%M'))
Unfortunately, I got a plot with only 00:00 x-axis.
I'd like to get %H:%M style x-axis with proper interval ticks label like "15:00, 17:00, 19:00, 21:00" (every 2 intervals here). How do I realize that?