Skip to content

Instantly share code, notes, and snippets.

@kotnik
Created May 4, 2016 10:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kotnik/c8983a9083f53b8dc57160a45bfd204d to your computer and use it in GitHub Desktop.
Save kotnik/c8983a9083f53b8dc57160a45bfd204d to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
import sys
import datetime
import subprocess
from collections import Counter
if __name__ == '__main__':
hours = {}
for line in sys.stdin:
in_time, in_code = line.strip().split(' ')
in_time = datetime.datetime.strptime(in_time, '%d/%b/%Y:%H:%M:%S')
in_hour = in_time.replace(minute=0, second=0, microsecond=0)
hours.setdefault(in_hour, Counter())
hours[in_hour][in_code] += 1
flat_codes = Counter()
for codes in hours.values():
flat_codes.update(codes)
all_codes = flat_codes.keys()
print("time\t" + "\t".join(all_codes))
codes_dat = ""
for hour, codes in sorted(hours.items()):
str_codes = ""
for code in all_codes:
str_codes += "\t%s" % codes[code]
codes_dat += "%s%s\n" % (hour, str_codes)
print("%s" % hour + str_codes)
gnuplot_script = """
set title "Stacked codes"
set key invert reverse Left outside
set style data histogram
set style histogram rowstacked
set style fill solid border -1
set boxwidth 0.75
set xtics rotate
set term png
set output "codes.png"
set datafile separator '\\t'
"""
plot_line = "plot 'codes.dat' using"
for pos, code in enumerate(all_codes):
if pos == 0:
plot_line += " %s:xtic(1) title '%s'" % (pos + 1, code)
else:
plot_line += ", '' using %s title '%s'" % (pos + 1, code)
gnuplot_script += plot_line
with open("codes.dat", "w") as dat_file:
dat_file.write(codes_dat)
with open("plot.script", "w") as plot_file:
plot_file.write(gnuplot_script)
print(gnuplot_script)
subprocess.run(["gnuplot", "plot.script"])
subprocess.run(["display", "codes.png"])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment