我认为您对matplotlib究竟如何在后台处理时间和日期感到有些困惑。
matplotlib中的所有日期时间都表示为简单浮点数。1天对应于1.0的差,日期是自1900年以来的天数(如果我没有记错的话,无论如何)。
因此,为了只绘制给定日期的时间,您需要使用
% 1。
我将使用点,但是您可以轻松使用条形图。考虑使用
bottom,如果你使用关键字参数
plt.bar,使杆的底部将在您的时间间隔的开始时间开始(请记住第二个参数是
高度 了吧,不是其顶部的y值)。
例如:
import matplotlib.pyplot as pltimport matplotlib as mplimport numpy as npimport datetime as dt# Make a series of events 1 day apartx = mpl.dates.drange(dt.datetime(2009,10,1),dt.datetime(2010,1,15),dt.timedelta(days=1))# Vary the datetimes so that they occur at random times# Remember, 1.0 is equivalent to 1 day in this case...x += np.random.random(x.size)# We can extract the time by using a modulo 1, and adding an arbitrary base datetimes = x % 1 + int(x[0]) # (The int is so the y-axis starts at midnight...)# I'm just plotting points here, but you could just as easily use a bar.fig = plt.figure()ax = fig.add_subplot(111)ax.plot_date(x, times, 'ro')ax.yaxis_date()fig.autofmt_xdate()plt.show()
希望能有所帮助!



