Skip to content
{{ message }}

Instantly share code, notes, and snippets.

# CristianCantoro/histo_timestamps.py

Last active Mar 13, 2018
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='', help='A file with the timestamps of ') args = parser.parse_args() return args def convert_time(atime): # Convert datetime objects to Matplotlib dates. # https://matplotlib.org/api/dates_api.html 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], bins=bins) # ax.set_xlim(convert_time(start), convert_time(end)) # obtain the max y-value of a histogram # https://stackoverflow.com/questions/15558136 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 ax.xaxis.set_major_formatter(years_fmt) xticks = [convert_time(abin) for abin in arrow.Arrow.range('hour', start, end)][1::6] ax.set_xticks(xticks) ax.set_title('Submission times (fixed bin size)') ax.set_xlabel('Timestamp (bin size = 1h)') ax.set_ylabel('Count') # auto-rotate date labels on the x-axis fig.autofmt_xdate() plt.show() if __name__ == '__main__': main()
to join this conversation on GitHub. Already have an account? Sign in to comment