2012-06-14 18 views
6

trong các mã dưới đây, bdate và EDATE đều datetime.datetime() đối tượng:Làm thế nào tôi có thể sử dụng xaxis_date() với barh()?

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

nhưng điều này ném một AttributeError đường xuống trong dates.py._to_ordinalf():

File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/matplotlib/pyplot.py", line 1926, in barh ret = ax.barh(bottom, width, height, left, **kwargs) File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/matplotlib/axes.py", line 4774, in barh orientation='horizontal', **kwargs) File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/matplotlib/axes.py", line 4624, in bar width = self.convert_xunits(width) File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/matplotlib/artist.py", line 147, in convert_xunits return ax.xaxis.convert_units(x) File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/matplotlib/axis.py", line 1312, in convert_units ret = self.converter.convert(x, self.units, self) File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/matplotlib/dates.py", line 1125, in convert return date2num(value) File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/matplotlib/dates.py", line 260, in 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", line 189, in _to_ordinalf base = float(dt.toordinal())

AttributeError: 'datetime.timedelta' object has no attribute 'toordinal'

i nghĩ rằng nó sẽ là tuyệt vời nếu tôi chỉ có thể xô datetimes tại xaxis và có nó làm việc ra các chi tiết; không nhiều lắm. bất kỳ gợi ý nào về cách làm cho ngày tháng có hiệu quả với xaxis?

Trả lời

11

Điều gì đang xảy ra là matplotlib không thực sự sử dụng các đối tượng datetime cho âm mưu.

Ngày đầu tiên được chuyển đổi thành định dạng dấu phẩy động bên trong. Chuyển đổi không được thiết lập để xử lý timedeltas (được cho là một sự giám sát).

Về cơ bản, bạn có thể thực hiện chính xác những gì bạn muốn, bạn chỉ cần chuyển đổi ngày tháng thành định dạng nội bộ của matplotlib trước tiên, sau đó gọi ax.xaxis_date().

Như một ví dụ nhanh (Hầu hết những điều này đang tạo dữ liệu để vẽ ...):

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() 

enter image description here

Các vấn đề liên quan