简体   繁体   English

在PyPlot中绘制纪元列表的直方图,x轴按月 - 年

[英]Plot histogram of epoch list, x axis by month-year in PyPlot

With a list of epoch dates, is there a parameter in pyplot or numpy to have an histogram where the bins match the months in the data list? 有了一个纪元日期列表, pyplotnumpy是否有一个参数可以得到一个直方图,其中binsdata列表中的月份相匹配? In this example, the list correspond to random date from 2012 to 2013. I would like that the histogram shows the bars from, for example, February 2012 to October 2013 if the values in data correspond only to dates from these months. 在此示例中,列表对应于2012年至2013年的随机日期。如果data的值仅对应于这些月份的日期,我希望直方图显示2012年2月至2013年10月的条形图。

This code makes an histogram, but it separates manually for bins=24 . 此代码生成直方图,但它为bins=24手动分隔。

import numpy as np
import matplotlib.mlab as mlab
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
import random

data = [int(random.randint(1293836400, 1356994800)) for _ in range(1000)]

# convert the epoch format to matplotlib date format
mpl_data = mdates.epoch2num(data)

fig, ax = plt.subplots(1,1)
ax.hist(mpl_data, bins=24, ec='black')
ax.xaxis.set_major_locator(mdates.MonthLocator())
ax.xaxis.set_major_formatter(mdates.DateFormatter('%d.%m.%y'))
fig.autofmt_xdate()
plt.show()

In order to do this you have to pick out the timestamps at which the beginning of each month begins. 为此,您必须选择每月开始的时间戳。 Dates/Times are always a lot trickier than just regular numbers so while this code looks a bit cumbersome, it does work. 日期/时间总是比常规数字更棘手,所以虽然这段代码看起来有点麻烦,但确实有效。

import numpy as np
import matplotlib.mlab as mlab
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
import random

data = [int(random.randint(1293836400, 1356994800)) for _ in range(1000)]

# create your bins as timestamps marked at the beginning of each month, using datetime objects to increment
import datetime as d
mindate = d.datetime.fromtimestamp(min(data))
maxdate = d.datetime.fromtimestamp(max(data))
bindate = d.datetime(year=mindate.year, month=mindate.month, day=1)
bins = [bindate.timestamp()]
while bindate < maxdate:
    if bindate.month == 12:
        bindate = d.datetime(year=bindate.year + 1, month=1, day=1)
    else:
        bindate = d.datetime(year=bindate.year, month=bindate.month + 1, day=1)
    bins.append(bindate.timestamp())
bins = mdates.epoch2num(bins)

mpl_data = mdates.epoch2num(data)
fig, ax = plt.subplots(1,1, figsize=(16, 4), facecolor='white')
ax.hist(mpl_data, bins=bins, ec='black')
ax.xaxis.set_major_locator(mdates.MonthLocator())
ax.xaxis.set_major_formatter(mdates.DateFormatter('%d.%m.%y'))
fig.autofmt_xdate()

在此输入图像描述

Another approach is to use pandas to group data by month, and then counting them. 另一种方法是使用pandas按月对数据进行分组,然后对它们进行计数。 The code is much shorter, and you can make a quick bar plot. 代码要短得多,你可以制作一个快速的条形图。 To re-create your plot above would take more work, but this gives you a feel for things you can do with other tools: 要重新创建上面的图表需要花费更多的工作,但这可以让您感受到使用其他工具可以做的事情:

srs = pd.DatetimeIndex(pd.Series(data) * 1e9)  # convert sec to nsec
df = pd.DataFrame({'count': np.ones(shape=len(srs))}, index=srs)
fig, ax = plt.subplots(1, 1, figsize=(16,4), facecolor='white')
df.groupby(pd.Grouper(freq='M')).count().plot.bar(ax=ax)

在此输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM