Instantly share code, notes, and snippets.

What would you like to do?
Create a histograms with time on the x-axis using arrow and matplotlib
#!/usr/bin/env python
import argparse
import arrow
from matplotlib import pyplot as plt
from matplotlib import dates as mdates
def get_args():
description=('Make an histogram from a list of timestamps')
parser = argparse.ArgumentParser(description=description)
parser.add_argument('file', metavar='<timestamps_file>',
help='A file with the timestamps of ')
args = parser.parse_args()
return args
def convert_time(atime):
# Convert datetime objects to Matplotlib dates.
return mdates.date2num(atime.datetime)
def main():
args = get_args()
with open(args.file) as infile:
timestamps = [arrow.get(line.strip())
for line in infile.readlines()]
start = min(timestamps).replace(hours=-1).span('hour')[0]
end = max(timestamps).replace(hours=+1).span('hour')[1]
# fixed bin size
bins = [convert_time(abin)
for abin in arrow.Arrow.range('minute', start, end)][1::30]
fig, ax = plt.subplots()
y, x, _ = ax.hist([convert_time(atime) for atime in timestamps],
# ax.set_xlim(convert_time(start), convert_time(end))
# obtain the max y-value of a histogram
print('(x_max, y_max): ({},{})'.format(x.max(), y.max()))
# Choose your xtick format string
years_fmt = mdates.DateFormatter('%Y-%m-%d %H:00')
# format the ticks
xticks = [convert_time(abin)
for abin in arrow.Arrow.range('hour', start, end)][1::6]
ax.set_title('Submission times (fixed bin size)')
ax.set_xlabel('Timestamp (bin size = 1h)')
# auto-rotate date labels on the x-axis
if __name__ == '__main__':
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment