使用hist()函数无法对齐日期时间对象直方图中的bin [英] Unable to align bins in a histogram of datetime objects using the hist() function

查看:117
本文介绍了使用hist()函数无法对齐日期时间对象直方图中的bin的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我试图绘制一些日期时间对象的直方图,但是我永远无法使垃圾箱与条形对齐。我的代码如下:

So I am trying to plot a histogram of some datetime objects, but i can never get the bins to line up with the bars. My code is below:

我首先导入我的东西并声明开始,结束和一天的对象:

I start by importing my stuff and declaring the start, end and a one day object:

import datetime
import matplotlib.pyplot as plt

end = datetime.date(2017,5,14) 
start = datetime.date(2017,5,8) 
one_day = datetime.timedelta(days = 1)  

然后我声明一个任意日期列表:

Then I declare an arbitrary list of dates:

date_list = [datetime.date(2017,5,14), datetime.date(2017,5,14), 
datetime.date(2017,5,14), datetime.date(2017,5,9), datetime.date(2017,5,13), 
datetime.date(2017,5,12), datetime.date(2017,5,11), 
datetime.date(2017,5,11), datetime.date(2017,5,9)]

然后,我经历了从开始到结束的几天(在这种情况下是一周),将每个日期添加到列表中:

Then I go through the range of days between my start and end (in this case a week), adding each date to a list:

week = [] 
for i in range((end-start).days+1):  
    week.append(start + (i)*one_day)

r原因是date_list中没有一周中的某些日子(我知道在这种情况下可以跳过此步骤,但我希望它可以扩展到其他date_lists中)。

The reason for this is that some of the days in the week don't come up in the date_list (I know I could just skip this for this case, but I want it to be expandable to other date_lists).

然后我用hist()绘图:

Then I plot with hist():

plt.hist(date_list, bins = len(week)+1)
plt.show()

我已经尝试了将bin格式与各种+1以及范围和对齐方式进行各种组合的方式,但是日期永远不会位于栏中一致的位置。

I've tried all manner of combinations of bin formats with various +1's and ranges and aligns but the date never sit in a consistent place in the bar.

推荐答案

如果仅设置bin的数量,则bin将在(数据的)第一个和最后一个值之间平均分配。由于数据例如如果缺少开始值,则垃圾箱会小于一整天。

If simply setting the number of bins, the bins will be equally distributed between the first and last value (of the data). Since the data is e.g. missing the start value, the bins will be smaller than a complete day.

要克服这一点,需要明确地将所需的bin设置为 bins 参数>历史。不幸的是,这里不能直接使用日期时间列表,因此需要先将日期时间转换为数字。可以使用matplotlib的 matplotlib.dates.date2num 方法来完成。

To overcome this, one need to explicitely set the desired bins to the bins argument of hist. Unfortunately, one cannot directly use the list of datetimes here, so the datetimes need to be converted to numbers first. This can be done using matplotlib's matplotlib.dates.date2num method.

完整的示例:

import datetime
import matplotlib.pyplot as plt
from matplotlib.dates import date2num

end = datetime.date(2017,5,14) 
start = datetime.date(2017,5,8) 
one_day = datetime.timedelta(days = 1)  

date_list = [datetime.date(2017,5,14), datetime.date(2017,5,14), 
datetime.date(2017,5,14), datetime.date(2017,5,9), datetime.date(2017,5,13), 
datetime.date(2017,5,12), datetime.date(2017,5,11), 
datetime.date(2017,5,11), datetime.date(2017,5,9)]

week = [] 
for i in range((end-start).days+1):  
    week.append(start + (i)*one_day)

numweek = date2num(week)

plt.hist(date_list, bins = numweek, ec="k")
plt.gcf().autofmt_xdate()
plt.show()

请注意, datetime.date(2017,5,14)(2017,5,13)和<$之间的bin的一部分c $ c>(2017,5,14),因此您可能希望将结束日期设置为
datetime.date(2017,5,15)

Note that the datetime.date(2017,5,14) is part of the bin between the (2017,5,13) and (2017,5,14), so you might want to set the enddate to datetime.date(2017,5,15).

这篇关于使用hist()函数无法对齐日期时间对象直方图中的bin的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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