我如何使用xaxis_date()与barh()? [英] How can i use xaxis_date() with barh()?

查看:1684
本文介绍了我如何使用xaxis_date()与barh()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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


文件/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 ,第4774行,barh
orientation ='horizo​​ntal',** kwargs)
文件// lib / python2.7 / site-packages / matplotlib / axes.py图书馆/框架/ Python.framework /版本/ 2.7 / lib / python2.7 / site-packages / matplotlib / axes.py,第4624行,条
width = 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行,转换
return date2num(value)
文件/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/ site-packages / matplotlib / dates.py,第260行,date2num
else:返回np.asarray(d中的val的_ ___ordinalf(val)]
文件/ Library / Frameworks / Python .framework / Versions / 2.7 / lib / python2.7 / site-packages / matplotlib / dates.py,第189行,_to_ordinalf
base = float(dt.toordinal())



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


我以为会如果我可以只是shov是伟大的e datetimes在xaxis,并有
制定细节;没那么多。关于如何使日期变得可以适应xaxis的任何建议?

解决方案

发生的情况是,matplotlib实际上并没有使用datetime对象



首先将日期转换为内部浮点格式。转换没有设置来处理timedeltas(这可以说是一个监督)。



你基本上可以做你想要的,你只需要明确地转换日期到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

#生成一系列绘图日期...
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))

#现在将它们转换为matplotlib的内部格式...
edate,bdate = [mdates.date2num(item)for(in edate,bdate)]

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

#绘制数据
ax.barh(ypos,edate - bdate,left = bdate,height = 0.8 ,align ='center')
ax.axis('tight')

#我们需要告诉matplotlib这些是日期...
ax.xaxis_date()

plt.show()


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?

解决方案

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

这篇关于我如何使用xaxis_date()与barh()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆