8

在下面的代码中, bdate 和 edate 都是 datetime.datetime() 对象:

pylab.barh(ypos, edate - bdate, left=bdate, height=TRMWidth )

但这会在 dates.py._to_ordinalf() 中抛出一个 AttributeError 方式:

文件“/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/matplotlib/pyplot.py”,第 1926 行,barh ret = ax.barh(bottom, width, height, left , **kwargs) 文件“/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/matplotlib/axes.py”,第 4774 行,barhorientation='horizo​​ntal',** kwargs) 文件“/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/matplotlib/axes.py”,第 4624 行,条形宽度 = self.convert_xunits(width) 文件“/ Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/matplotlib/artist.py”,第 147 行,在 convert_xunits 返回 ax.xaxis.convert_units(x) 文件“/Library/Frameworks/ Python.framework/Versions/2.7/lib/python2.7/site-packages/matplotlib/axis。py”,第 1312 行,在 convert_units ret = self.converter.convert(x, self.units, self) 文件“/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/matplotlib /dates.py”,第 1125 行,在转换中返回 date2num(value) 文件“/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/matplotlib/dates.py”,第 260 行, 在 date2num 中 else: return np.asarray([_to_ordinalf(val) for val in d]) File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/matplotlib/dates. py",第 189 行,在 _to_ordinalf base = float(dt.toordinal())py”,第 1125 行,在转换中返回 date2num(value) 文件“/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/matplotlib/dates.py”,第 260 行,在 date2num否则:返回 np.asarray([_to_ordinalf(val) for val in d]) 文件“/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/matplotlib/dates.py”,第 189 行,在 _to_ordinalf base = float(dt.toordinal())py”,第 1125 行,在转换中返回 date2num(value) 文件“/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/matplotlib/dates.py”,第 260 行,在 date2num否则:返回 np.asarray([_to_ordinalf(val) for val in d]) 文件“/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/matplotlib/dates.py”,第 189 行,在 _to_ordinalf base = float(dt.toordinal())在 _to_ordinalf 基础 = 浮动(dt.toordinal())在 _to_ordinalf 基础 = 浮动(dt.toordinal())

AttributeError:“datetime.timedelta”对象没有属性“toordinal”

我认为如果我可以将日期时间推到 xaxis 并让它计算出细节,那就太好了;没那么多。关于如何使日期适合 xaxis 的任何建议?

4

1 回答 1

16

发生的事情是 matplotlib 实际上并没有使用 datetime 对象进行绘图。

日期首先转换为内部浮点格式。转换未设置为处理时间增量(可以说是疏忽)。

你基本上可以做你想做的事,你只需要先将日期显式转换为matplotlib的内部格式,然后调用ax.xaxis_date().

作为一个简单的例子(其中大部分是生成要绘制的数据......):

import datetime as dt
import matplotlib.pyplot as plt
import matplotlib.dates as mdates

def drange(start, end, interval=dt.timedelta(days=1)):
    output = []
    while start <= end:
        output.append(start)
        start += interval
    return output

# Generate a series of dates for plotting...
edate = drange(dt.datetime(2012, 2, 1), dt.datetime(2012, 6, 15), 
                      dt.timedelta(days=5))
bdate = drange(dt.datetime(2012, 1, 1), dt.datetime(2012, 5, 15), 
                      dt.timedelta(days=5))

# Now convert them to matplotlib's internal format...
edate, bdate = [mdates.date2num(item) for item in (edate, bdate)]

ypos = range(len(edate))
fig, ax = plt.subplots()

# Plot the data
ax.barh(ypos, edate - bdate, left=bdate, height=0.8, align='center')
ax.axis('tight')

# We need to tell matplotlib that these are dates...
ax.xaxis_date()

plt.show()

在此处输入图像描述

于 2012-06-15T19:19:46.513 回答