gpt4 book ai didi

python - 我如何将 xaxis_date() 与 barh() 一起使用?

转载 作者:太空狗 更新时间:2023-10-29 21:24:52 24 4
gpt4 key购买 nike

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

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

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

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'

我认为如果我可以在 xaxis 上推送日期时间并拥有它会很棒制定细节;没那么多。关于如何使日期适合 xaxis 有什么建议吗?

最佳答案

实际情况是 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()

enter image description here

关于python - 我如何将 xaxis_date() 与 barh() 一起使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11042290/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com