Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
616 views
in Technique[技术] by (71.8m points)

python - How can i use xaxis_date() with barh()?

in the code below, bdate and edate are both datetime.datetime() objects:

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

but this throws an AttributeError way down in 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 thought it'd be great if i could just shove datetimes at xaxis and have it work out the details; not so much. any suggestions as to how to make dates agreaable to xaxis?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

What's happening is that matplotlib doesn't actually use datetime objects for plotting.

Dates are first converted into an internal floating point format. The conversion isn't set up to handle timedeltas (which is arguably an oversight).

You can basically do exactly what you wanted, you just need to explictly convert the dates to matplotlib's internal format first, and then call ax.xaxis_date().

As a quick example (Most of this is generating data to plot...):

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...